From 31570fb993e170e8f4ef9af0790f3289c0a5148f Mon Sep 17 00:00:00 2001 From: F1rsTTeaM Date: Sun, 5 May 2024 13:56:19 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D1=8E=D1=89=D0=B8=D0=B9=20=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B3=D0=B5=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormAirplaneCollection.cs | 84 +++++++++++++++---- .../ProjectAirplaneWithRadar/Program.cs | 33 +++++++- .../ProjectAirplaneWithRadar.csproj | 19 +++++ .../ProjectAirplaneWithRadar/serilog.json | 15 ++++ 4 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 AirplaneWithRadar/ProjectAirplaneWithRadar/serilog.json diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs index a477cac..87892e9 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/FormAirplaneCollection.cs @@ -1,4 +1,6 @@ -using ProjectAirplaneWithRadar.CollectionGenericObjects; +using System.Numerics; +using Microsoft.Extensions.Logging; +using ProjectAirplaneWithRadar.CollectionGenericObjects; using ProjectAirplaneWithRadar.Drawnings; using ProjectAirplaneWithRadar.Exceptions; @@ -19,13 +21,20 @@ namespace ProjectAirplaneWithRadar /// private AbstractCompany? _company = null; + /// + /// Логгер + /// + private readonly ILogger _logger; + /// /// Конструктор /// - public FormAirplaneCollection() + public FormAirplaneCollection(ILogger logger) { InitializeComponent(); _storageCollection = new(); + _logger = logger; + _logger.LogInformation("Форма загрузилась"); } /// @@ -65,15 +74,18 @@ namespace ProjectAirplaneWithRadar { MessageBox.Show("Объект добавлен"); pictureBox.Image = _company.Show(); + _logger.LogInformation("Добавлен объект: " + airplane.GetDataForSave()); } } catch (CollectionOverflowException ex) { MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } catch (ObjectNotFoundException ex) { MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -102,13 +114,16 @@ namespace ProjectAirplaneWithRadar { MessageBox.Show("Объект удален"); pictureBox.Image = _company.Show(); + _logger.LogInformation("Удален объект по позиции " + pos); } } catch (PositionOutOfCollectionException ex) { MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } catch (ObjectNotFoundException ex) { MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -187,14 +202,34 @@ namespace ProjectAirplaneWithRadar return; } - CollectionType collectionType = CollectionType.None; - if (radioButtonMassive.Checked) - collectionType = CollectionType.Massive; - else if (radioButtonList.Checked) - collectionType = CollectionType.List; + //CollectionType collectionType = CollectionType.None; + //if (radioButtonMassive.Checked) + // collectionType = CollectionType.Massive; + //else if (radioButtonList.Checked) + // collectionType = CollectionType.List; - _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); - RefreshListBoxItems(); + //_storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + //RefreshListBoxItems(); + + try + { + CollectionType collectionType = CollectionType.None; + if (radioButtonMassive.Checked) + { + collectionType = CollectionType.Massive; + } + else if (radioButtonList.Checked) + { + collectionType = CollectionType.List; + } + _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); + RefreshListBoxItems(); + _logger.LogInformation("Коллекция добавлена " + textBoxCollectionName.Text); + } + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); + } } /// @@ -209,13 +244,28 @@ namespace ProjectAirplaneWithRadar MessageBox.Show("Коллекция не выбрана"); return; } - 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()); - RefreshListBoxItems(); + //_storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + //RefreshListBoxItems(); + + try + { + if (MessageBox.Show("Удалить коллекцию?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) + { + return; + } + _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + RefreshListBoxItems(); + _logger.LogInformation("Коллекция: " + listBoxCollection.SelectedItem.ToString() + " удалена"); + } + catch (Exception ex) + { + _logger.LogError("Ошибка: {Message}", ex.Message); + } } /// @@ -276,10 +326,12 @@ namespace ProjectAirplaneWithRadar { _storageCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Сохранение в файл: {filename}", saveFileDialog.FileName); } catch(Exception ex) { MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } @@ -298,10 +350,12 @@ namespace ProjectAirplaneWithRadar _storageCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); RefreshListBoxItems(); + _logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName); } catch(Exception ex) { MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs b/AirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs index 42c6dca..4430a39 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/Program.cs @@ -1,3 +1,8 @@ +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; + namespace ProjectAirplaneWithRadar { internal static class Program @@ -11,7 +16,33 @@ namespace ProjectAirplaneWithRadar // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormAirplaneCollection()); + ServiceCollection services = new(); + ConfigureServices(services); + using ServiceProvider serviceProvider = services.BuildServiceProvider(); + Application.Run(serviceProvider.GetRequiredService()); } + + /// + /// DI + /// + /// + private static void ConfigureServices(ServiceCollection services) + { + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder().AddJsonFile($"{pathNeed}serilog.json").Build()).CreateLogger()); + }); + } + + } } \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar.csproj b/AirplaneWithRadar/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar.csproj index af03d74..1a2decf 100644 --- a/AirplaneWithRadar/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar.csproj +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/ProjectAirplaneWithRadar.csproj @@ -8,6 +8,19 @@ enable + + + + + + + + + + + + + True @@ -23,4 +36,10 @@ + + + Always + + + \ No newline at end of file diff --git a/AirplaneWithRadar/ProjectAirplaneWithRadar/serilog.json b/AirplaneWithRadar/ProjectAirplaneWithRadar/serilog.json new file mode 100644 index 0000000..0036428 --- /dev/null +++ b/AirplaneWithRadar/ProjectAirplaneWithRadar/serilog.json @@ -0,0 +1,15 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { "path": "log.log" } + } + ], + "Properties": { + "Application": "Sample" + } + } +} \ No newline at end of file