completed

This commit is contained in:
rozenkranzz 2025-02-23 00:02:27 +04:00
parent 6a0fd53bfb
commit 99aff14820
12 changed files with 278 additions and 87 deletions

View File

@ -0,0 +1,17 @@
using System.Runtime.Serialization;
namespace ProjectLiner.Exceptions;
internal class CollectionOverflowException : ApplicationException
{
public CollectionOverflowException(int count) :
base("Collection overflow. Count: " + count)
{ }
public CollectionOverflowException() : base() { }
public CollectionOverflowException(string message) : base(message) { }
public CollectionOverflowException(string message, Exception exception) :
base(message, exception)
{ }
protected CollectionOverflowException(SerializationInfo info,
StreamingContext context) : base(info, context) { }
}

View File

@ -0,0 +1,16 @@
using System.Runtime.Serialization;
namespace ProjectLiner.Exceptions;
internal class ObjectNotFoundException : ApplicationException
{
public ObjectNotFoundException(int i) : base("Object not found. Index: " + i)
{ }
public ObjectNotFoundException() : base() { }
public ObjectNotFoundException(string message) : base(message) { }
public ObjectNotFoundException(string message, Exception exception) :
base(message, exception)
{ }
protected ObjectNotFoundException(SerializationInfo info, StreamingContext
context) : base(info, context) { }
}

View File

@ -0,0 +1,16 @@
using System.Runtime.Serialization;
namespace ProjectLiner.Exceptions;
internal class PosOutOfCollectionRangeException : ApplicationException
{
public PosOutOfCollectionRangeException(int i) :
base("Pos out of collection range. Index: " + i)
{ }
public PosOutOfCollectionRangeException() : base() { }
public PosOutOfCollectionRangeException(string message) : base(message) { }
public PosOutOfCollectionRangeException(string message, Exception
exception) : base(message, exception) { }
protected PosOutOfCollectionRangeException(SerializationInfo info,
StreamingContext context) : base(info, context) { }
}

View File

