From b89a088a10570da53167df3e0661b7c91b10b283 Mon Sep 17 00:00:00 2001 From: Hells Hound Date: Sun, 27 Nov 2022 15:01:26 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AircraftCarrier/AircraftCarrier.csproj | 18 +++++++++++++ .../AircraftCarrier/FormMapWithSetWarships.cs | 27 ++++++++++++++++--- AircraftCarrier/AircraftCarrier/Program.cs | 24 ++++++++++++++++- AircraftCarrier/AircraftCarrier/nlog.config | 15 +++++++++++ 4 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 AircraftCarrier/AircraftCarrier/nlog.config diff --git a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj index 13ee123..38d723d 100644 --- a/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj +++ b/AircraftCarrier/AircraftCarrier/AircraftCarrier.csproj @@ -8,6 +8,24 @@ enable + + + + + + + Always + + + + + + + + + + + True diff --git a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs index 5fc2f29..b06332b 100644 --- a/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs +++ b/AircraftCarrier/AircraftCarrier/FormMapWithSetWarships.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -26,11 +27,16 @@ namespace AircraftCarrier /// private readonly MapsCollection _mapsCollection; /// + /// Логер + /// + private readonly ILogger _logger; + /// /// Конструктор /// - public FormMapWithSetWarships() + public FormMapWithSetWarships(ILogger logger) { InitializeComponent(); + _logger = logger; _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); comboBoxSelectorMap.Items.Clear(); foreach (var elem in _mapsDict) @@ -70,15 +76,18 @@ namespace AircraftCarrier if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation("При добавлении карты {0}", comboBoxSelectorMap.SelectedIndex == -1 ? "Не была выбрана карта" : "Не была названа карта"); return; } if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) { MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning("Нет карты с названием: {0}", textBoxNewMapName.Text); return; } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); ReloadMaps(); + _logger.LogInformation("Добавлена карта {0}", textBoxNewMapName.Text); } /// /// Выбор карты @@ -88,6 +97,7 @@ namespace AircraftCarrier private void ListBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + _logger.LogInformation("Был осуществлен переход на карту под названием: {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty); } /// /// Удаление карты @@ -104,6 +114,7 @@ namespace AircraftCarrier if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + _logger.LogInformation("Удалена карта {0}", listBoxMaps.SelectedItem?.ToString() ?? string.Empty); ReloadMaps(); } } @@ -135,16 +146,19 @@ namespace AircraftCarrier if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + warship >= 0) { MessageBox.Show("Объект добавлен"); + _logger.LogInformation("Добавлен объект {@Airplane}", warship); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось добавить объект"); + _logger.LogInformation("Не удалось добавить объект"); } } catch(StorageOverflowException ex) { MessageBox.Show("Не удалось добавить объект"); + _logger.LogWarning("Ошибка переполнения хранилища: {0}", ex.Message); MessageBox.Show($"Ошибка переполнения хранилища: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -166,16 +180,19 @@ namespace AircraftCarrier if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); + _logger.LogInformation("Из текущей карты удален объект {@ship}", (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos)); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation("Не удалось добавить объект по позиции {0} равен null", pos); } } catch (WarshipNotFoundException ex) { MessageBox.Show($"Ошибка удаления: {ex.Message}"); + _logger.LogWarning("Ошибка удаления: {0}", ex.Message); } } /// @@ -247,10 +264,12 @@ namespace AircraftCarrier try { _mapsCollection.SaveData(saveFileDialog.FileName); + _logger.LogInformation("Сохранение прошло успешно. Файл находится: {0}", saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch(Exception ex) { + _logger.LogWarning("Не удалось сохранить файл '{0}'. Текст ошибки: {1}", saveFileDialog.FileName, ex.Message); MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } @@ -267,11 +286,13 @@ namespace AircraftCarrier try { _mapsCollection.LoadData(openFileDialog.FileName); - MessageBox.Show("Загрузка прошла успешно!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation("Открытие файла '{0}' прошло успешно", openFileDialog.FileName); + MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } catch (Exception ex) { + _logger.LogWarning("Не удалось открыть файл {0}. Текст ошибки: {1}", openFileDialog.FileName, ex.Message); MessageBox.Show($"Не удалось открыть: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/AircraftCarrier/AircraftCarrier/Program.cs b/AircraftCarrier/AircraftCarrier/Program.cs index 00b84f4..5728ab7 100644 --- a/AircraftCarrier/AircraftCarrier/Program.cs +++ b/AircraftCarrier/AircraftCarrier/Program.cs @@ -1,3 +1,8 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System.ServiceProcess; + namespace AircraftCarrier { internal static class Program @@ -11,7 +16,24 @@ namespace AircraftCarrier // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMapWithSetWarships()); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = services.BuildServiceProvider()) + { + Application.Run(serviceProvider.GetRequiredService()); + } + } + + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + + + option.SetMinimumLevel(LogLevel.Information); + option.AddNLog("nlog.config"); + }); } } } \ No newline at end of file diff --git a/AircraftCarrier/AircraftCarrier/nlog.config b/AircraftCarrier/AircraftCarrier/nlog.config new file mode 100644 index 0000000..4c09ecb --- /dev/null +++ b/AircraftCarrier/AircraftCarrier/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file