From ce2f998244eebb7bd92ecec750d9a478a6df1300 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Wed, 6 Dec 2023 09:33:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=BF=D0=BE=20=D1=82=D1=80=D0=B5=D0=B1=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Generics/LocomotivesGenericStorage.cs | 2 +- .../LogicFormLocomotiveCollection.cs | 13 +++++++++++ ElectricLocomotive/Program.cs | 23 +++++++++++++++---- .../ProjectElectricLocomotive.csproj | 8 +++++++ ElectricLocomotive/serilog.json | 20 ++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 ElectricLocomotive/serilog.json diff --git a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs index adfc6fa..4254b70 100644 --- a/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs +++ b/ElectricLocomotive/Generics/LocomotivesGenericStorage.cs @@ -133,7 +133,7 @@ namespace ProjectElectricLocomotive.Generics } if (data.Length == 0) { - throw new Exception("Невалиданя операция, нет данных длясохранения"); + throw new InvalidOperationException("Невалиданя операция, нет данных длясохранения"); } using FileStream fs = new(filename, FileMode.Create); byte[] info = new diff --git a/ElectricLocomotive/LogicFormLocomotiveCollection.cs b/ElectricLocomotive/LogicFormLocomotiveCollection.cs index 8285d0f..75ad021 100644 --- a/ElectricLocomotive/LogicFormLocomotiveCollection.cs +++ b/ElectricLocomotive/LogicFormLocomotiveCollection.cs @@ -2,6 +2,7 @@ using ProjectElectricLocomotive.DrawningObjects; using ProjectElectricLocomotive.Exceptions; using ProjectElectricLocomotive.Generics; +using System.Windows.Forms; using System.Xml.Linq; namespace ProjectElectricLocomotive @@ -133,15 +134,18 @@ namespace ProjectElectricLocomotive MessageBox.Show("Объект добавлен"); pictureBoxCollection.Image = obj.ShowLocomotives(); + _logger.LogInformation($"Объект {obj.GetType()} добавлен"); } else { MessageBox.Show("Не удалось добавить объект"); + _logger.LogInformation($"Не удалось добавить объект"); } } catch (StorageOverflowException ex) { MessageBox.Show(ex.Message); + _logger.LogError(ex.ToString()); } } @@ -182,15 +186,18 @@ namespace ProjectElectricLocomotive { MessageBox.Show("Объект удален"); pictureBoxCollection.Image = obj.ShowLocomotives(); + _logger.LogInformation($"Объект {pos} удален"); } else { MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation($"Не удалось удалить объект {pos}"); } } catch (LocomotiveNotFoundException ex) { MessageBox.Show(ex.Message); + _logger.LogError(ex.ToString()); } } /// @@ -228,11 +235,14 @@ namespace ProjectElectricLocomotive _storage.SaveData(saveFileDialog.FileName); MessageBox.Show("Сохранение прошло успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation($"Сохранение в {saveFileDialog.FileName} прошло успешно"); } catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation($"Не удалось сохранить в {saveFileDialog.FileName}: {ex.Message}"); + _logger.LogDebug(ex.ToString()); } } } @@ -250,11 +260,14 @@ namespace ProjectElectricLocomotive MessageBox.Show("Загрузка прошла успешно", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); ReloadObjects(); + _logger.LogInformation($"Загрузка из {openFileDialog.FileName} прошла успешно"); } catch(Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation($"Не удалось загрузить из {openFileDialog.FileName}: {ex.Message}"); + _logger.LogDebug(ex.ToString()); } } } diff --git a/ElectricLocomotive/Program.cs b/ElectricLocomotive/Program.cs index daae88b..a29f79a 100644 --- a/ElectricLocomotive/Program.cs +++ b/ElectricLocomotive/Program.cs @@ -1,6 +1,7 @@ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging; +using Serilog; namespace ProjectElectricLocomotive { @@ -12,8 +13,7 @@ namespace ProjectElectricLocomotive [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); @@ -21,14 +21,29 @@ namespace ProjectElectricLocomotive { Application.Run(serviceProvider.GetRequiredService()); } + } + private static void ConfigureServices(ServiceCollection services) { services.AddSingleton().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: $"{pathNeed}serilog.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); }); } + } } \ No newline at end of file diff --git a/ElectricLocomotive/ProjectElectricLocomotive.csproj b/ElectricLocomotive/ProjectElectricLocomotive.csproj index eb2b961..c8d4348 100644 --- a/ElectricLocomotive/ProjectElectricLocomotive.csproj +++ b/ElectricLocomotive/ProjectElectricLocomotive.csproj @@ -9,8 +9,16 @@ + + + + + + + + diff --git a/ElectricLocomotive/serilog.json b/ElectricLocomotive/serilog.json new file mode 100644 index 0000000..44c260c --- /dev/null +++ b/ElectricLocomotive/serilog.json @@ -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": "GasolineTanker" + } + } +} \ No newline at end of file