Compare commits

...

2 Commits

Author SHA1 Message Date
Дамир Нугаев
52eaba7622 fix 2 2022-12-06 13:16:46 +04:00
Дамир Нугаев
5327e9044f готовая лабораторная работа 7 2022-12-03 20:08:32 +04:00
8 changed files with 46 additions and 44 deletions

View File

@ -9,21 +9,24 @@
</PropertyGroup>
<ItemGroup>
<None Remove="nlog.config" />
<None Remove="serilog.json" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="nlog.config">
<EmbeddedResource Include="serilog.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.1.0" />
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Settings.Configuration" Version="3.4.0" />
<PackageReference Include="Serilog.Settings.Delegates" Version="1.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -10,13 +10,9 @@ namespace Bus
internal class BusNotFoundException : ApplicationException
{
public BusNotFoundException(int i) : base($"Не найден объект по позиции {i}") { }
public BusNotFoundException() : base() { }
public BusNotFoundException(string message) : base(message) { }
public BusNotFoundException(string message, Exception exception) : base(message, exception) { }
protected BusNotFoundException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -36,6 +36,10 @@ namespace Bus
}
public FormMapWithSetDoubleDeckerBus()
{
}
private void ReloadMaps()
{
int index = listBoxMaps.SelectedIndex;
@ -130,24 +134,24 @@ namespace Bus
int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
try
{
if (_mapBusCollectionGeneric - pos != null)
var deletedBus = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty] - pos;
if (deletedBus != null)
{
MessageBox.Show("Объект удален");
pictureBox.Image = _mapBusCollectionGeneric.ShowSet();
_logger.LogInformation("Из текущей карты удален объект {@ship}", deletedBus);
pictureBox.Image = _mapsCollection[listBoxMaps.SelectedItem?.ToString() ?? string.Empty].ShowSet();
}
else
{
_logger.LogInformation("Не удалось добавить объект по позиции {0} равен null", pos);
MessageBox.Show("Не удалось удалить объект");
}
}
catch (BusNotFoundException ex)
{
MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
catch (Exception ex)
{
MessageBox.Show($"Неизвестная ошибка: {ex.Message}");
}
_logger.LogWarning("Ошибка удаления: {0}", ex.Message);
MessageBox.Show($"Ошибка удаления: {ex.Message}");
}
}
private void ButtonShowStorage_Click(object sender, EventArgs e)

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Serilog;
namespace Bus
{
@ -20,8 +21,8 @@ namespace Bus
using (ServiceProvider serviceProvider = services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormMapWithSetDoubleDeckerBus>());
}
/*Application.Run(new FormMapWithSetDoubleDeckerBus());*/
}
}
private static void ConfigureServices(ServiceCollection services)
@ -29,8 +30,14 @@ namespace Bus
services.AddSingleton<FormMapWithSetDoubleDeckerBus>()
.AddLogging(option =>
{
var configuration = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile(path: "serilog.json").Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
option.AddSerilog(logger);
});
}
}

View File

@ -26,11 +26,6 @@ namespace Bus
return Insert(bus, 0);
}
private bool isCorrectPosition(int position)
{
return 0 <= position && position < _maxCount;
}
public int Insert(T bus, int position)
{
if (position > _maxCount && position < 0)

View File

@ -10,13 +10,9 @@ namespace Bus
internal class StorageOverflowException : ApplicationException
{
public StorageOverflowException(int count) : base($"В наборе превышено допустимое количество: {count}") { }
public StorageOverflowException() : base() { }
public StorageOverflowException(string message) : base(message) { }
public StorageOverflowException(string message, Exception exception) : base(message, exception) { }
protected StorageOverflowException(SerializationInfo info, StreamingContext contex) : base(info, contex) { }
}
}

View File

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<nlog xmlns="http://www.nlog-progect.org/schemas/NLog.xsd"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instanse"
autoReload="true" internalLogLevel="Info">
<targets>
<target xsi:type="File" name="tofile" fileName="buslog-${shortdate}.log"/>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile"/>
</rules>
</nlog>
</configuration>

16
Bus/Bus/serilog.json Normal file
View File

@ -0,0 +1,16 @@
{
"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}"
}
}
],
}
}