@ -1,6 +1,9 @@
using ProjectLiner.CollectionGenericObjects; using System.Linq.Expressions;
using Microsoft.Extensions.Logging;
using ProjectLiner.CollectionGenericObjects;
using ProjectLiner.Drawnings; using ProjectLiner.Drawnings;
using ProjectLiner.Entities; using ProjectLiner.Entities;
using ProjectLiner.Exceptions;
using ProjectLiner.GenericObjectsCollection; using ProjectLiner.GenericObjectsCollection;
namespace ProjectLiner; namespace ProjectLiner;
@ -9,11 +12,13 @@ public partial class FormShipCollection : Form
{ {
private readonly CollectionStorage<DrawingBaseLiner> _collectionStorage; private readonly CollectionStorage<DrawingBaseLiner> _collectionStorage;
private AbstractCompany? _company = null; private AbstractCompany? _company = null;
public FormShipCollection() private readonly ILogger _logger;
public FormShipCollection(ILogger<FormShipCollection> logger)
{ {
InitializeComponent(); InitializeComponent();
_collectionStorage = new CollectionStorage<DrawingBaseLiner>(); _collectionStorage = new CollectionStorage<DrawingBaseLiner>();
panelCompanyTools.Enabled = false; panelCompanyTools.Enabled = false;
_logger = logger;
} }
private void ComboBoxCompanySelector_SelectedIndexChanged(object sender, private void ComboBoxCompanySelector_SelectedIndexChanged(object sender,
@ -34,14 +39,19 @@ public partial class FormShipCollection : Form
{ {
return; return;
} }
if (_company + liner) try
{ {
MessageBox.Show("Object was added"); if (_company + liner)
pictureBox.Image = _company.Show(); {
pictureBox.Image = _company.Show();
MessageBox.Show("Liner added successfully");
_logger.LogInformation("Liner added successfully at {Time}", DateTime.Now);
}
} }
else catch (CollectionOverflowException ex)
{ {
MessageBox.Show("Couldn't add object"); MessageBox.Show("" + ex.Message);
_logger.LogWarning("Failed to add liner: {Message} at {Time}", ex.Message, DateTime.Now);
} }
} }
private void ButtonRemoveShip_Click(object sender, EventArgs e) private void ButtonRemoveShip_Click(object sender, EventArgs e)
@ -52,19 +62,31 @@ public partial class FormShipCollection : Form
return; return;
} }
if (MessageBox.Show("Remove object?", "Removing...", if (MessageBox.Show("Remove object?", "Removing...",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes) MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
{ {
return; return;
} }
int pos = Convert.ToInt32(maskedTextBoxPosition.Text); int pos = Convert.ToInt32(maskedTextBoxPosition.Text);
if (_company - pos) try
{ {
MessageBox.Show("Object was removed"); if (_company - pos)
pictureBox.Image = _company.Show(); {
MessageBox.Show("Object was removed");
pictureBox.Image = _company.Show();
_logger.LogInformation("Liner removed successfully at position {pos} at {Time}",
pos, DateTime.Now);
}
} }
else catch (ObjectNotFoundException ex)
{
MessageBox.Show("" + ex.Message);
_logger.LogWarning("Failed to remove liner: Object not found at postion {pos}",
pos);
}
catch (PosOutOfCollectionRangeException ex)
{ {
MessageBox.Show("Couldn't remove object"); MessageBox.Show("Couldn't remove object");
_logger.LogWarning("Failed to remove liner: {Message}", ex.Message);
} }
} }
private void ButtonSubmitForTesting_Click(object sender, EventArgs e) private void ButtonSubmitForTesting_Click(object sender, EventArgs e)
@ -77,7 +99,19 @@ public partial class FormShipCollection : Form
int counter = 100; int counter = 100;
while (liner == null) while (liner == null)
{ {
liner = _company.GetRandomObject(); try
{
liner = _company.GetRandomObject();
_logger.LogInformation("Object was submitted for testing at position {pos}", counter);
}
catch (ObjectNotFoundException)
{
_logger.LogWarning("Object not found at position {pos}", counter);
}
catch (PosOutOfCollectionRangeException)
{
_logger.LogWarning("Position out of range at {pos}", counter);
}
counter--; counter--;
if (counter <= 0) if (counter <= 0)
{ {
@ -110,6 +144,7 @@ public partial class FormShipCollection : Form
{ {
MessageBox.Show("Please fill all fields", "Error", MessageBox.Show("Please fill all fields", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogInformation("Failed to add collection: Please fill all fields");
return; return;
} }
CollectionType collectionType = CollectionType.None; CollectionType collectionType = CollectionType.None;
@ -123,6 +158,8 @@ public partial class FormShipCollection : Form
} }
_collectionStorage.AddCollection(textBoxCollectionName.Text, _collectionStorage.AddCollection(textBoxCollectionName.Text,
collectionType); collectionType);
_logger.LogInformation("Collection of type {type} with name {name} added successfully",
collectionType, textBoxCollectionName.Text);
RefreshListBoxItems(); RefreshListBoxItems();
} }
@ -134,6 +171,7 @@ public partial class FormShipCollection : Form
{ {
MessageBox.Show("Collection is not selected", "Error", MessageBox.Show("Collection is not selected", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogInformation("Failed to remove collection: Collection is not selected");
return; return;
} }
@ -145,6 +183,7 @@ public partial class FormShipCollection : Form
RefreshListBoxItems(); RefreshListBoxItems();
MessageBox.Show("Collection removed successfully", "Success", MessageBox.Show("Collection removed successfully", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Collection {name} removed successfully", collectionName);
} }
} }
@ -167,6 +206,7 @@ public partial class FormShipCollection : Form
listBoxCollections.SelectedItem == null) listBoxCollections.SelectedItem == null)
{ {
MessageBox.Show("Collection is not selected"); MessageBox.Show("Collection is not selected");
_logger.LogInformation("Failed to create company: Collection is not selected");
return; return;
} }
IGenericObjectsCollection<DrawingBaseLiner>? collection = IGenericObjectsCollection<DrawingBaseLiner>? collection =
@ -174,6 +214,7 @@ public partial class FormShipCollection : Form
if (collection == null) if (collection == null)
{ {
MessageBox.Show("Collection is not found"); MessageBox.Show("Collection is not found");
_logger.LogInformation("Failed to create company: Collection is not found");
return; return;
} }
switch (comboBoxCompanySelector.Text) switch (comboBoxCompanySelector.Text)
@ -184,6 +225,7 @@ public partial class FormShipCollection : Form
break; break;
} }
panelCompanyTools.Enabled = true; panelCompanyTools.Enabled = true;
_logger.LogInformation($"Company was created: {comboBoxCompanySelector.Text}");
RefreshListBoxItems(); RefreshListBoxItems();
} }
@ -191,15 +233,18 @@ public partial class FormShipCollection : Form
{ {
if (saveFileDialog.ShowDialog() == DialogResult.OK) if (saveFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_collectionStorage.SaveData(saveFileDialog.FileName)) try
{ {
_collectionStorage.SaveData(saveFileDialog.FileName);
MessageBox.Show("Data saved successfully", "Success", MessageBox.Show("Data saved successfully", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Saved in {filename}", saveFileDialog.FileName);
} }
else catch (Exception ex)
{ {
MessageBox.Show("Data wasn't saved", "Error", MessageBox.Show(ex.Message + "Data wasn't saved", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Error saving data: {Message}", ex.Message);
} }
} }
} }
@ -208,16 +253,19 @@ public partial class FormShipCollection : Form
{ {
if (openFileDialog.ShowDialog() == DialogResult.OK) if (openFileDialog.ShowDialog() == DialogResult.OK)
{ {
if (_collectionStorage.LoadData(openFileDialog.FileName)) try
{ {
_collectionStorage.LoadData(openFileDialog.FileName);
MessageBox.Show("Data loaded successfully", "Success", MessageBox.Show("Data loaded successfully", "Success",
MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBoxButtons.OK, MessageBoxIcon.Information);
_logger.LogInformation("Loaded from {filename}", openFileDialog.FileName);
RefreshListBoxItems(); RefreshListBoxItems();
} }
else catch (Exception ex)
{ {
MessageBox.Show("Data wasn't loaded", "Error", MessageBox.Show(ex.Message + "Data wasn't loaded", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error); MessageBoxButtons.OK, MessageBoxIcon.Error);
_logger.LogError("Error loading data: {Message}", ex.Message);
} }
} }
RefreshListBoxItems(); RefreshListBoxItems();

View File

@ -1,4 +1,6 @@
using ProjectLiner.Drawnings; using System;
using ProjectLiner.Drawnings;
using ProjectLiner.Exceptions;
namespace ProjectLiner.GenericObjectsCollection; namespace ProjectLiner.GenericObjectsCollection;
@ -6,11 +8,12 @@ public abstract class AbstractCompany
{ {
protected readonly int _placeSizeWidth = 210; protected readonly int _placeSizeWidth = 210;
protected readonly int _placeSizeHeight = 80; protected readonly int _placeSizeHeight = 80;
protected readonly int _placeSpacing = 20;
protected readonly int _pictureWidth; protected readonly int _pictureWidth;
protected readonly int _pictureHeight; protected readonly int _pictureHeight;
protected IGenericObjectsCollection<DrawingBaseLiner>? _collection = null; protected IGenericObjectsCollection<DrawingBaseLiner>? _collection = null;
private int GetMaxCount => _pictureWidth * _pictureHeight / private int GetMaxCount => _pictureWidth / _placeSizeWidth
(_placeSizeWidth * _placeSizeHeight); * _pictureHeight / _placeSizeHeight;
public AbstractCompany(int picWidth, int picHeight, public AbstractCompany(int picWidth, int picHeight,
IGenericObjectsCollection<DrawingBaseLiner> collection) IGenericObjectsCollection<DrawingBaseLiner> collection)
{ {
@ -21,11 +24,31 @@ public abstract class AbstractCompany
} }
public static bool operator +(AbstractCompany company, DrawingBaseLiner liner) public static bool operator +(AbstractCompany company, DrawingBaseLiner liner)
{ {
return company._collection?.Insert(liner) ?? false; try
{
company._collection?.Insert(liner);
return true;
}
catch (CollectionOverflowException)
{
throw;
}
} }
public static bool operator -(AbstractCompany company, int position) public static bool operator -(AbstractCompany company, int position)
{ {
return company._collection?.Remove(position) ?? false; try
{
company._collection?.Remove(position);
return true;
}
catch (PosOutOfCollectionRangeException)
{
throw;
}
catch (ObjectNotFoundException)
{
throw;
}
} }
public DrawingBaseLiner? GetRandomObject() public DrawingBaseLiner? GetRandomObject()
@ -38,11 +61,23 @@ public abstract class AbstractCompany
Bitmap bitmap = new(_pictureWidth, _pictureHeight); Bitmap bitmap = new(_pictureWidth, _pictureHeight);
Graphics graphics = Graphics.FromImage(bitmap); Graphics graphics = Graphics.FromImage(bitmap);
DrawBackgound(graphics); DrawBackgound(graphics);
SetObjectsPosition(); try
for (int i = 0; i < (_collection?.Count ?? 0); ++i) {
SetObjectsPosition();
for (int i = 0; i < (_collection?.Count ?? 0); ++i)
{
try
{
DrawingBaseLiner? obj = _collection?.Get(i);
obj?.DrawTransport(graphics);
}
catch (ObjectNotFoundException)
{
}
}
}
catch (ObjectNotFoundException)
{ {
DrawingBaseLiner? obj = _collection?.Get(i);
obj?.DrawTransport(graphics);
} }
return bitmap; return bitmap;
} }

View File

@ -1,5 +1,6 @@
using System.Text; using System.Text;
using ProjectLiner.Drawnings; using ProjectLiner.Drawnings;
using ProjectLiner.Exceptions;
namespace ProjectLiner.GenericObjectsCollection; namespace ProjectLiner.GenericObjectsCollection;
@ -49,11 +50,11 @@ public class CollectionStorage<T>
private readonly string _separatorForKeyValue = "|"; private readonly string _separatorForKeyValue = "|";
private readonly string _separatorItems = ";"; private readonly string _separatorItems = ";";
public bool SaveData(string filename) public void SaveData(string filename)
{ {
if (_storages.Count == 0) if (_storages.Count == 0)
{ {
return false; throw new Exception("No data to save");
} }
if (File.Exists(filename)) if (File.Exists(filename))
{ {
@ -92,14 +93,13 @@ public class CollectionStorage<T>
using FileStream fs = new(filename, FileMode.Create); using FileStream fs = new(filename, FileMode.Create);
byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString()); byte[] info = new UTF8Encoding(true).GetBytes(sb.ToString());
fs.Write(info, 0, info.Length); fs.Write(info, 0, info.Length);
return true;
} }
public bool LoadData(string filename) public void LoadData(string filename)
{ {
if (!File.Exists(filename)) if (!File.Exists(filename))
{ {
return false; throw new Exception("File not found");
} }
string bufferTextFromFile = ""; string bufferTextFromFile = "";
using (FileStream fs = new(filename, FileMode.Open)) using (FileStream fs = new(filename, FileMode.Open))
@ -115,11 +115,11 @@ public class CollectionStorage<T>
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
if (strs == null || strs.Length == 0) if (strs == null || strs.Length == 0)
{ {
return false; throw new Exception(filename + " is empty");
} }
if (!strs[0].Equals(_collectionKey)) if (!strs[0].Equals(_collectionKey))
{ {
return false; throw new Exception("Invalid file format");
} }
_storages.Clear(); _storages.Clear();
foreach (string data in strs) foreach (string data in strs)
@ -133,11 +133,9 @@ public class CollectionStorage<T>
CollectionType collectionType = CollectionType collectionType =
(CollectionType)Enum.Parse(typeof(CollectionType), record[1]); (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
IGenericObjectsCollection<T>? collection = IGenericObjectsCollection<T>? collection =
CollectionStorage<T>.CreateCollection(collectionType); CollectionStorage<T>.CreateCollection(collectionType) ??
if (collection == null) throw new Exception("Invalid collection type");
{
return false;
}
collection.MaxCount = Convert.ToInt32(record[2]); collection.MaxCount = Convert.ToInt32(record[2]);
string[] set = record[3].Split(_separatorItems, string[] set = record[3].Split(_separatorItems,
StringSplitOptions.RemoveEmptyEntries); StringSplitOptions.RemoveEmptyEntries);
@ -145,15 +143,18 @@ public class CollectionStorage<T>
{ {
if (elem?.CreateDrawingLiner() is T liner) if (elem?.CreateDrawingLiner() is T liner)
{ {
if (!collection.Insert(liner)) try
{ {
return false; collection.Insert(liner);
}
catch (CollectionOverflowException e)
{
throw new Exception("Collection overflow", e);
} }
} }
} }
_storages.Add(record[0], collection); _storages.Add(record[0], collection);
} }
return true;
} }
private static IGenericObjectsCollection<T>? CreateCollection( private static IGenericObjectsCollection<T>? CreateCollection(

View File

@ -1,4 +1,6 @@
 
using ProjectLiner.Exceptions;
namespace ProjectLiner.GenericObjectsCollection; namespace ProjectLiner.GenericObjectsCollection;
public class GenericObjectsCollection<T> : IGenericObjectsCollection<T> public class GenericObjectsCollection<T> : IGenericObjectsCollection<T>
@ -36,36 +38,45 @@ public class GenericObjectsCollection<T> : IGenericObjectsCollection<T>
{ {
if (position < 0 || position >= _collection.Length) if (position < 0 || position >= _collection.Length)
{ {
return null; throw new PosOutOfCollectionRangeException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException("Object not found at position " + position);
} }
return _collection[position]; return _collection[position];
} }
public bool Insert(T obj) public void Insert(T obj)
{ {
for (int i = 0; i < _collection.Length; ++i) try
{ {
if (_collection[i] == null) Insert(obj, 0);
{ }
_collection[i] = obj; catch (CollectionOverflowException)
return true; {
} throw;
} }
return false;
} }
public bool Insert(T obj, int position) public void Insert(T obj, int position)
{ {
if (position < 0 || position >= _collection.Length)
{
throw new PosOutOfCollectionRangeException(position);
}
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return;
} }
else else
{ {
while (position++ < _collection.Length) while (position++ < _collection.Length - 1)
{ {
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return true; return;
} }
} }
@ -74,20 +85,24 @@ public class GenericObjectsCollection<T> : IGenericObjectsCollection<T>
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return true; return;
} }
} }
} }
return false; throw new CollectionOverflowException("Maximum number of objects has been exceeded: " +
_collection.Length);
} }
public bool Remove(int position) public void Remove(int position)
{ {
if (position < 0 || position >= _collection.Length)
{
throw new PosOutOfCollectionRangeException(position);
}
if (_collection[position] == null) if (_collection[position] == null)
{ {
return false; throw new ObjectNotFoundException("Object not found at position " + position);
} }
_collection[position] = null; _collection[position] = null;
return true;
} }
public IEnumerable<T> GetItems() public IEnumerable<T> GetItems()

