This commit is contained in:
gg12 darfren 2023-11-27 20:37:21 +04:00
parent 157ab08659
commit 0898a6c9d8
5 changed files with 23 additions and 49 deletions

View File

@ -14,20 +14,19 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Serilog;
namespace Monorail
{
public partial class FormMonorailCollection : Form
{
private readonly MonorailGenericStorage _storage;
private readonly ILogger _logger;
public FormMonorailCollection(ILogger<FormMonorailCollection> logger)
public FormMonorailCollection()
{
InitializeComponent();
_storage = new MonorailGenericStorage(pictureBoxCollection.Width,
pictureBoxCollection.Height);
_logger = logger;
}
private void ReloadObjects()
@ -72,7 +71,7 @@ namespace Monorail
{
bool q = obj + m;
MessageBox.Show("Объект добавлен");
_logger.LogInformation($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
Log.Information($"Добавлен объект в коллекцию {listBoxStorages.SelectedItem.ToString() ?? string.Empty}");
m.ChangePictureBoxSize(pictureBoxCollection.Width, pictureBoxCollection.Height);
pictureBoxCollection.Image = obj.ShowMonorails();
}
@ -107,7 +106,7 @@ namespace Monorail
int pos = Convert.ToInt32(maskedTextBox.Text);
var q = obj - pos;
MessageBox.Show("Объект удален");
_logger.LogInformation($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
Log.Information($"Удален объект из коллекции {listBoxStorages.SelectedItem.ToString() ?? string.Empty} по номеру {pos}");
pictureBoxCollection.Image = obj.ShowMonorails();
}
catch (MonorailNotFoundException ex)
@ -155,7 +154,7 @@ MessageBoxIcon.Question) == DialogResult.Yes)
?? string.Empty;
_storage.DelSet(name);
ReloadObjects();
_logger.LogInformation($"Удален набор: {name}");
Log.Information($"Удален набор: {name}");
}
}
@ -170,7 +169,7 @@ MessageBoxIcon.Question) == DialogResult.Yes)
}
_storage.AddSet(storageAddNameBox.Text);
ReloadObjects();
_logger.LogInformation($"Добавлен набор: {storageAddNameBox.Text}");
Log.Information($"Добавлен набор: {storageAddNameBox.Text}");
}
private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
@ -182,7 +181,7 @@ MessageBoxIcon.Question) == DialogResult.Yes)
_storage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Сохранение прошло успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"Файл {saveFileDialog.FileName} успешно сохранен");
Log.Information($"Файл {saveFileDialog.FileName} успешно сохранен");
}
catch (Exception ex)
{
@ -202,11 +201,12 @@ MessageBoxIcon.Question) == DialogResult.Yes)
_storage.LoadData(openFileDialog.FileName);
MessageBox.Show("Загрузка прошла успешно",
"Результат", MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation($"Файл {openFileDialog.FileName} успешно загружен");
Log.Information($"Файл {openFileDialog.FileName} успешно загружен");
foreach (var collection in _storage.Keys)
{
listBoxStorages.Items.Add(collection);
}
ReloadObjects();
}
catch (Exception ex)
{

View File

@ -9,6 +9,8 @@
<ItemGroup>
<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.Sinks.File" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
@ -26,10 +28,4 @@
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -8,36 +8,28 @@ using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Json;
namespace Monorail
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var services = new ServiceCollection();
ConfigureServices(services);
using (ServiceProvider serviceProvider =
services.BuildServiceProvider())
{
Application.Run(serviceProvider.GetRequiredService<FormMonorailCollection>());
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormMonorailCollection>()
.AddLogging(option =>
{
option.SetMinimumLevel(LogLevel.Information);
option.AddNLog("nlog.config");
});
Log.Logger = new LoggerConfiguration()
.WriteTo.File("log.txt")
.MinimumLevel.Debug()
.CreateLogger();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMonorailCollection());
}
}
}

View File

View File

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