Add NLog logging

This commit is contained in:
ShabOl 2023-11-26 00:26:38 +04:00
parent 1db5725314
commit ce068c174b
4 changed files with 61 additions and 4 deletions

View File

@ -27,4 +27,15 @@
<Folder Include="MovementStrategy\" /> <Folder Include="MovementStrategy\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
</ItemGroup>
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,7 @@
using AirBomber.Exceptions; using AirBomber.Exceptions;
using AirBomber.Generics; using AirBomber.Generics;
using AirBomber.Rendering; using AirBomber.Rendering;
using Microsoft.Extensions.Logging;
namespace AirBomber namespace AirBomber
{ {
@ -8,11 +9,14 @@ namespace AirBomber
{ {
private readonly EntitiesGenericStorage _storage; private readonly EntitiesGenericStorage _storage;
public FormEntityCollection() private readonly ILogger _logger;
public FormEntityCollection(ILogger<FormEntityCollection> Logger)
{ {
InitializeComponent(); InitializeComponent();
_storage = new EntitiesGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height); _storage = new EntitiesGenericStorage(CollectionPictureBox.Width, CollectionPictureBox.Height);
_logger = Logger;
} }
private void ReloadObjects() private void ReloadObjects()
@ -112,6 +116,8 @@ namespace AirBomber
_storage.AddSet(SetNameTextBox.Text); _storage.AddSet(SetNameTextBox.Text);
ReloadObjects(); ReloadObjects();
_logger.LogInformation($"Добавлен набор: {SetNameTextBox.Text}");
} }
private void ButtonRemoveSet_Click(object sender, EventArgs e) private void ButtonRemoveSet_Click(object sender, EventArgs e)
@ -119,15 +125,19 @@ namespace AirBomber
if (StorageListBox.SelectedIndex == -1) if (StorageListBox.SelectedIndex == -1)
return; return;
string SetName = StorageListBox.SelectedItem.ToString() ?? string.Empty;
if (MessageBox.Show( if (MessageBox.Show(
$"Удалить объект{StorageListBox.SelectedItem}?", $"Удалить объект{SetName}?",
"Удаление", "Удаление",
MessageBoxButtons.YesNo, MessageBoxButtons.YesNo,
MessageBoxIcon.Question MessageBoxIcon.Question
) == DialogResult.Yes) ) == DialogResult.Yes)
{ {
_storage.RemoveSet(StorageListBox.SelectedItem.ToString() ?? string.Empty); _storage.RemoveSet(SetName);
ReloadObjects(); ReloadObjects();
_logger.LogInformation($"Удален набор: {SetName}");
} }
} }

View File

@ -1,3 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using System;
namespace AirBomber namespace AirBomber
{ {
internal static class Program internal static class Program
@ -6,7 +11,25 @@ namespace AirBomber
static void Main() static void Main()
{ {
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormEntityCollection());
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider ServiceProvider = services.BuildServiceProvider())
{
Application.Run(ServiceProvider.GetRequiredService<FormEntityCollection>());
}
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<FormEntityCollection>().AddLogging(
option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
}
);
} }
} }
} }

13
AirBomber/nlog.config Normal file
View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" internalLogLevel="Info">
<targets>
<target xsi:type="File" name="tofile" fileName="airbomberlog-${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile" />
</rules>
</nlog>
</configuration>