View File

@ -5,9 +5,9 @@ public interface IGenericObjectsCollection<T>
{ {
int Count { get; } int Count { get; }
int MaxCount { get; set; } int MaxCount { get; set; }
bool Insert(T obj); void Insert(T obj);
bool Insert(T obj, int position); void Insert(T obj, int position);
bool Remove(int position); void Remove(int position);
T? Get(int position); T? Get(int position);
CollectionType GetCollectionType { get; } CollectionType GetCollectionType { get; }

View File

@ -1,4 +1,5 @@
using ProjectLiner.Drawnings; using ProjectLiner.Drawnings;
using ProjectLiner.Exceptions;
using ProjectLiner.GenericObjectsCollection; using ProjectLiner.GenericObjectsCollection;
namespace ProjectLiner.CollectionGenericObjects; namespace ProjectLiner.CollectionGenericObjects;
@ -60,15 +61,17 @@ public class LandingStage : AbstractCompany
return; return;
} }
DrawingBaseLiner? obj = _collection.Get(index); try
if (obj != null)
{ {
obj.SetPictureSize(_pictureWidth, _pictureHeight); DrawingBaseLiner? obj = _collection.Get(index);
obj?.SetPictureSize(_pictureWidth, _pictureHeight);
int x = i * (_placeSizeWidth); int x = i * (_placeSizeWidth);
int y = j * (_placeSizeHeight); int y = j * (_placeSizeHeight);
obj.SetPosition(x + spacing, y + spacing); obj?.SetPosition(x + spacing, y + spacing);
}
catch (ObjectNotFoundException)
{
} }
index++; index++;
} }
} }

