From 1a8269e81b894887ce1c0c9e64d529f81f298b77 Mon Sep 17 00:00:00 2001 From: marusya Date: Sat, 16 Dec 2023 19:54:05 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DrawningUsta.cs | 2 +- ...ormSelfPropelledArtilleryUnitCollection.cs | 24 ++++++++++++------- .../SelfPropelledArtilleryUnit/Program.cs | 15 +++++++++--- .../SelfPropelledArtilleryUnit.csproj | 5 ++++ .../SelfPropelledArtilleryUnit/SetGeneric.cs | 9 +++++-- .../UstaGenericCollection.cs | 12 +++++----- .../UstaGenericStorage.cs | 1 - .../UstaNotFoundException.cs | 2 +- .../appsettings.json | 20 ++++++++++++++++ 9 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/appsettings.json diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs index 8f5ecf9..8095c73 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/DrawningUsta.cs @@ -95,7 +95,7 @@ namespace SelfPropelledArtilleryUnit.DrawningObjects public void SetPosition(int x, int y) { // TODO: Изменение x, y, если при установке объект выходит за границы - if (x < 0 || y < 0 || x + _ustaWidth > _pictureWidth || y + _ustaHeight > _pictureHeight) + if (x < 0 || y < 0 ) { x = 10; y = 10; diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs index ca25c3b..24c77fa 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/FormSelfPropelledArtilleryUnitCollection.cs @@ -30,6 +30,7 @@ namespace SelfPropelledArtilleryUnit InitializeComponent(); _storage = new UstaGenericStorage(pictureBoxCollection.Width, pictureBoxCollection.Height); + _logger = logger; } /// /// Заполнение listBoxObjects @@ -171,17 +172,24 @@ namespace SelfPropelledArtilleryUnit return; } int pos = Convert.ToInt32(maskedTextBoxNumber.Text); - if (obj - pos != null) + try { - MessageBox.Show("Объект удален"); - pictureBoxCollection.Image = obj.ShowUsta(); - _logger.LogInformation($"Удален объект из набора {listBoxStorage.SelectedItem.ToString()}"); + if (obj - pos != null) + { + MessageBox.Show("Объект удален"); + pictureBoxCollection.Image = obj.ShowUsta(); + _logger.LogInformation($"Удален объект из набора {listBoxStorage.SelectedItem.ToString()}"); + } + else + { + MessageBox.Show("Не удалось удалить объект"); + _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorage.SelectedItem.ToString()}"); + } } - else + catch (UstaNotFoundException ex) { - MessageBox.Show("Не удалось удалить объект"); - _logger.LogWarning($"Не удалось удалить объект из набора {listBoxStorage.SelectedItem.ToString()}"); - + MessageBox.Show(ex.Message); + _logger.LogWarning($"{ex.Message} из набора {listBoxStorage.SelectedItem.ToString()}"); } } /// diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs index 7c7cde6..82731cf 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/Program.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; +using Serilog; namespace ProjectUsta { @@ -25,11 +26,19 @@ namespace ProjectUsta private static void ConfigureServices(ServiceCollection services) { - services.AddSingleton() - .AddLogging(option => + 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}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); }); diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.csproj b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.csproj index cee80ce..b5cbe31 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.csproj +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit.csproj @@ -9,8 +9,13 @@ + + + + + \ No newline at end of file diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs index 972be12..e7f9ac4 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/SetGeneric.cs @@ -65,8 +65,13 @@ namespace SelfPropelledArtilleryUnit.Generics /// public bool Remove(int position) { - if ((position < 0) || (position > _maxCount)) return false; - _places.RemoveAt(position); + if (position < 0 || position > _maxCount || position >= Count) + throw new UstaNotFoundException(); + if (_places[position] == null) + { + throw new UstaNotFoundException(); + } + _places[position] = null; return true; } /// diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs index 4de0d4f..7857c8d 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericCollection.cs @@ -57,14 +57,14 @@ namespace SelfPropelledArtilleryUnit.Generics /// /// /// - public static int? operator +(UstaGenericCollection collect, T? + public static int operator +(UstaGenericCollection collect, T? obj) { if (obj == null) { return -1; } - return collect?._collection.Insert(obj); + return collect?._collection.Insert(obj) ?? -1; } /// /// Перегрузка оператора вычитания @@ -72,15 +72,15 @@ namespace SelfPropelledArtilleryUnit.Generics /// /// /// - public static bool operator -(UstaGenericCollection collect, int + public static T operator -(UstaGenericCollection collect, int pos) { - T? obj = collect._collection[pos]; + T obj = collect._collection[pos]; if (obj != null) { - return collect._collection.Remove(pos); + collect?._collection.Remove(pos); } - return false; + return obj; } /// /// Получение объекта IMoveableObject diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericStorage.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericStorage.cs index a9b4140..ef36e8d 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericStorage.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaGenericStorage.cs @@ -126,7 +126,6 @@ namespace SelfPropelledArtilleryUnit.Generics using (StreamWriter writer = new StreamWriter(filename)) { writer.Write($"UstaStorage{Environment.NewLine}{data}"); - writer.Write(data.ToString()); } } diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaNotFoundException.cs b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaNotFoundException.cs index 534e3a7..6295c73 100644 --- a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaNotFoundException.cs +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/UstaNotFoundException.cs @@ -5,7 +5,7 @@ using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; -namespace SelfPropelledArtilleryUnit.Exceptions +namespace SelfPropelledArtilleryUnit { [Serializable] internal class UstaNotFoundException : ApplicationException { diff --git a/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/appsettings.json b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/appsettings.json new file mode 100644 index 0000000..18b762e --- /dev/null +++ b/SelfPropelledArtilleryUnit/SelfPropelledArtilleryUnit/appsettings.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