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