View File

@ -1,4 +1,6 @@
 
using ProjectLiner.Exceptions;
namespace ProjectLiner.GenericObjectsCollection; namespace ProjectLiner.GenericObjectsCollection;
public class ListGenericObjects<T> : IGenericObjectsCollection<T> public class ListGenericObjects<T> : IGenericObjectsCollection<T>
@ -31,48 +33,50 @@ public class ListGenericObjects<T> : IGenericObjectsCollection<T>
{ {
if (position < 0 || position >= _collection.Count) if (position < 0 || position >= _collection.Count)
{ {
return null; throw new PosOutOfCollectionRangeException(position);
}
if (_collection[position] == null)
{
throw new ObjectNotFoundException("Object not found at position " + position);
} }
return _collection[position]; return _collection[position];
} }
public bool Insert(T obj) public void Insert(T obj)
{ {
if (_collection.Count < _maxCount) if (_collection.Count == _maxCount)
{ {
_collection.Add(obj); throw new CollectionOverflowException("Collection is full " + _collection.Count);
return true;
} }
return false; _collection.Add(obj);
} }
public bool Insert(T obj, int position) public void Insert(T obj, int position)
{ {
if (position < 0 || position >= _maxCount) if (position < 0 || position >= _maxCount)
{ {
return false; throw new PosOutOfCollectionRangeException(position);
} }
if (position >= _collection.Count) if (position >= _collection.Count)
{ {
_collection.AddRange(Enumerable.Repeat(default(T)!, position - _collection.Count + 1)); _collection.AddRange(Enumerable.Repeat(default(T)!,
position - _collection.Count + 1));
} }
if (_collection[position] == null) if (_collection[position] == null)
{ {
_collection[position] = obj; _collection[position] = obj;
return true; return;
} }
throw new CollectionOverflowException("Maximum number of objects has been exceeded: " +
return false; _maxCount);
} }
public bool Remove(int position) public void Remove(int position)
{ {
if (position < 0 || position >= _collection.Count || if (position < 0 || position >= _collection.Count)
_collection[position] == null)
{ {
return false; throw new PosOutOfCollectionRangeException(position);
} }
_collection.RemoveAt(position); _collection.RemoveAt(position);
return true;
} }
public IEnumerable<T> GetItems() public IEnumerable<T> GetItems()

View File

@ -1,3 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using Serilog;
namespace ProjectLiner namespace ProjectLiner
{ {
internal static class Program internal static class Program
@ -11,7 +16,28 @@ namespace ProjectLiner
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new FormShipCollection());
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
ServiceCollection services = new ServiceCollection();
ConfigureServices(services);
using ServiceProvider serviceProvider = services.BuildServiceProvider(); // Ensure BuildServiceProvider is available
Application.Run(serviceProvider.GetRequiredService<FormShipCollection>());
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddSingleton<FormShipCollection>()
.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog();
});
} }
} }
} }

View File

@ -8,6 +8,16 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
<PackageReference Include="NLog" Version="5.4.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.4.0" />
<PackageReference Include="Serilog" Version="4.2.0" />
<PackageReference Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Update="Properties\Resources.Designer.cs"> <Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>