From f2bb1e99a82fe010c37d03b497f040a2761b6ff1 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Mon, 21 Nov 2022 11:19:10 +0400 Subject: [PATCH] Lab 7 --- Catamaran/App.config | 54 +++++++++++++++++- Catamaran/BoatNotFoundException.cs | 21 +++++++ Catamaran/Catamaran.csproj | 80 +++++++++++++++++++++++++++ Catamaran/FormMapWithSetBoats.cs | 54 +++++++++++++----- Catamaran/MapsCollection.cs | 10 ++-- Catamaran/Program.cs | 24 ++++++-- Catamaran/StorageOverflowException.cs | 21 +++++++ Catamaran/nlog.config | 14 +++++ Catamaran/packages.config | 27 +++++++++ 9 files changed, 279 insertions(+), 26 deletions(-) create mode 100644 Catamaran/BoatNotFoundException.cs create mode 100644 Catamaran/StorageOverflowException.cs create mode 100644 Catamaran/nlog.config create mode 100644 Catamaran/packages.config diff --git a/Catamaran/App.config b/Catamaran/App.config index 56efbc7..99add49 100644 --- a/Catamaran/App.config +++ b/Catamaran/App.config @@ -1,6 +1,58 @@ - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Catamaran/BoatNotFoundException.cs b/Catamaran/BoatNotFoundException.cs new file mode 100644 index 0000000..a49c599 --- /dev/null +++ b/Catamaran/BoatNotFoundException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + [Serializable] + internal class BoatNotFoundException : ApplicationException + { + public BoatNotFoundException(int i) : base($"Не найден объект по позиции { i}") { } + public BoatNotFoundException() : base() { } + public BoatNotFoundException(string message) : base(message) { } + public BoatNotFoundException(string message, Exception exception) : + base(message, exception) + { } + protected BoatNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/Catamaran/Catamaran.csproj b/Catamaran/Catamaran.csproj index c83a6c2..23fce1a 100644 --- a/Catamaran/Catamaran.csproj +++ b/Catamaran/Catamaran.csproj @@ -33,8 +33,84 @@ 4 + + packages\Azure.Core.1.20.0\lib\net461\Azure.Core.dll + + + packages\Azure.Data.AppConfiguration.1.2.0\lib\netstandard2.0\Azure.Data.AppConfiguration.dll + + + packages\Microsoft.Bcl.AsyncInterfaces.7.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll + + + packages\Microsoft.Extensions.Configuration.Abstractions.2.1.0\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll + + + packages\Microsoft.Extensions.DependencyInjection.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.dll + + + packages\Microsoft.Extensions.DependencyInjection.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.DependencyInjection.Abstractions.dll + + + packages\Microsoft.Extensions.Logging.7.0.0\lib\net462\Microsoft.Extensions.Logging.dll + + + packages\Microsoft.Extensions.Logging.Abstractions.7.0.0\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll + + + packages\Microsoft.Extensions.Options.7.0.0\lib\net462\Microsoft.Extensions.Options.dll + + + packages\Microsoft.Extensions.Primitives.7.0.0\lib\net462\Microsoft.Extensions.Primitives.dll + + + packages\NLog.5.0.5\lib\net46\NLog.dll + + + packages\NLog.Extensions.Logging.5.1.0\lib\net461\NLog.Extensions.Logging.dll + + + packages\Serilog.2.9.0\lib\net46\Serilog.dll + + + packages\Serilog.Extensions.Logging.3.1.0\lib\netstandard2.0\Serilog.Extensions.Logging.dll + + + packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll + + + + + packages\System.Diagnostics.DiagnosticSource.7.0.0\lib\net462\System.Diagnostics.DiagnosticSource.dll + + + + packages\System.Memory.4.5.5\lib\net461\System.Memory.dll + + + packages\System.Memory.Data.1.0.2\lib\net461\System.Memory.Data.dll + + + + packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll + + + packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll + + + packages\System.Text.Encodings.Web.4.7.2\lib\net461\System.Text.Encodings.Web.dll + + + packages\System.Text.Json.4.6.0\lib\net461\System.Text.Json.dll + + + packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll + + + packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll + @@ -48,6 +124,7 @@ + @@ -81,6 +158,7 @@ + FormBoat.cs @@ -100,6 +178,8 @@ Resources.resx True + + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/Catamaran/FormMapWithSetBoats.cs b/Catamaran/FormMapWithSetBoats.cs index 48c7d19..b1dde22 100644 --- a/Catamaran/FormMapWithSetBoats.cs +++ b/Catamaran/FormMapWithSetBoats.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -8,6 +9,8 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; + + namespace Catamaran { public partial class FormMapWithSetBoats : Form @@ -26,12 +29,19 @@ namespace Catamaran /// private readonly MapsCollection _mapsCollection; + /// + /// Логер + /// + private readonly ILogger _logger; + + /// /// Конструктор /// - public FormMapWithSetBoats() + public FormMapWithSetBoats(ILogger logger) { InitializeComponent(); + _logger = logger; _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); comboBoxSelectorMap.Items.Clear(); foreach (var elem in _mapsDict) @@ -83,6 +93,8 @@ namespace Catamaran _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); ReloadMaps(); + _logger.LogInformation($"Добавлена карта {textBoxNewMapName.Text}"); + } /// @@ -135,15 +147,27 @@ namespace Catamaran return; } int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + } } - else + catch (BoatNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); } + catch (Exception ex) + { + MessageBox.Show($"Неизвестная ошибка: {ex.Message}"); + } + } /// /// Вывод набора @@ -238,17 +262,17 @@ namespace Catamaran { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", - MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBoxButtons.OK, MessageBoxIcon.Information); } - else + catch (Exception ex) { - MessageBox.Show("Не сохранилось", "Результат", + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } - } } private void loadToolStripMenuItem_Click(object sender, EventArgs e) @@ -256,15 +280,15 @@ namespace Catamaran // TODO продумать логику if(openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) + try { MessageBox.Show("Открытие прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else + catch (Exception ex) { - MessageBox.Show("Не удалось открыть", "Результат", + MessageBox.Show($"Не удалось открыть: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } diff --git a/Catamaran/MapsCollection.cs b/Catamaran/MapsCollection.cs index f1aec84..51d7015 100644 --- a/Catamaran/MapsCollection.cs +++ b/Catamaran/MapsCollection.cs @@ -91,7 +91,7 @@ namespace Catamaran /// /// Путь и имя файла /// - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -106,7 +106,6 @@ namespace Catamaran sw.WriteLine($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}"); } } - return true; } /// @@ -115,11 +114,11 @@ namespace Catamaran /// /// - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } using (StreamReader sr = new StreamReader(filename)) { @@ -127,7 +126,7 @@ namespace Catamaran if ((str = sr.ReadLine()) == null || !str.Contains("MapsCollection")) { //если нет такой записи, то это не те данные - return false; + throw new Exception("Формат данных в файле не правильный"); } //очищаем записи _mapStorages.Clear(); @@ -148,7 +147,6 @@ namespace Catamaran _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, (char)StringSplitOptions.RemoveEmptyEntries)); } } - return true; } } } diff --git a/Catamaran/Program.cs b/Catamaran/Program.cs index fb95aab..53e4ec9 100644 --- a/Catamaran/Program.cs +++ b/Catamaran/Program.cs @@ -3,20 +3,36 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; namespace Catamaran { internal static class Program { /// - /// Главная точка входа для приложения. + /// The main entry point for the application. /// [STAThread] static void Main() { - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new FormMapWithSetBoats()); + ApplicationConfiguration.Initialize(); + 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"); + }); } } } diff --git a/Catamaran/StorageOverflowException.cs b/Catamaran/StorageOverflowException.cs new file mode 100644 index 0000000..5357ebc --- /dev/null +++ b/Catamaran/StorageOverflowException.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace Catamaran +{ + [Serializable] + internal class StorageOverflowException : ApplicationException + { + public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: { count}") { } + public StorageOverflowException() : base() { } + public StorageOverflowException(string message) : base(message) { } + public StorageOverflowException(string message, Exception exception) : + base(message, exception) + { } + protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/Catamaran/nlog.config b/Catamaran/nlog.config new file mode 100644 index 0000000..249507c --- /dev/null +++ b/Catamaran/nlog.config @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/Catamaran/packages.config b/Catamaran/packages.config new file mode 100644 index 0000000..258ab16 --- /dev/null +++ b/Catamaran/packages.config @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file