From 7ac785ccfd3b55745e3272b972ac59c4afc432d0 Mon Sep 17 00:00:00 2001 From: gettterot Date: Wed, 15 May 2024 09:37:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=B0=D0=BD=20Logger,=20=D0=B0=20=D1=82=D0=B0=D0=BA=D0=B6?= =?UTF-8?q?=D0=B5=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=20=D1=81=20=D1=83?= =?UTF-8?q?=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=D0=BC=20=D0=BB=D0=B0?= =?UTF-8?q?=D0=B9=D0=BD=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ListGenericObjects.cs | 7 +- .../ProjectLiner/Entities/EntityLiner.cs | 1 - .../FormLinerCollection.Designer.cs | 8 +- .../ProjectLiner/FormLinerCollection.cs | 80 +++++++++++++------ ProjectLiner/ProjectLiner/Program.cs | 36 +++++---- ProjectLiner/ProjectLiner/ProjectLiner.csproj | 6 +- ProjectLiner/ProjectLiner/serilog.config | 13 --- ProjectLiner/ProjectLiner/serilog.json | 19 +++++ 8 files changed, 111 insertions(+), 59 deletions(-) delete mode 100644 ProjectLiner/ProjectLiner/serilog.config create mode 100644 ProjectLiner/ProjectLiner/serilog.json diff --git a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs index c70f706..e1dc795 100644 --- a/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs +++ b/ProjectLiner/ProjectLiner/CollectionGenericObjects/ListGenericObjects.cs @@ -45,8 +45,11 @@ public class ListGenericObjects : ICollectionGenericObjects } public T? Get(int position) { - if (position >= Count || position < 0) throw new PositionOutOfCollectionException(); - if (_collection[position] == null) throw new ObjectNotFoundException(); + // Проверка позиции + if (position >= Count || position < 0) + { + throw new PositionOutOfCollectionException(position); + } return _collection[position]; } public int Insert(T obj) diff --git a/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs index 0b44be7..cb1a862 100644 --- a/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs +++ b/ProjectLiner/ProjectLiner/Entities/EntityLiner.cs @@ -77,5 +77,4 @@ public class EntityLiner: EntityCommonLiner Color.FromName(strs[3]), Color.FromName(strs[4]), Convert.ToBoolean(strs[5]), Convert.ToBoolean(strs[6]), Convert.ToBoolean(strs[7])); } - } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/FormLinerCollection.Designer.cs b/ProjectLiner/ProjectLiner/FormLinerCollection.Designer.cs index ad3a8cc..15c3bdb 100644 --- a/ProjectLiner/ProjectLiner/FormLinerCollection.Designer.cs +++ b/ProjectLiner/ProjectLiner/FormLinerCollection.Designer.cs @@ -75,7 +75,7 @@ // buttonCreateCompany // buttonCreateCompany.Font = new Font("Segoe UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point); - buttonCreateCompany.Location = new Point(8, 520); + buttonCreateCompany.Location = new Point(7, 495); buttonCreateCompany.Margin = new Padding(5); buttonCreateCompany.Name = "buttonCreateCompany"; buttonCreateCompany.Size = new Size(257, 38); @@ -96,13 +96,13 @@ panelStorage.Dock = DockStyle.Top; panelStorage.Location = new Point(3, 43); panelStorage.Name = "panelStorage"; - panelStorage.Size = new Size(266, 405); + panelStorage.Size = new Size(266, 370); panelStorage.TabIndex = 7; // // buttonCollectionDel // buttonCollectionDel.Font = new Font("Segoe UI", 9.75F, FontStyle.Regular, GraphicsUnit.Point); - buttonCollectionDel.Location = new Point(5, 348); + buttonCollectionDel.Location = new Point(3, 314); buttonCollectionDel.Margin = new Padding(5); buttonCollectionDel.Name = "buttonCollectionDel"; buttonCollectionDel.Size = new Size(257, 38); @@ -180,7 +180,7 @@ comboBoxSelectorCompany.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxSelectorCompany.FormattingEnabled = true; comboBoxSelectorCompany.Items.AddRange(new object[] { "Хранилище" }); - comboBoxSelectorCompany.Location = new Point(20, 458); + comboBoxSelectorCompany.Location = new Point(23, 419); comboBoxSelectorCompany.Name = "comboBoxSelectorCompany"; comboBoxSelectorCompany.Size = new Size(242, 49); comboBoxSelectorCompany.TabIndex = 0; diff --git a/ProjectLiner/ProjectLiner/FormLinerCollection.cs b/ProjectLiner/ProjectLiner/FormLinerCollection.cs index 6d5b9a5..7446d28 100644 --- a/ProjectLiner/ProjectLiner/FormLinerCollection.cs +++ b/ProjectLiner/ProjectLiner/FormLinerCollection.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Logging; using ProjectLiner.CollectionGenericObjects; using ProjectLiner.Drawnings; +using ProjectLiner.Exceptions; using System.Windows.Forms; namespace ProjectLiner; @@ -58,19 +59,23 @@ public partial class FormLinerCollection : Form /// private void SetLiner(DrawningCommonLiner liner) { - if (_company == null || liner == null) + try { - return; + if (_company == null || liner == null) + { + return; + } + if (_company + liner != -1) + { + MessageBox.Show("Объект добавлен"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Добавлен объект: " + liner.GetDataForSave()); + } } - - if (_company + liner != -1) + catch (CollectionOverflowException ex) { - MessageBox.Show("Объект добавлен"); - pictureBox.Image = _company.Show(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); + MessageBox.Show(ex.Message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } @@ -81,26 +86,43 @@ public partial class FormLinerCollection : Form /// private void ButtonRemoveLiner_Click(object sender, EventArgs e) { - if (string.IsNullOrEmpty(maskedTextBoxPosition?.Text) || _company == null) + if (string.IsNullOrEmpty(maskedTextBox1.Text) || _company == null) { return; } + if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) { return; } - int pos = Convert.ToInt32(maskedTextBoxPosition.Text); - if (_company - pos != null) - { - MessageBox.Show("Объект удален"); - pictureBox.Image = _company.Show(); - } - else - { - MessageBox.Show("Не удалось удалить объект"); - } + try + { + int pos = Convert.ToInt32(maskedTextBox1.Text); + if (_company - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBox.Image = _company.Show(); + _logger.LogInformation("Удаление объекта по индексу " + pos); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + _logger.LogInformation("Не удалось удалить объект из коллекции по индексу " + pos); + } + } + catch (ObjectNotFoundException ex) + { + MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); + } + catch (PositionOutOfCollectionException ex) + { + MessageBox.Show(ex.Message); + _logger.LogError("Ошибка: {Message}", ex.Message); + } } + /// /// Передача объекта в другую форму /// @@ -118,14 +140,19 @@ public partial class FormLinerCollection : Form { liner = _company.GetRandomObject(); counter--; - if (counter <= 0) break; + if (counter <= 0) + { + break; + } } if (liner == null) { return; } - FormLiner form = new FormLiner(); - form.SetLiner = liner; + FormLiner form = new() + { + SetLiner = liner + }; form.ShowDialog(); } /// @@ -153,6 +180,7 @@ public partial class FormLinerCollection : Form if (string.IsNullOrEmpty(textBoxCollectionName.Text) || (!radioButtonList.Checked && !radioButtonMassive.Checked)) { MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogInformation("Не удалось добавить коллекцию: не все данные заполнены"); return; } CollectionType collectionType = CollectionType.None; @@ -166,6 +194,7 @@ public partial class FormLinerCollection : Form } _storageCollection.AddCollection(textBoxCollectionName.Text, collectionType); RerfreshListBoxItems(); + _logger.LogInformation("Добавлена коллекция типа " + collectionType + " с названием " + textBoxCollectionName.Text); } /// /// Удаление коллекции @@ -184,6 +213,7 @@ public partial class FormLinerCollection : Form return; } _storageCollection.DelCollection(listBoxCollection.SelectedItem.ToString()); + _logger.LogInformation("Удаление коллекции с названием {name}", listBoxCollection.SelectedItem.ToString()); RerfreshListBoxItems(); } /// @@ -271,10 +301,12 @@ public partial class FormLinerCollection : Form listBoxCollection.Items.Add(collection); } RerfreshListBoxItems(); + _logger.LogInformation("Загрузка из файла: {filename}", openFileDialog.FileName); } catch (Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogError("Ошибка: {Message}", ex.Message); } } } diff --git a/ProjectLiner/ProjectLiner/Program.cs b/ProjectLiner/ProjectLiner/Program.cs index 8076eac..68aced0 100644 --- a/ProjectLiner/ProjectLiner/Program.cs +++ b/ProjectLiner/ProjectLiner/Program.cs @@ -1,7 +1,7 @@ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; -using NLog.Extensions.Logging; -using System; +using Serilog; namespace ProjectLiner { @@ -15,28 +15,36 @@ namespace ProjectLiner { // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. - ApplicationConfiguration.Initialize(); + ApplicationConfiguration.Initialize(); ServiceCollection services = new(); - ConfigureService(services); + ConfigureServices(services); using ServiceProvider serviceProvider = services.BuildServiceProvider(); Application.Run(serviceProvider.GetRequiredService()); } /// - /// DI + /// Êîíôèãóðàöèÿ ñåðâèñà DI /// /// - private static void ConfigureService(ServiceCollection services) + private static void ConfigureServices(ServiceCollection services) { - services - .AddSingleton() - .AddLogging(option => { - option.SetMinimumLevel(LogLevel.Information); - option.AddNLog("serilog.config"); - }); - - + string[] path = Directory.GetCurrentDirectory().Split('\\'); + string pathNeed = ""; + for (int i = 0; i < path.Length - 3; i++) + { + pathNeed += path[i] + "\\"; + } + services.AddSingleton() + .AddLogging(option => + { + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .AddJsonFile($"{pathNeed}serilog.json") + .Build()) + .CreateLogger()); + }); } } } \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/ProjectLiner.csproj b/ProjectLiner/ProjectLiner/ProjectLiner.csproj index bf72d90..5e663e8 100644 --- a/ProjectLiner/ProjectLiner/ProjectLiner.csproj +++ b/ProjectLiner/ProjectLiner/ProjectLiner.csproj @@ -11,6 +11,10 @@ + + + + @@ -29,7 +33,7 @@ - + Always diff --git a/ProjectLiner/ProjectLiner/serilog.config b/ProjectLiner/ProjectLiner/serilog.config deleted file mode 100644 index 54e4ba6..0000000 --- a/ProjectLiner/ProjectLiner/serilog.config +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/ProjectLiner/ProjectLiner/serilog.json b/ProjectLiner/ProjectLiner/serilog.json new file mode 100644 index 0000000..21a527e --- /dev/null +++ b/ProjectLiner/ProjectLiner/serilog.json @@ -0,0 +1,19 @@ +{ + "Serilog": { + "Using": [ + "Serilog.Sinks.File" + ], + "MinimumLevel": "Debug", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log.log" + } + } + ], + "Properties": { + "Application": "Sample" + } + } +} \ No newline at end of file