diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs index ae11658..e5d96b9 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.Designer.cs @@ -59,7 +59,7 @@ // maskedTextBoxNumber // maskedTextBoxNumber.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; - maskedTextBoxNumber.Location = new Point(38, 532); + maskedTextBoxNumber.Location = new Point(38, 342); maskedTextBoxNumber.Mask = "0"; maskedTextBoxNumber.Name = "maskedTextBoxNumber"; maskedTextBoxNumber.Size = new Size(156, 27); @@ -69,9 +69,9 @@ // ButtonRefreshCollection.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; ButtonRefreshCollection.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); - ButtonRefreshCollection.Location = new Point(45, 663); + ButtonRefreshCollection.Location = new Point(38, 412); ButtonRefreshCollection.Name = "ButtonRefreshCollection"; - ButtonRefreshCollection.Size = new Size(150, 41); + ButtonRefreshCollection.Size = new Size(150, 29); ButtonRefreshCollection.TabIndex = 2; ButtonRefreshCollection.Text = "Обновить все"; ButtonRefreshCollection.UseVisualStyleBackColor = true; @@ -80,9 +80,9 @@ // ButtonRemoveLocomotive.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; ButtonRemoveLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); - ButtonRemoveLocomotive.Location = new Point(45, 590); + ButtonRemoveLocomotive.Location = new Point(37, 375); ButtonRemoveLocomotive.Name = "ButtonRemoveLocomotive"; - ButtonRemoveLocomotive.Size = new Size(150, 44); + ButtonRemoveLocomotive.Size = new Size(150, 31); ButtonRemoveLocomotive.TabIndex = 1; ButtonRemoveLocomotive.Text = "Удалить локо"; ButtonRemoveLocomotive.UseVisualStyleBackColor = true; @@ -92,9 +92,9 @@ // ButtonAddLocomotive.Anchor = AnchorStyles.Top; ButtonAddLocomotive.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); - ButtonAddLocomotive.Location = new Point(42, 461); + ButtonAddLocomotive.Location = new Point(38, 306); ButtonAddLocomotive.Name = "ButtonAddLocomotive"; - ButtonAddLocomotive.Size = new Size(150, 39); + ButtonAddLocomotive.Size = new Size(150, 30); ButtonAddLocomotive.TabIndex = 0; ButtonAddLocomotive.Text = "Добавить локо"; ButtonAddLocomotive.UseVisualStyleBackColor = true; @@ -103,15 +103,15 @@ // pictureBoxCollections // pictureBoxCollections.Anchor = AnchorStyles.Left; - pictureBoxCollections.Location = new Point(-1, 27); + pictureBoxCollections.Location = new Point(-1, 31); pictureBoxCollections.Name = "pictureBoxCollections"; - pictureBoxCollections.Size = new Size(1104, 692); + pictureBoxCollections.Size = new Size(303, 409); pictureBoxCollections.TabIndex = 1; pictureBoxCollections.TabStop = false; // // textBoxStorageName // - textBoxStorageName.Location = new Point(31, 43); + textBoxStorageName.Location = new Point(31, 28); textBoxStorageName.Margin = new Padding(3, 4, 3, 4); textBoxStorageName.Name = "textBoxStorageName"; textBoxStorageName.Size = new Size(149, 27); @@ -119,6 +119,7 @@ // // groupBox1 // + groupBox1.Anchor = AnchorStyles.None; groupBox1.Controls.Add(listBoxStorage); groupBox1.Controls.Add(ButtonAddObject); groupBox1.Controls.Add(ButtonRemoveObject); @@ -127,7 +128,7 @@ groupBox1.Margin = new Padding(3, 4, 3, 4); groupBox1.Name = "groupBox1"; groupBox1.Padding = new Padding(3, 4, 3, 4); - groupBox1.Size = new Size(216, 395); + groupBox1.Size = new Size(216, 270); groupBox1.TabIndex = 5; groupBox1.TabStop = false; groupBox1.Text = "Наборы"; @@ -136,7 +137,7 @@ // listBoxStorage.FormattingEnabled = true; listBoxStorage.ItemHeight = 20; - listBoxStorage.Location = new Point(31, 163); + listBoxStorage.Location = new Point(31, 97); listBoxStorage.Margin = new Padding(3, 4, 3, 4); listBoxStorage.Name = "listBoxStorage"; listBoxStorage.Size = new Size(149, 124); @@ -147,9 +148,9 @@ // ButtonAddObject.Anchor = AnchorStyles.Top; ButtonAddObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); - ButtonAddObject.Location = new Point(31, 96); + ButtonAddObject.Location = new Point(31, 62); ButtonAddObject.Name = "ButtonAddObject"; - ButtonAddObject.Size = new Size(150, 39); + ButtonAddObject.Size = new Size(149, 28); ButtonAddObject.TabIndex = 7; ButtonAddObject.Text = "Добавить набор"; ButtonAddObject.UseVisualStyleBackColor = true; @@ -159,9 +160,9 @@ // ButtonRemoveObject.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; ButtonRemoveObject.Font = new Font("Candara Light", 9F, FontStyle.Regular, GraphicsUnit.Point); - ButtonRemoveObject.Location = new Point(31, 325); + ButtonRemoveObject.Location = new Point(31, 228); ButtonRemoveObject.Name = "ButtonRemoveObject"; - ButtonRemoveObject.Size = new Size(150, 44); + ButtonRemoveObject.Size = new Size(149, 31); ButtonRemoveObject.TabIndex = 8; ButtonRemoveObject.Text = "Удалить набор"; ButtonRemoveObject.UseVisualStyleBackColor = true; @@ -175,11 +176,11 @@ Instruments.Controls.Add(maskedTextBoxNumber); Instruments.Controls.Add(ButtonAddLocomotive); Instruments.Controls.Add(ButtonRemoveLocomotive); - Instruments.Location = new Point(1102, 0); + Instruments.Location = new Point(302, -1); Instruments.Margin = new Padding(3, 4, 3, 4); Instruments.Name = "Instruments"; Instruments.Padding = new Padding(3, 4, 3, 4); - Instruments.Size = new Size(236, 719); + Instruments.Size = new Size(236, 448); Instruments.TabIndex = 6; Instruments.TabStop = false; Instruments.Text = "Инструменты"; @@ -190,7 +191,7 @@ menuStrip1.Items.AddRange(new ToolStripItem[] { файлToolStripMenuItem }); menuStrip1.Location = new Point(0, 0); menuStrip1.Name = "menuStrip1"; - menuStrip1.Size = new Size(1338, 28); + menuStrip1.Size = new Size(538, 28); menuStrip1.TabIndex = 7; menuStrip1.Text = "menuStrip1"; // @@ -228,7 +229,7 @@ // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1338, 724); + ClientSize = new Size(538, 443); Controls.Add(Instruments); Controls.Add(pictureBoxCollections); Controls.Add(menuStrip1); diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs index 587b075..87187f5 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.cs @@ -58,10 +58,13 @@ namespace ProjectElectricLocomotive if (string.IsNullOrEmpty(textBoxStorageName.Text)) { MessageBox.Show("Не всё заполнено", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + _logger.LogWarning("Неудачная попытка. Коллекция не добавлена, не все данные заполнены"); return; } _storage.AddSet(textBoxStorageName.Text); ReloadObjects(); + + _logger.LogInformation($"Добавлен набор: {textBoxStorageName.Text}"); } private void listBoxStorage_SelectedIndexChanged(object sender, EventArgs e) @@ -75,12 +78,15 @@ namespace ProjectElectricLocomotive { return; } + string name = listBoxStorage.SelectedItem.ToString() ?? string.Empty; if (MessageBox.Show($"Удалить объект {listBoxStorage.SelectedItem}?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { - _storage.DelSet(listBoxStorage.SelectedItem.ToString() ?? string.Empty); + _storage.DelSet(name); ReloadObjects(); + _logger.LogInformation($"Набор '{name}' удален"); } + _logger.LogWarning("Отмена удаления набора"); } private void ButtonAddLocomotive_Click(object sender, EventArgs e) @@ -102,17 +108,25 @@ namespace ProjectElectricLocomotive { return; } - + try + { + if (obj + loco > -1) + { + MessageBox.Show("Объект добавлен"); + pictureBoxCollections.Image = obj.ShowLocomotives(); + _logger.LogInformation($"Добавлен объект {obj}"); +; } + else + { + MessageBox.Show("Не удалось добавить объект"); + } + } + catch (StorageOverflowException ex) + { + MessageBox.Show(ex.Message); + } //проверяем, удалось ли нам загрузить объект - if (obj + loco > -1) - { - MessageBox.Show("Объект добавлен"); - pictureBoxCollections.Image = obj.ShowLocomotives(); - } - else - { - MessageBox.Show("Не удалось добавить объект"); - } + } private void ButtonRemoveLocomotive_Click(object sender, EventArgs e) @@ -126,14 +140,17 @@ namespace ProjectElectricLocomotive if (MessageBox.Show("Удалить объект?", "Удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { + _logger.LogWarning("Отмена удаления объекта"); return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); try { - if (obj - pos != null) + var removeObj = obj - pos; + if (removeObj != null) { MessageBox.Show("Объект удален"); + _logger.LogInformation($"Удален объект с позиции{pos}"); pictureBoxCollections.Image = obj.ShowLocomotives(); } else @@ -169,11 +186,12 @@ namespace ProjectElectricLocomotive try { _storage.SaveData(saveFileDialog.FileName); - + _logger.LogInformation($"Данные загружены в файл {saveFileDialog.FileName}"); } catch (Exception ex) { MessageBox.Show($"Не сохранилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); + _logger.LogWarning($"Не удалось сохранить информацию в файл: {ex.Message}"); } } } @@ -190,11 +208,12 @@ namespace ProjectElectricLocomotive try { _storage.LoadData(openFileDialog.FileName); + _logger.LogInformation($"Данные загружены из файла {openFileDialog.FileName}"); } catch (Exception ex) { MessageBox.Show($"Не загрузилось: {ex.Message}", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); - + _logger.LogWarning($"Не удалось загрузить информацию из файла: {ex.Message}"); } } } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx index f2c8442..3d78e27 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/FormLocomotiveCollections.resx @@ -133,6 +133,6 @@ 615, 17 - 84 + 25 \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs index fcbb498..70aea37 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericCollection.cs @@ -51,10 +51,7 @@ namespace ProjectElectricLocomotive.Generics public static T? operator -(LocomotiveGenericCollection collect, int pos) { T? obj = collect._collection[pos]; - if (obj != null) - { - collect._collection.Remove(pos); - } + collect._collection.Remove(pos); return obj; } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs index 0bdac4c..e137d59 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/LocomotiveGenericStorage.cs @@ -135,7 +135,7 @@ namespace ProjectElectricLocomotive.Generics { if (!File.Exists(filename)) { - throw new Exception("Файл не найден"); + throw new FileNotFoundException("Файл не найден"); } using (StreamReader fs = File.OpenText(filename)) @@ -145,13 +145,13 @@ namespace ProjectElectricLocomotive.Generics if (str == null || str.Length == 0) { - throw new Exception("Нет данных для загрузки"); + throw new IOException("Нет данных для загрузки"); } if (!str.StartsWith("LocomotiveStorage")) { //если нет такой записи, то это не те данные - throw new Exception("Неверный формат данных"); + throw new FileFormatException("Неверный формат данных"); } _locomotivesStorage.Clear(); @@ -163,7 +163,7 @@ namespace ProjectElectricLocomotive.Generics if (strs == null) { - throw new Exception("Нет данных для загрузки"); + throw new FileNotFoundException("Нет данных для загрузки"); } string[] record = strs.Split(_separatorForKeyValue, StringSplitOptions.RemoveEmptyEntries); diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs index 0dccc49..fa42fd5 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/Program.cs @@ -1,11 +1,14 @@ -using ProjectElectricLocomotive; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Serilog; -namespace ElectricLocomotive +namespace ProjectElectricLocomotive { internal static class Program { /// - /// The main entry point for the application. + /// The main entry point for the application. /// [STAThread] static void Main() @@ -13,7 +16,30 @@ namespace ElectricLocomotive // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. ApplicationConfiguration.Initialize(); - Application.Run(new FormLocomotiveCollections()); + var services = new ServiceCollection(); + ConfigureServices(services); + using (ServiceProvider serviceProvider = services.BuildServiceProvider()) + { + Application.Run(serviceProvider.GetRequiredService()); + } + } + private static void ConfigureServices(ServiceCollection services) + { + services.AddSingleton() + .AddLogging(option => + { + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile(path: "C:\\Users\\katri\\OneDrive\\ \\ \\\\PIbd-21_Bakalskaya_E.D._ElectricLocomotive._BASE\\ProjectElectricLocomotive\\ProjectElectricLocomotive\\appSettings.json", optional: false, reloadOnChange: true) + .Build(); + + var logger = new LoggerConfiguration() + .ReadFrom.Configuration(configuration) + .CreateLogger(); + + option.SetMinimumLevel(LogLevel.Information); + option.AddSerilog(logger); + }); } } } \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/ProjectElectricLocomotive.csproj b/ProjectElectricLocomotive/ProjectElectricLocomotive/ProjectElectricLocomotive.csproj index eb2b961..2a73900 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/ProjectElectricLocomotive.csproj +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/ProjectElectricLocomotive.csproj @@ -9,8 +9,26 @@ + + + + + + Always + + + + + + + + + + + + diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/SetGeneric.cs b/ProjectElectricLocomotive/ProjectElectricLocomotive/SetGeneric.cs index 026eba2..ef88ca7 100644 --- a/ProjectElectricLocomotive/ProjectElectricLocomotive/SetGeneric.cs +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/SetGeneric.cs @@ -1,4 +1,5 @@ -using System; +using ProjectElectricLocomotive.Exceptions; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -9,8 +10,8 @@ namespace ProjectElectricLocomotive.Generics internal class SetGeneric where T : class { - private readonly List _places; - + private readonly List _places; + public int Count => _places.Count; /// Максимальное количество объектов в списке @@ -29,17 +30,26 @@ namespace ProjectElectricLocomotive.Generics public int Insert(T loco, int position) { - if (position < 0 || position >= _maxCount) return -1; + if(_places.Count >= _maxCount) + throw new StorageOverflowException(_maxCount); + + if (position < 0 || position >= _maxCount) + { + return -1; + } + _places.Insert(position, loco); return position; } public T? Remove(int position) { - if (position >= Count || position < 0) + if (position >= Count || position < 0) return null; T? tmp = _places[position]; + if (tmp == null) + throw new LocoNotFoundException(position); _places[position] = null; return tmp; } diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/appSettings.json b/ProjectElectricLocomotive/ProjectElectricLocomotive/appSettings.json new file mode 100644 index 0000000..f98af6a --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/appSettings.json @@ -0,0 +1,20 @@ +{ + "Serilog": { + "Using": [ "Serilog.Sinks.File" ], + "MinimumLevel": "Information", + "WriteTo": [ + { + "Name": "File", + "Args": { + "path": "log_.log", + "rollingInterval": "Day", + "outputTemplate": "[{Timestamp:HH:mm:ss.fff}]{Level:u4}: {Message:lj}{NewLine}{Exception}" + } + } + ], + "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], + "Properties": { + "Application": "Locomotives" + } + } +} \ No newline at end of file diff --git a/ProjectElectricLocomotive/ProjectElectricLocomotive/nlog.config b/ProjectElectricLocomotive/ProjectElectricLocomotive/nlog.config new file mode 100644 index 0000000..cca6f0f --- /dev/null +++ b/ProjectElectricLocomotive/ProjectElectricLocomotive/nlog.config @@ -0,0 +1,14 @@ + + + + + + + + + + + +