Лабораторная работа №7
This commit is contained in:
parent
4babf537a4
commit
e380554720
@ -1,4 +1,6 @@
|
|||||||
namespace ProjectAirbus.CollectionGenericObjects;
|
using ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
|
namespace ProjectAirbus.CollectionGenericObjects;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Параметризованный набор объектов
|
/// Параметризованный набор объектов
|
||||||
@ -44,7 +46,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
|
|||||||
// TODO проверка позиции
|
// TODO проверка позиции
|
||||||
if (position < 0 || position >= Count)
|
if (position < 0 || position >= Count)
|
||||||
{
|
{
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
@ -54,7 +56,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
|
|||||||
{
|
{
|
||||||
if (Count == _maxCount)
|
if (Count == _maxCount)
|
||||||
{
|
{
|
||||||
return -1;
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
_collection.Add(obj);
|
_collection.Add(obj);
|
||||||
return Count;
|
return Count;
|
||||||
@ -64,11 +66,11 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
|
|||||||
{
|
{
|
||||||
if (Count == _maxCount)
|
if (Count == _maxCount)
|
||||||
{
|
{
|
||||||
return -1;
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
if (position >= Count || position < 0)
|
if (position >= Count || position < 0)
|
||||||
{
|
{
|
||||||
return -1;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
_collection.Insert(position, obj);
|
_collection.Insert(position, obj);
|
||||||
return position;
|
return position;
|
||||||
@ -78,7 +80,7 @@ public class ListGenericObjects<T> : ICollectionGenericObjects<T> where T : clas
|
|||||||
{
|
{
|
||||||
if (position >= Count || position < 0)
|
if (position >= Count || position < 0)
|
||||||
{
|
{
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
T obj = _collection[position];
|
T obj = _collection[position];
|
||||||
_collection.RemoveAt(position);
|
_collection.RemoveAt(position);
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace ProjectAirbus.CollectionGenericObjects;
|
using ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
|
namespace ProjectAirbus.CollectionGenericObjects;
|
||||||
|
|
||||||
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : class
|
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : class
|
||||||
{
|
{
|
||||||
@ -44,11 +46,14 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
|
|||||||
public T? Get(int position)
|
public T? Get(int position)
|
||||||
{
|
{
|
||||||
// TODO проверка позиции
|
// TODO проверка позиции
|
||||||
if (position < 0 || position > Count)
|
if (position < 0 || position >= Count)
|
||||||
{
|
{
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
|
}
|
||||||
|
if (_collection[position] == null)
|
||||||
|
{
|
||||||
|
//throw new ObjectNotFoundException(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
return _collection[position];
|
return _collection[position];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,7 +69,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T obj, int position)
|
public int Insert(T obj, int position)
|
||||||
@ -77,7 +82,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
|
|||||||
// TODO вставка
|
// TODO вставка
|
||||||
if (position < 0 || position >= Count)
|
if (position < 0 || position >= Count)
|
||||||
{
|
{
|
||||||
return -1;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_collection[position] == null)
|
if (_collection[position] == null)
|
||||||
@ -106,7 +111,7 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
throw new CollectionOverflowException(Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Remove(int position)
|
public T Remove(int position)
|
||||||
@ -116,10 +121,14 @@ public class MassiveGenericObjects<T> : ICollectionGenericObjects<T> where T : c
|
|||||||
// присвоив элементу массива значение null
|
// присвоив элементу массива значение null
|
||||||
if (position >= Count || position < 0)
|
if (position >= Count || position < 0)
|
||||||
{
|
{
|
||||||
return null;
|
throw new PositionOutOfCollectionException(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
T obj = _collection[position];
|
T? obj = _collection[position];
|
||||||
|
if (obj == null)
|
||||||
|
{
|
||||||
|
throw new ObjectNotFoundException(position);
|
||||||
|
}
|
||||||
_collection[position] = null;
|
_collection[position] = null;
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using ProjectAirbus.Drawnings;
|
using ProjectAirbus.Drawnings;
|
||||||
|
using ProjectAirbus.Exceptions;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace ProjectAirbus.CollectionGenericObjects;
|
namespace ProjectAirbus.CollectionGenericObjects;
|
||||||
@ -98,12 +99,11 @@ public class StorageCollection<T> where T : DrawningAirbus
|
|||||||
/// Сохранение информации по автомобилям в хранилище в файл
|
/// Сохранение информации по автомобилям в хранилище в файл
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - сохранение прошло успешно, false - ошибка при сохранение данных</returns>
|
public void SaveData(string filename)
|
||||||
public bool SaveData(string filename)
|
|
||||||
{
|
{
|
||||||
if (_storages.Count == 0)
|
if (_storages.Count == 0)
|
||||||
{
|
{
|
||||||
return false;
|
throw new Exception("В хранилище отсутствуют коллекции для сохранения");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (File.Exists(filename))
|
if (File.Exists(filename))
|
||||||
@ -146,22 +146,18 @@ public class StorageCollection<T> where T : DrawningAirbus
|
|||||||
|
|
||||||
writer.Write(sb);
|
writer.Write(sb);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Загрузка информации по автомобилям в хранилище из файла
|
/// Загрузка информации по автомобилям в хранилище из файла
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - загрузка прошла успешно, false - ошибка при загрузке данных</returns>
|
public void LoadData(string filename)
|
||||||
public bool LoadData(string filename)
|
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename))
|
||||||
{
|
{
|
||||||
return false;
|
throw new FileNotFoundException("Файл не существует");
|
||||||
}
|
}
|
||||||
|
|
||||||
using (StreamReader fs = File.OpenText(filename))
|
using (StreamReader fs = File.OpenText(filename))
|
||||||
@ -170,12 +166,12 @@ public class StorageCollection<T> where T : DrawningAirbus
|
|||||||
|
|
||||||
if (str == null || str.Length == 0)
|
if (str == null || str.Length == 0)
|
||||||
{
|
{
|
||||||
return false;
|
throw new IOException("В файле нет данных");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!str.StartsWith(_collectionKey))
|
if (!str.StartsWith(_collectionKey))
|
||||||
{
|
{
|
||||||
return false;
|
throw new IOException("В файле неверные данные");
|
||||||
}
|
}
|
||||||
|
|
||||||
_storages.Clear();
|
_storages.Clear();
|
||||||
@ -189,12 +185,8 @@ public class StorageCollection<T> where T : DrawningAirbus
|
|||||||
}
|
}
|
||||||
|
|
||||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||||
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenericObjects<T>? collection = StorageCollection<T>.CreateCollection(collectionType) ??
|
||||||
if (collection == null)
|
throw new Exception("Не удалось определить тип коллекции: " + record[1]);
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
collection.MaxCount = Convert.ToInt32(record[2]);
|
collection.MaxCount = Convert.ToInt32(record[2]);
|
||||||
|
|
||||||
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
string[] set = record[3].Split(_separatorItems, StringSplitOptions.RemoveEmptyEntries);
|
||||||
@ -202,15 +194,21 @@ public class StorageCollection<T> where T : DrawningAirbus
|
|||||||
{
|
{
|
||||||
if (elem?.CreateDrawningAirbus() is T airbus)
|
if (elem?.CreateDrawningAirbus() is T airbus)
|
||||||
{
|
{
|
||||||
if (collection.Insert(airbus) == -1)
|
try
|
||||||
{
|
{
|
||||||
return false;
|
if (collection.Insert(airbus) == -1)
|
||||||
|
{
|
||||||
|
throw new Exception("Объект не удалось добавить в коллекцию: " + record[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
throw new Exception("Коллекция переполнена", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
_storages.Add(record[0], collection);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,20 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку переполнения коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class CollectionOverflowException : ApplicationException
|
||||||
|
{
|
||||||
|
public CollectionOverflowException(int Count) : base("В коллекции превышено допустимое количество: " + Count) { }
|
||||||
|
|
||||||
|
public CollectionOverflowException() : base() { }
|
||||||
|
|
||||||
|
public CollectionOverflowException(string Message) : base(Message) { }
|
||||||
|
|
||||||
|
public CollectionOverflowException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
protected CollectionOverflowException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку, что по указанной позиции нет элемента
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class ObjectNotFoundException : ApplicationException
|
||||||
|
{
|
||||||
|
public ObjectNotFoundException(int i) : base("Не найден объект по позиции " + i) { }
|
||||||
|
|
||||||
|
public ObjectNotFoundException() : base() { }
|
||||||
|
|
||||||
|
public ObjectNotFoundException(string message) : base(message) { }
|
||||||
|
|
||||||
|
public ObjectNotFoundException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
protected ObjectNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
|
||||||
|
namespace ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Класс, описывающий ошибку выхода за границы коллекции
|
||||||
|
/// </summary>
|
||||||
|
[Serializable]
|
||||||
|
internal class PositionOutOfCollectionException : ApplicationException
|
||||||
|
{
|
||||||
|
public PositionOutOfCollectionException(int i) : base("Выход за границы коллекции. Позиция " + i) { }
|
||||||
|
|
||||||
|
public PositionOutOfCollectionException() : base() { }
|
||||||
|
|
||||||
|
public PositionOutOfCollectionException(string message) : base(message) { }
|
||||||
|
|
||||||
|
public PositionOutOfCollectionException(string message, Exception exception) : base(message, exception) { }
|
||||||
|
|
||||||
|
protected PositionOutOfCollectionException(SerializationInfo info, StreamingContext context) : base(info, context) { }
|
||||||
|
}
|
@ -1,5 +1,7 @@
|
|||||||
using ProjectAirbus.CollectionGenericObjects;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using ProjectAirbus.CollectionGenericObjects;
|
||||||
using ProjectAirbus.Drawnings;
|
using ProjectAirbus.Drawnings;
|
||||||
|
using ProjectAirbus.Exceptions;
|
||||||
|
|
||||||
namespace ProjectAirbus;
|
namespace ProjectAirbus;
|
||||||
|
|
||||||
@ -18,13 +20,20 @@ public partial class FormAirbusCollection : Form
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private AbstractCompany? _company = null;
|
private AbstractCompany? _company = null;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Логер
|
||||||
|
/// </summary>
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Конструктор
|
/// Конструктор
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public FormAirbusCollection()
|
public FormAirbusCollection(ILogger<FormAirbusCollection> logger)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_storageCollection = new();
|
_storageCollection = new();
|
||||||
|
_logger = logger;
|
||||||
|
_logger.LogInformation("Форма создалась");
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -56,19 +65,25 @@ public partial class FormAirbusCollection : Form
|
|||||||
/// <param name="airbus"></param>
|
/// <param name="airbus"></param>
|
||||||
private void SetAirbus(DrawningAirbus airbus)
|
private void SetAirbus(DrawningAirbus airbus)
|
||||||
{
|
{
|
||||||
if (_company == null || airbus == null)
|
try
|
||||||
{
|
{
|
||||||
return;
|
if (_company == null || airbus == null)
|
||||||
}
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (_company + airbus != -1)
|
if (_company + airbus != -1)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект добавлен");
|
MessageBox.Show("Объект добавлен");
|
||||||
pictureBox.Image = _company.Show();
|
pictureBox.Image = _company.Show();
|
||||||
|
_logger.LogInformation("Добавлен объект: " + airbus.GetDataForSave());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (ObjectNotFoundException ex) { }
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось добавить объект");
|
MessageBox.Show(ex.Message);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,14 +105,19 @@ public partial class FormAirbusCollection : Form
|
|||||||
}
|
}
|
||||||
|
|
||||||
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
|
||||||
if (_company - pos != null)
|
try
|
||||||
{
|
{
|
||||||
MessageBox.Show("Объект удален");
|
if (_company - pos != null)
|
||||||
pictureBox.Image = _company.Show();
|
{
|
||||||
|
MessageBox.Show("Объект удален");
|
||||||
|
pictureBox.Image = _company.Show();
|
||||||
|
_logger.LogInformation("Удален объект по позиции " + pos);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не удалось удалить объект");
|
MessageBox.Show(ex.Message);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,26 +135,33 @@ public partial class FormAirbusCollection : Form
|
|||||||
|
|
||||||
DrawningAirbus? airbus = null;
|
DrawningAirbus? airbus = null;
|
||||||
int counter = 100;
|
int counter = 100;
|
||||||
while (airbus == null)
|
try
|
||||||
{
|
{
|
||||||
airbus = _company.GetRandomObject();
|
while (airbus == null)
|
||||||
counter--;
|
|
||||||
if (counter <= 0)
|
|
||||||
{
|
{
|
||||||
break;
|
airbus = _company.GetRandomObject();
|
||||||
|
counter--;
|
||||||
|
if (counter <= 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (airbus == null)
|
if (airbus == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FormAirbus form = new()
|
FormAirbus form = new()
|
||||||
|
{
|
||||||
|
SetAirbus = airbus
|
||||||
|
};
|
||||||
|
form.ShowDialog();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
SetAirbus = airbus
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
};
|
}
|
||||||
form.ShowDialog();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -164,19 +191,26 @@ public partial class FormAirbusCollection : Form
|
|||||||
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
CollectionType collectionType = CollectionType.None;
|
|
||||||
if (radioButtonMassive.Checked)
|
|
||||||
{
|
{
|
||||||
collectionType = CollectionType.Massive;
|
CollectionType collectionType = CollectionType.None;
|
||||||
}
|
if (radioButtonMassive.Checked)
|
||||||
else if (radioButtonList.Checked)
|
{
|
||||||
{
|
collectionType = CollectionType.Massive;
|
||||||
collectionType = CollectionType.List;
|
}
|
||||||
}
|
else if (radioButtonList.Checked)
|
||||||
|
{
|
||||||
|
collectionType = CollectionType.List;
|
||||||
|
}
|
||||||
|
|
||||||
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType);
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
|
_logger.LogInformation("Коллекция добавлена " + textBoxCollectionName.Text);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -191,15 +225,21 @@ public partial class FormAirbusCollection : Form
|
|||||||
MessageBox.Show("Коллекция не выбрана");
|
MessageBox.Show("Коллекция не выбрана");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
try
|
||||||
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
|
||||||
{
|
{
|
||||||
return;
|
if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
||||||
|
_logger.LogInformation("Коллекция " + listBoxCollection.SelectedItem.ToString() + " удалена");
|
||||||
|
RefreshListBoxItems();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString());
|
|
||||||
RefreshListBoxItems();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -258,13 +298,16 @@ public partial class FormAirbusCollection : Form
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -278,13 +321,16 @@ public partial class FormAirbusCollection : Form
|
|||||||
{
|
{
|
||||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.LoadData(openFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.LoadData(openFileDialog.FileName);
|
||||||
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
MessageBox.Show("Не загрузилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("Ошибка: {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
|
@ -1,3 +1,8 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace ProjectAirbus
|
namespace ProjectAirbus
|
||||||
{
|
{
|
||||||
internal static class Program
|
internal static class Program
|
||||||
@ -11,7 +16,34 @@ namespace ProjectAirbus
|
|||||||
// To customize application configuration such as set high DPI settings or default font,
|
// To customize application configuration such as set high DPI settings or default font,
|
||||||
// see https://aka.ms/applicationconfiguration.
|
// see https://aka.ms/applicationconfiguration.
|
||||||
ApplicationConfiguration.Initialize();
|
ApplicationConfiguration.Initialize();
|
||||||
Application.Run(new FormAirbusCollection());
|
ServiceCollection services = new();
|
||||||
|
ConfigureServices(services);
|
||||||
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||||
|
Application.Run(serviceProvider.GetRequiredService<FormAirbusCollection>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Êîíôèãóðàöèÿ ñåðâèñà DI
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="services"></param>
|
||||||
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
|
{
|
||||||
|
// services.AddSingleton<FormCarCollection>()
|
||||||
|
//.AddLogging(option =>
|
||||||
|
//{
|
||||||
|
// option.SetMinimumLevel(LogLevel.Information);
|
||||||
|
// option.AddNLog("nlog.config");
|
||||||
|
//});
|
||||||
|
services.AddSingleton<FormAirbusCollection>()
|
||||||
|
.AddLogging(option =>
|
||||||
|
{
|
||||||
|
option.SetMinimumLevel(LogLevel.Information);
|
||||||
|
option.AddSerilog(new LoggerConfiguration()
|
||||||
|
.ReadFrom.Configuration(new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("serilog.json")
|
||||||
|
.Build())
|
||||||
|
.CreateLogger());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,9 +9,15 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Remove="MovementStrategy\Entities\**" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||||
<EmbeddedResource Remove="MovementStrategy\Entities\**" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="8.0.0" />
|
||||||
<None Remove="MovementStrategy\Entities\**" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
|
||||||
|
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||||
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||||
|
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@ -29,4 +35,10 @@
|
|||||||
</EmbeddedResource>
|
</EmbeddedResource>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="serilog.json">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
18
ProjectAirbus/ProjectAirbus/serilog.json
Normal file
18
ProjectAirbus/ProjectAirbus/serilog.json
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
{
|
||||||
|
"Serilog": {
|
||||||
|
"Using": [ "Serilog.Sinks.File" ],
|
||||||
|
"MinimumLevel": "Information",
|
||||||
|
"WriteTo": [
|
||||||
|
{
|
||||||
|
"Name": "File",
|
||||||
|
"Args": {
|
||||||
|
"path": "log.log",
|
||||||
|
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"Properties": {
|
||||||
|
"Application": "Airbus"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user