Eliseev E.E. LabWork07 #8

Closed
ElEgEv wants to merge 10 commits from LabWork07 into LabWork06
4 changed files with 63 additions and 8 deletions
Showing only changes of commit c846489b7c - Show all commits

View File

@ -8,6 +8,16 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<None Remove="nlog.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
@ -28,4 +38,12 @@
<Folder Include="делите\" />
</ItemGroup>
<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="NLog.Extensions.Logging" Version="5.1.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -24,10 +25,14 @@ namespace Airbus
//объект от коллекции карт
private readonly MapsCollection _mapsCollection;
//логер
private readonly ILogger _logger;
//конструктор
public FormMapWithSetPlanes()
public FormMapWithSetPlanes(ILogger<FormMapWithSetPlanes> logger)
{
InitializeComponent();
_logger = logger;
_mapsCollection = new MapsCollection(pictureBox.Width, pictureBox.Height);
comboBoxSelectorMap.Items.Clear();
foreach (var element in _mapsDict)
@ -78,6 +83,7 @@ namespace Airbus
_mapsCollection.AddMap(textBoxNewMapName.Text,
_mapsDict[comboBoxSelectorMap.Text]);
ReloadMaps();
_logger.LogInformation($"Добавлена карта {textBoxNewMapName.Text}");
}
//Выбор карты
@ -117,14 +123,11 @@ namespace Airbus
{
MessageBox.Show("Не удалось добавить объект");
}
}
//добавление объекта
private void ButtonAddPlane_Click(object sender, EventArgs e)
{
//ВСТАВИТЬ ПРОВЕРКУ НА ВОЗМОЖНОСТЬ ВСТАВКИ
var formPlaneConfig = new FormPlaneConfig();
formPlaneConfig.AddEvent(AddPlane);
@ -134,8 +137,6 @@ namespace Airbus
//удаление объекта
private void ButtonRemovePlane_Click(object sender, EventArgs e)
{
//ВСТАВИТЬ ПРОВЕРКУ НА ВОЗМОЖНОСТЬ УДАЛЕНИЯ
if (string.IsNullOrEmpty(maskedTextBoxPosition.Text))
{
return;

View File

@ -1,3 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
namespace Airbus
{
internal static class Program
@ -11,7 +15,24 @@ namespace Airbus
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
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");
});
Review

Настройку логера следует выносить в отдельный конфигурационный файл, чтобы ее можно было менять без пересборки проекта

Настройку логера следует выносить в отдельный конфигурационный файл, чтобы ее можно было менять без пересборки проекта
}
}
}

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>