ПИбд-21 Лобашов И.Д. 7 лаб базовая #7

Closed
goldfest228 wants to merge 3 commits from lab7 into lab6
9 changed files with 68 additions and 22 deletions
Showing only changes of commit 1a8269e81b - Show all commits

View File

@ -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;

View File

@ -30,6 +30,7 @@ namespace SelfPropelledArtilleryUnit
InitializeComponent();
_storage = new UstaGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
_logger = logger;
}
/// <summary>
/// Заполнение 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()}");
}
}
/// <summary>

View File

@ -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<FormSelfPropelledArtilleryUnitCollection>()
.AddLogging(option =>
services.AddSingleton<FormSelfPropelledArtilleryUnitCollection>().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);
});

View File

@ -9,8 +9,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.AspNetCore" Version="8.0.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="8.0.0" />
</ItemGroup>
</Project>

View File

@ -65,8 +65,13 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <returns></returns>
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;
}
/// <summary>

View File

@ -57,14 +57,14 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <param name="collect"></param>
/// <param name="obj"></param>
/// <returns></returns>
public static int? operator +(UstaGenericCollection<T, U> collect, T?
public static int operator +(UstaGenericCollection<T, U> collect, T?
obj)
{
if (obj == null)
{
return -1;
}
return collect?._collection.Insert(obj);
return collect?._collection.Insert(obj) ?? -1;
}
/// <summary>
/// Перегрузка оператора вычитания
@ -72,15 +72,15 @@ namespace SelfPropelledArtilleryUnit.Generics
/// <param name="collect"></param>
/// <param name="pos"></param>
/// <returns></returns>
public static bool operator -(UstaGenericCollection<T, U> collect, int
public static T operator -(UstaGenericCollection<T, U> 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;
}
/// <summary>
/// Получение объекта IMoveableObject

View File

@ -126,7 +126,6 @@ namespace SelfPropelledArtilleryUnit.Generics
using (StreamWriter writer = new StreamWriter(filename))
{
writer.Write($"UstaStorage{Environment.NewLine}{data}");
writer.Write(data.ToString());
}
}

View File

@ -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
{

View File

@ -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"
}
}
}