Лабораторная №7
This commit is contained in:
parent
28b59aba44
commit
8aa0b7ef16
@ -9,9 +9,21 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.7" />
|
||||
<PackageReference Include="Serilog" Version="3.1.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appSettings.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
@ -15,18 +15,20 @@ using System.Windows.Forms;
|
||||
using DoubleDeckerBus.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Xml.Linq;
|
||||
using Serilog;
|
||||
using Log = Serilog.Log;
|
||||
|
||||
|
||||
|
||||
namespace DoubleDeckerBus
|
||||
{
|
||||
public partial class FormBusCollection : Form
|
||||
{
|
||||
private readonly BusesGenericStorage _storage;
|
||||
private readonly ILogger _logger;
|
||||
public FormBusCollection(ILogger<FormBusCollection> logger)
|
||||
{
|
||||
InitializeComponent();
|
||||
_storage = new BusesGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height);
|
||||
_logger = logger;
|
||||
}
|
||||
private void ReloadObjects()
|
||||
{
|
||||
@ -60,7 +62,7 @@ namespace DoubleDeckerBus
|
||||
}
|
||||
_storage.AddSet(textBoxStorageName.Text);
|
||||
ReloadObjects();
|
||||
Log.Information($"Добавлен набор:{textBoxStorageName.Text}");
|
||||
_logger.LogInformation($"Добавлен набор:{textBoxStorageName.Text}");
|
||||
}
|
||||
private void ButtonDelObject_Click(object sender, EventArgs e)
|
||||
{
|
||||
@ -74,7 +76,7 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
_storage.DelSet(name);
|
||||
ReloadObjects();
|
||||
Log.Information($"Удален набор: {name}");
|
||||
_logger.LogInformation($"Удален набор: {name}");
|
||||
}
|
||||
}
|
||||
private void buttonAddBus_Click(object sender, EventArgs e)
|
||||
@ -97,12 +99,12 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
bool q = obj + m;
|
||||
MessageBox.Show("Объект добавлен");
|
||||
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
_logger.LogInformation($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
}
|
||||
catch (StorageOverflowException ex)
|
||||
{
|
||||
Log.Warning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||||
_logger.LogWarning($"Коллекция {listBoxStorages.SelectedItem.ToString() ?? string.Empty} переполнена");
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
});
|
||||
@ -129,23 +131,23 @@ namespace DoubleDeckerBus
|
||||
if (obj - pos != null)
|
||||
{
|
||||
MessageBox.Show("Объект удален");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
Log.Information($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
pictureBoxCollection.Image = obj.ShowBuses();
|
||||
_logger.LogInformation($"лодка удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
MessageBox.Show("Объект не удален");
|
||||
Log.Warning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogWarning($"лодка не удалена из набора {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
}
|
||||
catch (BusNotFoundException ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
Log.Warning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
_logger.LogWarning($"BoatNotFound: {ex.Message} in set {listBoxStorages.SelectedItem.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("Объект не добавлен"); Log.Warning("Not input");
|
||||
MessageBox.Show("Объект не добавлен"); _logger.LogWarning("Not input");
|
||||
}
|
||||
}
|
||||
private void ButtonRefreshCollection_Click(object sender, EventArgs e)
|
||||
@ -169,11 +171,11 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
_storage.SaveData(saveFileDialog.FileName);
|
||||
MessageBox.Show("Сохранение прошло успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
|
||||
_logger.LogInformation($"Файл {saveFileDialog.FileName} успешно сохранен");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("Не удалось сохранить");
|
||||
_logger.LogWarning("Не удалось сохранить");
|
||||
MessageBox.Show($"Не сохранилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
||||
}
|
||||
@ -188,7 +190,7 @@ namespace DoubleDeckerBus
|
||||
{
|
||||
_storage.LoadData(openFileDialog.FileName);
|
||||
MessageBox.Show("Загрузка прошла успешно","Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
Log.Information($"Файл {openFileDialog.FileName} успешно загружен");
|
||||
_logger.LogInformation($"Файл {openFileDialog.FileName} успешно загружен");
|
||||
foreach (var collection in _storage.Keys)
|
||||
{
|
||||
listBoxStorages.Items.Add(collection);
|
||||
@ -197,7 +199,7 @@ namespace DoubleDeckerBus
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Warning("Не удалось загрузить");
|
||||
_logger.LogWarning("Не удалось загрузить");
|
||||
MessageBox.Show($"Не загрузилось: {ex.Message}","Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
|
||||
}
|
||||
|
@ -2,6 +2,8 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Drawing;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NLog.Extensions.Logging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Serilog;
|
||||
|
||||
namespace DoubleDeckerBus
|
||||
{
|
||||
@ -10,22 +12,32 @@ namespace DoubleDeckerBus
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
// To customize application configuration such as set high DPI settings or default font,
|
||||
// see https://aka.ms/applicationconfiguration.
|
||||
ApplicationConfiguration.Initialize();
|
||||
var services = new ServiceCollection();
|
||||
ConfigureServices(services);
|
||||
using (ServiceProvider serviceProvider =
|
||||
services.BuildServiceProvider())
|
||||
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
|
||||
{
|
||||
Application.Run(serviceProvider.GetRequiredService<FormBusCollection>());
|
||||
}
|
||||
}
|
||||
private static void ConfigureServices(ServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<FormBusCollection>()
|
||||
.AddLogging(option =>
|
||||
services.AddSingleton<FormBusCollection>().AddLogging(option =>
|
||||
{
|
||||
string[] path = Directory.GetCurrentDirectory().Split('\\');
|
||||
string pathNeed = "";
|
||||
for (int i = 0; i < path.Length - 3; i++)
|
||||
{
|
||||
pathNeed += path[i] + "\\";
|
||||
}
|
||||
var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile(path: $"appSettings.json", optional: false, reloadOnChange: true).Build();
|
||||
|
||||
var logger = new LoggerConfiguration().ReadFrom.Configuration(configuration).CreateLogger();
|
||||
option.SetMinimumLevel(LogLevel.Information);
|
||||
option.AddNLog("nlog.config");
|
||||
option.AddSerilog(logger);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
20
DoubleDeckerBus/DoubleDeckerBus/appSettings.json
Normal file
20
DoubleDeckerBus/DoubleDeckerBus/appSettings.json
Normal file
@ -0,0 +1,20 @@
|
||||
{
|
||||
"Serilog": {
|
||||
"Using": [ "Serilog.Sinks.File" ],
|
||||
"MinimumLevel": "Information",
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "File",
|
||||
"Args": {
|
||||
"path": "Logs/log_.log",
|
||||
"rollingInterval": "Day",
|
||||
"outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
],
|
||||
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
|
||||
"Properties": {
|
||||
"Application": "DoubleDeckerBus"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user