This commit is contained in:
GokaPek 2023-12-03 18:28:33 +04:00
parent 1ac62c3cc3
commit 348bede956
5 changed files with 23 additions and 13 deletions

View File

@ -12,8 +12,9 @@ using SelfPropelledArtilleryUnit.DrawningObjects;
using SelfPropelledArtilleryUnit.MovementStrategy;
using SelfPropelledArtilleryUnit.Generics;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using NLog;
using Serilog;
using System.Xml.Linq;
using ILogger = Serilog.ILogger;
namespace SelfPropelledArtilleryUnit
{
@ -22,7 +23,7 @@ namespace SelfPropelledArtilleryUnit
/// </summary>
public partial class FormSPAUCollection : Form
{
readonly int countPlaces = 11;
readonly int countPlaces = 12;
/// <summary>
/// Набор объектов
/// </summary>
@ -30,7 +31,7 @@ namespace SelfPropelledArtilleryUnit
/// <summary>
/// Логер
/// </summary>
private readonly Microsoft.Extensions.Logging.ILogger _logger;
private readonly ILogger<FormSPAUCollection> _logger;
/// <summary>
/// Конструктор
/// </summary>
@ -165,6 +166,7 @@ namespace SelfPropelledArtilleryUnit
}
catch (Exception ex)
{
MessageBox.Show("Не удалось добавить объект");
_logger.LogWarning($"Неудачная попытка добавления: {ex}");
}
@ -216,7 +218,7 @@ namespace SelfPropelledArtilleryUnit
else
{
MessageBox.Show("Не удалось удалить объект");
_logger.LogWarning("Неудачная попытка удаления");
_logger.LogWarning($"Неудачная попытка удаления c позиции {pos}");
}
}
catch(SPAUNotFoundException ex)

View File

@ -1,6 +1,9 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Serilog;
using System.Drawing;
using ILogger = Serilog.ILogger;
namespace SelfPropelledArtilleryUnit
{
internal static class Program
@ -14,8 +17,7 @@ namespace SelfPropelledArtilleryUnit
ApplicationConfiguration.Initialize();
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider serviceProvider =
services.BuildServiceProvider())
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormSPAUCollection>());
@ -23,13 +25,17 @@ namespace SelfPropelledArtilleryUnit
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormSPAUCollection>()
.AddLogging(option =>
string currentTime = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
string logFileName = $"logs/spaulog-{currentTime}.txt";
services.AddSingleton<FormSPAUCollection>().AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
option.AddSerilog(new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.File(logFileName)
.CreateLogger());
});
}
}
}

View File

@ -116,7 +116,7 @@ namespace SelfPropelledArtilleryUnit.Generics
}
if (data.Length == 0)
{
throw new Exception("Невалиданя операция, нет данных для сохранения");
throw new ReadFileExeption("Невалиданя операция, нет данных для сохранения");
}
using (StreamWriter writer = new StreamWriter(filename))
{

View File

@ -11,6 +11,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
</ItemGroup>
<ItemGroup>

View File

@ -62,7 +62,7 @@ namespace SelfPropelledArtilleryUnit.Generics
throw new SPAUNotFoundException("Вставка невоможна, объект не найден");
if (position < 0 || position > _maxCount)
throw new SPAUNotFoundException("Недоступная позиция");
throw new SPAUNotFoundException(position);
if (Count == 0)
@ -83,7 +83,7 @@ namespace SelfPropelledArtilleryUnit.Generics
public bool Remove(int position)
{
if (_places[position] == null)
throw new SPAUNotFoundException("Невалиданя операция, нет данных для сохранения");
throw new SPAUNotFoundException(position);
_places.RemoveAt(position);
return true;