Логирование.

This commit is contained in:
Nikolaeva_Y.A 2022-12-06 09:24:09 +04:00
parent dfbed5bdd5
commit f29cc31b04
4 changed files with 52 additions and 7 deletions

View File

@ -8,6 +8,13 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.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 Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -24,10 +25,14 @@ namespace Airbus
//объект от коллекции карт //объект от коллекции карт
private readonly MapsCollection _mapsCollection; private readonly MapsCollection _mapsCollection;
//логер
private readonly ILogger _logger;
//конструктор //конструктор
public FormMapWithSetPlanes() public FormMapWithSetPlanes(ILogger<FormMapWithSetPlanes> logger)
{ {
InitializeComponent(); InitializeComponent();
_logger = logger;
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height); _mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear(); comboBoxSelectorMap.Items.Clear();
foreach (var element in _mapsDict) foreach (var element in _mapsDict)
@ -77,6 +82,7 @@ namespace Airbus
_mapsCollection.AddMap(textBoxNewMapName.Text, _mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]); _mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps(); ReloadMaps();
_logger.LogInformation($"Добавлена карта {textBoxNewMapName.Text}");
} }
//Выбор карты //Выбор карты
@ -122,8 +128,6 @@ namespace Airbus
//добавление объекта //добавление объекта
private void ButtonAddPlane_Click(object sender, EventArgs e) private void ButtonAddPlane_Click(object sender, EventArgs e)
{ {
//ВСТАВИТЬ ПРОВЕРКУ НА ВОЗМОЖНОСТЬ ВСТАВКИ
var formPlaneConfig = new FormPlaneConfig(); var formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlane); formPlaneConfig.AddEvent(AddPlane);
@ -132,8 +136,6 @@ namespace Airbus
//удаление объекта //удаление объекта
private void ButtonRemovePlane_Click(object sender, EventArgs e) private void ButtonRemovePlane_Click(object sender, EventArgs e)
{ {
//ВСТАВИТЬ ПРОВЕРКУ НА ВОЗМОЖНОСТЬ УДАЛЕНИЯ
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text)) if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{ {
return; return;

View File

@ -1,3 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
namespace Airbus namespace Airbus
{ {
internal static class Program internal static class Program
@ -11,7 +15,24 @@ namespace Airbus
// 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.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormMapWithSetPlanes());
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormMapWithSetPlanes>());
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormMapWithSetPlanes>()
.AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
});
} }
} }
} }

15
Airbus/Airbus/nlog.config Normal file
View File

@ -0,0 +1,15 @@
<?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="planelog-${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile" />
</rules>
</nlog>
</configuration>