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

View File

@ -9,6 +9,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.3.5" /> <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>
<ItemGroup> <ItemGroup>
@ -26,10 +28,4 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Update="nlog.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project> </Project>

View File

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

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>