From a7feb55e23f0c925c5e0acfceeedfb5980e7e37a Mon Sep 17 00:00:00 2001 From: shadowik Date: Mon, 28 Nov 2022 17:38:26 +0400 Subject: [PATCH 1/6] Exeptions --- .../DoubleDeckerBus/BusNotFoundException.cs | 19 +++++++++ .../DoubleDeckerBus/FormMapWithSetBuses.cs | 39 ++++++++++++------- .../DoubleDeckerBus/MapsCollection.cs | 11 ++---- .../DoubleDeckerBus/SetBusesGeneric.cs | 6 ++- .../StorageOverFlowException.cs | 19 +++++++++ 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs diff --git a/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs b/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs new file mode 100644 index 0000000..3757dac --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/BusNotFoundException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus +{ + [Serializable] + internal class BusNotFoundException : ApplicationException + { + public BusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { } + public BusNotFoundException() : base() { } + public BusNotFoundException(string message) : base(message) { } + public BusNotFoundException(string message, Exception exception) : base(message, exception) { } + protected BusNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index 9b404cb..f02e109 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -88,15 +88,27 @@ namespace DoubleDeckerBus 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 (BusNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); + MessageBox.Show($"Ошибка удаления: {ex.Message}"); } + catch (Exception ex) + { + MessageBox.Show($"Неизветсная шибка: {ex.Message}"); + } + } private void ButtonShowStorage_Click(object sender, EventArgs e) @@ -183,13 +195,13 @@ namespace DoubleDeckerBus { if (saveFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.SaveData(saveFileDialog.FileName)) + try { + _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); } - else - { - MessageBox.Show("Не сохранилось", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + catch (Exception ex) { + MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } @@ -198,13 +210,14 @@ namespace DoubleDeckerBus { if (openFileDialog.ShowDialog() == DialogResult.OK) { - if (_mapsCollection.LoadData(openFileDialog.FileName)) - { + try { + _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); ReloadMaps(); } - else { - MessageBox.Show("Ошибка при загрузке", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + catch (Exception ex) + { + MessageBox.Show($"Ошибка при загрузке: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs index 5642947..c7e70e2 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs @@ -51,7 +51,7 @@ namespace DoubleDeckerBus stream.Write(info, 0, info.Length); } - public bool SaveData(string filename) + public void SaveData(string filename) { if (File.Exists(filename)) { @@ -65,14 +65,13 @@ namespace DoubleDeckerBus sw.Write($"{storage.Key}{separatorDict}{storage.Value.GetData(separatorDict, separatorData)}{Environment.NewLine}"); } } - return true; } - public bool LoadData(string filename) + public void LoadData(string filename) { if (!File.Exists(filename)) { - return false; + throw new Exception("Файл не найден"); } string line; using (StreamReader sw = new(filename)) @@ -80,7 +79,7 @@ namespace DoubleDeckerBus line = sw.ReadLine(); if (line == null || !line.Contains("MapsCollection")) { - return false; + throw new Exception("Формат данных в файле не совпадает"); } _mapStorages.Clear(); @@ -102,8 +101,6 @@ namespace DoubleDeckerBus _mapStorages[elem[0]].LoadData(elem[2].Split(separatorData, StringSplitOptions.RemoveEmptyEntries)); line = sw.ReadLine(); } - - return true; } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index 1da17a2..00927bc 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -28,9 +28,12 @@ namespace DoubleDeckerBus } public int Insert(T bus, int position) - { + { + // TODO проверка на _maxCount if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; + // TODO проверка позиции + BusyPlaces++; _places.Insert(position, bus); return position; @@ -38,6 +41,7 @@ namespace DoubleDeckerBus public T Remove(int position) { + // TODO проверка позиции if (position < 0 || position >= _maxCount) return null; T savedBus = _places[position]; _places.RemoveAt(position); diff --git a/DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs b/DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs new file mode 100644 index 0000000..1e6d319 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/StorageOverFlowException.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace DoubleDeckerBus +{ + [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) { } + } +} -- 2.25.1 From ceb8d66616830dc651ac24a45a2610a310c62166 Mon Sep 17 00:00:00 2001 From: shadowik Date: Mon, 28 Nov 2022 18:00:06 +0400 Subject: [PATCH 2/6] Logs --- .../DoubleDeckerBus/DoubleDeckerBus.csproj | 18 ++++++++++++++++ .../DoubleDeckerBus/FormMapWithSetBuses.cs | 9 ++++++-- DoubleDeckerBus/DoubleDeckerBus/Program.cs | 21 ++++++++++++++++++- DoubleDeckerBus/DoubleDeckerBus/nlog.config | 15 +++++++++++++ 4 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 DoubleDeckerBus/DoubleDeckerBus/nlog.config diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj index d7b238e..b0ba214 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj @@ -8,6 +8,24 @@ enable + + + + + + + Always + + + + + + + + + + + PreserveNewest diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index f02e109..eec2e1c 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.Extensions.Logging; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -21,9 +22,12 @@ namespace DoubleDeckerBus private readonly MapsCollection _mapsCollection; - public FormMapWithSetBuses() + private readonly ILogger _logger; + + public FormMapWithSetBuses(ILogger logger) { InitializeComponent(); + _logger = logger; _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); comboBoxSelectorMap.Items.Clear(); foreach (var item in _mapsDict) { @@ -170,6 +174,7 @@ namespace DoubleDeckerBus } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); ReloadMaps(); + _logger.LogInformation($"Добавлена карта {textBoxNewMapName.Text}"); } private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) diff --git a/DoubleDeckerBus/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/DoubleDeckerBus/Program.cs index 66c3e0d..c3de90f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Program.cs @@ -1,3 +1,8 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using NLog.Extensions.Logging; +using System.ServiceProcess; + namespace DoubleDeckerBus { internal static class Program @@ -11,7 +16,21 @@ namespace DoubleDeckerBus // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormMapWithSetBuses()); + 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/DoubleDeckerBus/DoubleDeckerBus/nlog.config b/DoubleDeckerBus/DoubleDeckerBus/nlog.config new file mode 100644 index 0000000..3e28e76 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/nlog.config @@ -0,0 +1,15 @@ + + + + + + + + + + + + + \ No newline at end of file -- 2.25.1 From 0eb06f89e268c546bf03c8fa2b6bce7d4d1e9ce4 Mon Sep 17 00:00:00 2001 From: shadowik Date: Mon, 28 Nov 2022 19:31:44 +0400 Subject: [PATCH 3/6] Final --- .../DoubleDeckerBus/DoubleDeckerBus.csproj | 17 +++++++++--- .../DoubleDeckerBus/FormMapWithSetBuses.cs | 18 +++++++++++++ .../DoubleDeckerBus/MapsCollection.cs | 4 +-- DoubleDeckerBus/DoubleDeckerBus/Program.cs | 13 +++++++--- .../DoubleDeckerBus/SetBusesGeneric.cs | 19 +++++++++----- DoubleDeckerBus/DoubleDeckerBus/nlog.config | 15 ----------- .../DoubleDeckerBus/serilog.Designer.cs | 26 +++++++++++++++++++ DoubleDeckerBus/DoubleDeckerBus/serilog.json | 20 ++++++++++++++ 8 files changed, 103 insertions(+), 29 deletions(-) delete mode 100644 DoubleDeckerBus/DoubleDeckerBus/nlog.config create mode 100644 DoubleDeckerBus/DoubleDeckerBus/serilog.Designer.cs create mode 100644 DoubleDeckerBus/DoubleDeckerBus/serilog.json diff --git a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj index b0ba214..4200721 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj +++ b/DoubleDeckerBus/DoubleDeckerBus/DoubleDeckerBus.csproj @@ -9,21 +9,27 @@ - + - + + SettingsSingleFileGenerator + serilog.Designer.cs Always + + + - + + @@ -35,6 +41,11 @@ True Resources.resx + + True + True + serilog.json + diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index eec2e1c..779e742 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -74,10 +74,12 @@ namespace DoubleDeckerBus { MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + _logger.LogInformation($"Объект добавлен"); } else { MessageBox.Show("Не удалось добавить объект"); + _logger.LogInformation($"Объект не добаавлен"); } } @@ -97,20 +99,24 @@ namespace DoubleDeckerBus if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Объект удален"); + _logger.LogInformation($"Объект удален"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else { MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation($"Объект не удален"); } } catch (BusNotFoundException ex) { MessageBox.Show($"Ошибка удаления: {ex.Message}"); + _logger.LogWarning("Автобус не найден"); } catch (Exception ex) { MessageBox.Show($"Неизветсная шибка: {ex.Message}"); + _logger.LogWarning("Неизвестная ошибка при удалении"); } } @@ -122,6 +128,7 @@ namespace DoubleDeckerBus return; } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + _logger.LogInformation($"Отображение хранилища"); } private void ButtonShowOnMap_Click(object sender, EventArgs e) @@ -131,6 +138,7 @@ namespace DoubleDeckerBus return; } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowOnMap(); + _logger.LogInformation($"Отображение карты"); } private void ButtonMove_Click(object sender, EventArgs e) @@ -158,6 +166,7 @@ namespace DoubleDeckerBus break; } pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].MoveObject(dir); + _logger.LogInformation($"Передвижение {name}"); } private void ButtonAddMap_Click(object sender, EventArgs e) @@ -165,11 +174,13 @@ namespace DoubleDeckerBus if (comboBoxSelectorMap.SelectedIndex == -1 || string.IsNullOrEmpty(textBoxNewMapName.Text)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Не все данные заполнены при добавлени карты"); return; } if (!_mapsDict.ContainsKey(comboBoxSelectorMap.Text)) { MessageBox.Show("Нет такой карты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Нет такой карты {comboBoxSelectorMap.Text}"); return; } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); @@ -180,18 +191,21 @@ namespace DoubleDeckerBus private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + _logger.LogInformation($"Выбраная карта изменилась {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}"); } private void ButtonDeleteMap_Click(object sender, EventArgs e) { if (listBoxMaps.SelectedIndex == -1) { + _logger.LogWarning("Удаление карты не произошло. Не выбрана карта"); return; } if (MessageBox.Show($"Удалить карту {listBoxMaps.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + _logger.LogInformation($"Удалена карта {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}"); ReloadMaps(); } } @@ -204,9 +218,11 @@ namespace DoubleDeckerBus { _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Сохранение {openFileDialog.FileName} прошло успешно"); } catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Сохранение {openFileDialog.FileName} прошло не успешно"); } } } @@ -218,11 +234,13 @@ namespace DoubleDeckerBus try { _mapsCollection.LoadData(openFileDialog.FileName); MessageBox.Show("Загрузка прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogInformation($"Загрузка из файла {openFileDialog.FileName} прошла успешна"); ReloadMaps(); } catch (Exception ex) { MessageBox.Show($"Ошибка при загрузке: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning($"Загрузка из файла {openFileDialog.FileName} прошла не успешно"); } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs index c7e70e2..eff74e7 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapsCollection.cs @@ -71,7 +71,7 @@ namespace DoubleDeckerBus { if (!File.Exists(filename)) { - throw new Exception("Файл не найден"); + throw new FileNotFoundException("Файл не найден"); } string line; using (StreamReader sw = new(filename)) @@ -79,7 +79,7 @@ namespace DoubleDeckerBus line = sw.ReadLine(); if (line == null || !line.Contains("MapsCollection")) { - throw new Exception("Формат данных в файле не совпадает"); + throw new FileFormatException("Формат данных в файле не совпадает"); } _mapStorages.Clear(); diff --git a/DoubleDeckerBus/DoubleDeckerBus/Program.cs b/DoubleDeckerBus/DoubleDeckerBus/Program.cs index c3de90f..75a901f 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/Program.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/Program.cs @@ -1,7 +1,8 @@ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging; -using System.ServiceProcess; +using Serilog; +using Serilog.Extensions.Logging; namespace DoubleDeckerBus { @@ -27,8 +28,14 @@ namespace DoubleDeckerBus private static void ConfigureServices(ServiceCollection services) { services.AddSingleton().AddLogging(option => { + var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path: "serilog.json").Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("nlog.config"); + option.AddSerilog(logger); }); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index 00927bc..eed9d3e 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -28,11 +28,16 @@ namespace DoubleDeckerBus } public int Insert(T bus, int position) - { - // TODO проверка на _maxCount - if (position < 0 || position >= _maxCount || BusyPlaces == _maxCount) return -1; + { + if (BusyPlaces == _maxCount) { + throw new StorageOverflowException(BusyPlaces); + } + + if (position < 0 || position >= _maxCount) + { + throw new BusNotFoundException("Место указано неверно"); + } - // TODO проверка позиции BusyPlaces++; _places.Insert(position, bus); @@ -41,8 +46,10 @@ namespace DoubleDeckerBus public T Remove(int position) { - // TODO проверка позиции - if (position < 0 || position >= _maxCount) return null; + + if (position < 0 || position >= _maxCount) { + throw new BusNotFoundException(position); + } T savedBus = _places[position]; _places.RemoveAt(position); return savedBus; diff --git a/DoubleDeckerBus/DoubleDeckerBus/nlog.config b/DoubleDeckerBus/DoubleDeckerBus/nlog.config deleted file mode 100644 index 3e28e76..0000000 --- a/DoubleDeckerBus/DoubleDeckerBus/nlog.config +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/DoubleDeckerBus/DoubleDeckerBus/serilog.Designer.cs b/DoubleDeckerBus/DoubleDeckerBus/serilog.Designer.cs new file mode 100644 index 0000000..456cc8b --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/serilog.Designer.cs @@ -0,0 +1,26 @@ +//------------------------------------------------------------------------------ +// +// Этот код создан программой. +// Исполняемая версия:4.0.30319.42000 +// +// Изменения в этом файле могут привести к неправильной работе и будут потеряны в случае +// повторной генерации кода. +// +//------------------------------------------------------------------------------ + +namespace DoubleDeckerBus { + + + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.3.0.0")] + internal sealed partial class serilog : global::System.Configuration.ApplicationSettingsBase { + + private static serilog defaultInstance = ((serilog)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new serilog()))); + + public static serilog Default { + get { + return defaultInstance; + } + } + } +} diff --git a/DoubleDeckerBus/DoubleDeckerBus/serilog.json b/DoubleDeckerBus/DoubleDeckerBus/serilog.json new file mode 100644 index 0000000..4c3cf19 --- /dev/null +++ b/DoubleDeckerBus/DoubleDeckerBus/serilog.json @@ -0,0 +1,20 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log_.log", + "rollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "Properties": { + "Application": "DoubleDeckerBus" + } + } +} \ No newline at end of file -- 2.25.1 From a20bad83366743b131c3f67e576096730ac9eb2e Mon Sep 17 00:00:00 2001 From: shadowik Date: Sun, 11 Dec 2022 21:44:32 +0400 Subject: [PATCH 4/6] Final Commit --- .../DoubleDeckerBus/DrawingObjectBus.cs | 14 ++++++++++++-- .../DoubleDeckerBus/FormBusConfig.cs | 2 +- .../DoubleDeckerBus/FormMapWithSetBuses.cs | 18 +++++++++++------- .../DoubleDeckerBus/IDrawingObject.cs | 1 + .../DoubleDeckerBus/MapWithSetBusesGeneric.cs | 2 +- .../DoubleDeckerBus/SetBusesGeneric.cs | 14 ++++++++++++-- 6 files changed, 38 insertions(+), 13 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs index bd5e511..25c0020 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -8,13 +9,22 @@ namespace DoubleDeckerBus { internal class DrawingObjectBus : IDrawingObject { - private DrawingBus _bus = null; + public DrawingBus? _bus = null; public DrawingObjectBus(DrawingBus bus) { _bus = bus; } + public DrawingBus? Get_bus() + { + return _bus; + } + + public DrawingBus getBus(DrawingBus? _bus) { + return _bus; + } + public float Step => _bus?.Bus?.Step ?? 0; public (float Left, float Right, float Top, float Bottom) GetCurrentPosition() @@ -22,7 +32,7 @@ namespace DoubleDeckerBus return _bus?.GetCurrentPosition() ?? default; } - public string GetInfo() + public string? GetInfo() { return _bus?.GetDataForSave(); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormBusConfig.cs b/DoubleDeckerBus/DoubleDeckerBus/FormBusConfig.cs index 2c4b4d3..63346bc 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormBusConfig.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormBusConfig.cs @@ -143,7 +143,7 @@ namespace DoubleDeckerBus } private void ButtonAdd_Click(object sender, EventArgs e) - { + { EventAddBus?.Invoke(_bus); Close(); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs index 779e742..35a549d 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/FormMapWithSetBuses.cs @@ -60,26 +60,30 @@ namespace DoubleDeckerBus var formBusConfig = new FormBusConfig(); formBusConfig.AddEvent(AddBus); formBusConfig.Show(); - } private void AddBus(DrawingBus bus) - { + { if (listBoxMaps.SelectedIndex == -1) { return; } - DrawingObjectBus boat = new(bus); - if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + boat >= 0) + try { + DrawingObjectBus _bus = new(bus); + _ = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] + _bus; MessageBox.Show("Объект добавлен"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); _logger.LogInformation($"Объект добавлен"); } - else + catch (StorageOverflowException ex) { - MessageBox.Show("Не удалось добавить объект"); - _logger.LogInformation($"Объект не добаавлен"); + MessageBox.Show($"Не удалось добавить объект {ex.Message}"); + _logger.LogInformation($"Объект не добаавлен {ex.Message}"); + } + catch (Exception ex) { + MessageBox.Show($"Не удалось добавить объект {ex.Message}"); + _logger.LogInformation($"Объект не добаавлен {ex.Message}"); } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs b/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs index 93c20a0..2e091e9 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/IDrawingObject.cs @@ -8,6 +8,7 @@ namespace DoubleDeckerBus { internal interface IDrawingObject { + public DrawingBus getBus(); public float Step { get; } void SetObject(int x, int y, int width, int height); diff --git a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs index d5513cf..66aed8b 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/MapWithSetBusesGeneric.cs @@ -29,7 +29,7 @@ namespace DoubleDeckerBus } public static int operator +(MapWithSetBusesGeneric map, T bus) - { + { return map._setBuses.Insert(bus); } diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index eed9d3e..32625aa 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace DoubleDeckerBus { internal class SetBusesGeneric - where T : class + where T : IDrawingObject { private readonly List _places; @@ -29,6 +29,16 @@ namespace DoubleDeckerBus public int Insert(T bus, int position) { + try + { + if (bus.getBus(bus.Get_bus()) == null) + { + throw new NullReferenceException(); + } + } + catch (NullReferenceException) { + MessageBox.Show("null"); + } if (BusyPlaces == _maxCount) { throw new StorageOverflowException(BusyPlaces); } @@ -57,7 +67,7 @@ namespace DoubleDeckerBus public T this[int position] { get { - if (position < 0 || position >= _maxCount) return null; + if (position < 0 || position >= _maxCount) return default(T); return _places[position]; } set { -- 2.25.1 From 3e9def95973baed3aabacc339f6c478508999f9d Mon Sep 17 00:00:00 2001 From: shadowik Date: Sun, 11 Dec 2022 22:51:28 +0400 Subject: [PATCH 5/6] Fix --- DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs | 5 +++++ DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs index 25c0020..f881878 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/DrawingObjectBus.cs @@ -56,5 +56,10 @@ namespace DoubleDeckerBus { return new DrawingObjectBus(data.CreateDrawningCar()); } + + public DrawingBus getBus() + { + return _bus; + } } } diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index 32625aa..d934c26 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -31,7 +31,7 @@ namespace DoubleDeckerBus { try { - if (bus.getBus(bus.Get_bus()) == null) + if (bus.getBus() == null) { throw new NullReferenceException(); } -- 2.25.1 From 3de58e4df9ed97f2f93d41356a9067324a200a55 Mon Sep 17 00:00:00 2001 From: shadowik Date: Sun, 11 Dec 2022 22:58:41 +0400 Subject: [PATCH 6/6] Fix --- DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs index d934c26..a0a4123 100644 --- a/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs +++ b/DoubleDeckerBus/DoubleDeckerBus/SetBusesGeneric.cs @@ -29,20 +29,6 @@ namespace DoubleDeckerBus public int Insert(T bus, int position) { - try - { - if (bus.getBus() == null) - { - throw new NullReferenceException(); - } - } - catch (NullReferenceException) { - MessageBox.Show("null"); - } - if (BusyPlaces == _maxCount) { - throw new StorageOverflowException(BusyPlaces); - } - if (position < 0 || position >= _maxCount) { throw new BusNotFoundException("Место указано неверно"); -- 2.25.1