Добавлен логгер, осталось почистить код и убедиться в работоспособности

This commit is contained in:
Данила Мочалов 2022-11-08 01:12:30 +04:00
parent 7009f2b1b7
commit 47dfc705d8
5 changed files with 39 additions and 5 deletions

View File

@ -1,4 +1,6 @@
using System; using Serilog;
using Serilog.Formatting.Compact;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -21,6 +23,8 @@ namespace Locomotive
}; };
/// Объект от коллекции карт /// Объект от коллекции карт
private readonly MapsCollection _mapsCollection; private readonly MapsCollection _mapsCollection;
/// Конструктор /// Конструктор
public FormMapWithSetLocomotives() public FormMapWithSetLocomotives()
{ {
@ -64,12 +68,14 @@ namespace Locomotive
return; return;
} }
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]); _mapsCollection.AddMap(textBoxNewMapName.Text, _mapsDict[comboBoxSelectorMap.Text]);
Log.Information($"Map {textBoxNewMapName.Text} added");
ReloadMaps(); ReloadMaps();
} }
/// Выбор карты /// Выбор карты
private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e) private void listBoxMaps_SelectedIndexChanged(object sender, EventArgs e)
{ {
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); 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) 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) if (MessageBox.Show($"Delete map {listBoxMaps.SelectedItem}?","Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
_mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty); _mapsCollection.DelMap(listBoxMaps.SelectedItem?.ToString() ?? string.Empty);
Log.Information($"Map {listBoxMaps.SelectedItem?.ToString() ?? string.Empty} deleted");
ReloadMaps(); ReloadMaps();
} }
} }
@ -105,6 +112,7 @@ namespace Locomotive
{ {
MessageBox.Show("Object added"); MessageBox.Show("Object added");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
Log.Information($"Locomotive {locomotive} added");
} }
else else
@ -144,6 +152,7 @@ namespace Locomotive
if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null) if (_mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos != null)
{ {
MessageBox.Show("Object removed"); MessageBox.Show("Object removed");
Log.Information($"Locomotive at {pos} deleted");
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }
else else
@ -217,6 +226,7 @@ namespace Locomotive
{ {
_mapsCollection.SaveData(saveFileDialog.FileName); _mapsCollection.SaveData(saveFileDialog.FileName);
MessageBox.Show("Saving success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Saving success", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Saved to {saveFileDialog.FileName}");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -234,6 +244,7 @@ namespace Locomotive
{ {
_mapsCollection.LoadData(loadFileDialog.FileName); _mapsCollection.LoadData(loadFileDialog.FileName);
MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("Loaded successfully", "Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
Log.Information($"Loaded from {loadFileDialog.FileName}");
ReloadMaps(); ReloadMaps();
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet(); pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
} }

View File

@ -8,6 +8,13 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="2.12.0" />
<PackageReference Include="Serilog.Formatting.Compact" Version="1.1.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>

View File

@ -1,4 +1,5 @@
using System; using Serilog;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -78,6 +79,7 @@ namespace Locomotive
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
Log.Warning($"FileNotFoundException {filename}");
throw new FileNotFoundException("Файл не найден"); throw new FileNotFoundException("Файл не найден");
} }
using (FileStream fs = new(filename, FileMode.Open)) using (FileStream fs = new(filename, FileMode.Open))
@ -87,6 +89,7 @@ namespace Locomotive
if (!curLine.Contains("MapsCollection")) if (!curLine.Contains("MapsCollection"))
{ {
Log.Warning($"FileFormatException");
throw new FileFormatException("Формат данных в файле неправильный"); throw new FileFormatException("Формат данных в файле неправильный");
} }

View File

@ -1,3 +1,5 @@
using Serilog;
namespace Locomotive namespace Locomotive
{ {
internal static class Program internal static class Program
@ -10,6 +12,9 @@ namespace Locomotive
{ {
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
Log.Logger = new LoggerConfiguration().WriteTo.File(new Serilog.Formatting.Compact.CompactJsonFormatter(), "C:\\secondCourse\\OOP\\log.clef").CreateLogger();
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormMapWithSetLocomotives()); Application.Run(new FormMapWithSetLocomotives());
} }

View File

@ -1,4 +1,5 @@
using System; using Serilog;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -31,7 +32,10 @@ namespace Locomotive
public int Insert(T locomotive, int position) public int Insert(T locomotive, int position)
{ {
if (position < 0) return -1; 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); _places.Insert(position, locomotive);
return position; return position;
@ -40,7 +44,11 @@ namespace Locomotive
public T Remove(int position) public T Remove(int position)
{ {
if (position >= _maxCount || position < 0) return null; 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]; T result = _places[position];
_places[position] = null; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! РАНЬШЕ REMOVE _places[position] = null; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! РАНЬШЕ REMOVE
return result; return result;