diff --git a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs index 1e15a30..19b6790 100644 --- a/Locomotive/Locomotive/FormMapWithSetLocomotives.cs +++ b/Locomotive/Locomotive/FormMapWithSetLocomotives.cs @@ -1,4 +1,6 @@ -using System; +using Serilog; +using Serilog.Formatting.Compact; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -21,6 +23,8 @@ namespace Locomotive }; /// Объект от коллекции карт private readonly MapsCollection _mapsCollection; + + /// Конструктор public FormMapWithSetLocomotives() { @@ -64,12 +68,14 @@ namespace Locomotive return; } _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); + Log.Information($"Map {textBoxNewMapName.Text} added"); ReloadMaps(); } /// Выбор карты private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) { pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + Log.Information($"Map switched to {listBoxMaps.SelectedItem?.ToString() ?? string.Empty}"); } /// Удаление карты private void buttonDeleteMap_Click(object sender, EventArgs e) @@ -81,6 +87,7 @@ namespace Locomotive if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?","Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); + Log.Information($"Map {listBoxMaps.SelectedItem?.ToString() ?? string.Empty} deleted"); ReloadMaps(); } } @@ -105,6 +112,7 @@ namespace Locomotive { MessageBox.Show("Object added"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); + Log.Information($"Locomotive {locomotive} added"); } else @@ -144,6 +152,7 @@ namespace Locomotive if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) { MessageBox.Show("Object removed"); + Log.Information($"Locomotive at {pos} deleted"); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } else @@ -217,6 +226,7 @@ namespace Locomotive { _mapsCollection.SaveData(saveFileDialog.FileName); MessageBox.Show("Saving success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Saved to {saveFileDialog.FileName}"); } catch (Exception ex) { @@ -234,6 +244,7 @@ namespace Locomotive { _mapsCollection.LoadData(loadFileDialog.FileName); MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); + Log.Information($"Loaded from {loadFileDialog.FileName}"); ReloadMaps(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); } diff --git a/Locomotive/Locomotive/Locomotive.csproj b/Locomotive/Locomotive/Locomotive.csproj index 13ee123..4a08ebd 100644 --- a/Locomotive/Locomotive/Locomotive.csproj +++ b/Locomotive/Locomotive/Locomotive.csproj @@ -8,6 +8,13 @@ enable + + + + + + + True diff --git a/Locomotive/Locomotive/MapsCollection.cs b/Locomotive/Locomotive/MapsCollection.cs index 67427df..01faa30 100644 --- a/Locomotive/Locomotive/MapsCollection.cs +++ b/Locomotive/Locomotive/MapsCollection.cs @@ -1,4 +1,5 @@ -using System; +using Serilog; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -78,6 +79,7 @@ namespace Locomotive { if (!File.Exists(filename)) { + Log.Warning($"FileNotFoundException {filename}"); throw new FileNotFoundException("Файл не найден"); } using (FileStream fs = new(filename, FileMode.Open)) @@ -87,6 +89,7 @@ namespace Locomotive if (!curLine.Contains("MapsCollection")) { + Log.Warning($"FileFormatException"); throw new FileFormatException("Формат данных в файле неправильный"); } diff --git a/Locomotive/Locomotive/Program.cs b/Locomotive/Locomotive/Program.cs index ceb3e6a..50c16fd 100644 --- a/Locomotive/Locomotive/Program.cs +++ b/Locomotive/Locomotive/Program.cs @@ -1,3 +1,5 @@ +using Serilog; + namespace Locomotive { internal static class Program @@ -10,6 +12,9 @@ namespace Locomotive { // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. + + + Log.Logger = new LoggerConfiguration().WriteTo.File(new Serilog.Formatting.Compact.CompactJsonFormatter(), "C:\\secondCourse\\OOP\\log.clef").CreateLogger(); ApplicationConfiguration.Initialize(); Application.Run(new FormMapWithSetLocomotives()); } diff --git a/Locomotive/Locomotive/SetLocomotivesGeneric.cs b/Locomotive/Locomotive/SetLocomotivesGeneric.cs index 53f8b6a..2b6612a 100644 --- a/Locomotive/Locomotive/SetLocomotivesGeneric.cs +++ b/Locomotive/Locomotive/SetLocomotivesGeneric.cs @@ -1,4 +1,5 @@ -using System; +using Serilog; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -31,7 +32,10 @@ namespace Locomotive public int Insert(T locomotive, int position) { if (position < 0) return -1; - if (Count >= _maxCount) throw new StorageOverflowException(_maxCount); + if (Count >= _maxCount) { + Log.Warning("StorageOverflowException"); + throw new StorageOverflowException(_maxCount); + } _places.Insert(position, locomotive); return position; @@ -40,7 +44,11 @@ namespace Locomotive public T Remove(int position) { if (position >= _maxCount || position < 0) return null; - if (_places[position] is null) throw new LocomotiveNotFoundException(position); + if (_places[position] is null) + { + Log.Warning($"LocomotiveNotFoundException at {position}"); + throw new LocomotiveNotFoundException(position); + } T result = _places[position]; _places[position] = null; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! РАНЬШЕ REMOVE return result;