In process : solve log problem & add left exceptions
This commit is contained in:
parent
f575f977b0
commit
7f58bbb1ad
@ -1,4 +1,6 @@
|
|||||||
|
|
||||||
|
using ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
public class ArrayGenObj<T> : ICollectionGenObj<T>
|
public class ArrayGenObj<T> : ICollectionGenObj<T>
|
||||||
@ -36,74 +38,140 @@ public class ArrayGenObj<T> : ICollectionGenObj<T>
|
|||||||
// methods :
|
// methods :
|
||||||
public T? GetItem(int index)
|
public T? GetItem(int index)
|
||||||
{
|
{
|
||||||
if (index > Count || index < 0)
|
try
|
||||||
{
|
{
|
||||||
|
if (index > Count)
|
||||||
|
throw new CollectionOverflowException(index);
|
||||||
|
if (index < 0)
|
||||||
|
throw new PositionOutOfCollectionException(index);
|
||||||
|
|
||||||
|
if (_collection[index] == null)
|
||||||
|
throw new ObjectNotFoundException(index);
|
||||||
|
|
||||||
|
return _collection[index];
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (PositionOutOfCollectionException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return _collection[index];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? item)
|
public int Insert(T? item)
|
||||||
{
|
{
|
||||||
if (Count >= _maxCount || item == null)
|
if (item == null) { return -1; }
|
||||||
|
|
||||||
|
try // выход за границы, курируется CollectionOverflowException
|
||||||
{
|
{
|
||||||
|
if (Count >= _maxCount)
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException(Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
// any empty place -> fill immediately
|
||||||
|
for (int i = 0; i < _collection.Length; i++)
|
||||||
|
{
|
||||||
|
if (_collection[i] == null)
|
||||||
|
{
|
||||||
|
_collection[i] = item;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Count;
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// any empty place -> fill immediately
|
|
||||||
for (int i = 0; i < _collection.Length; i++)
|
|
||||||
{
|
|
||||||
if (_collection[i] == null)
|
|
||||||
{
|
|
||||||
_collection[i] = item;
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? item, int index)
|
public int Insert(T? item, int index)
|
||||||
{
|
{
|
||||||
if (index >= _maxCount || Count >= _maxCount ||
|
try
|
||||||
index < 0 || _collection[index] != null
|
|
||||||
|| item == null)
|
|
||||||
{
|
{
|
||||||
return -1;
|
if (index < 0 || index >= _maxCount) throw new PositionOutOfCollectionException(index);
|
||||||
}
|
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
||||||
|
|
||||||
if (_collection[index] == null)
|
if (item == null) throw new ObjectNotFoundException(index);
|
||||||
{
|
|
||||||
_collection[index] = item;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int min_diff = 100, firstNullIndex = 100;
|
if (_collection[index] == null)
|
||||||
|
|
||||||
for (int i = 0; i < Count; i++)
|
|
||||||
{
|
|
||||||
if (_collection[i] == null
|
|
||||||
&& min_diff > Math.Abs(index - i))
|
|
||||||
{
|
{
|
||||||
min_diff = Math.Abs(index - i);
|
_collection[index] = item;
|
||||||
firstNullIndex = i;
|
return index;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int min_diff = 100, firstNullIndex = 100;
|
||||||
|
|
||||||
|
for (int i = 0; i < Count; i++)
|
||||||
|
{
|
||||||
|
if (_collection[i] == null && min_diff > Math.Abs(index - i))
|
||||||
|
{
|
||||||
|
min_diff = Math.Abs(index - i);
|
||||||
|
firstNullIndex = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_collection[firstNullIndex] = item;
|
||||||
|
return firstNullIndex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
_collection[firstNullIndex] = item;
|
{
|
||||||
return firstNullIndex;
|
Console.WriteLine(ex.Message);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
catch (PositionOutOfCollectionException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int index)
|
public T? Remove(int index)
|
||||||
{
|
{
|
||||||
if (index >= Count || index < 0)
|
try
|
||||||
// on the other positions items don't exist
|
|
||||||
{
|
{
|
||||||
|
if (index >= _maxCount || index < 0)
|
||||||
|
// on the other positions items don't exist
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException(index);
|
||||||
|
// [?] PositionOutOfCollectionException <<<
|
||||||
|
}
|
||||||
|
|
||||||
|
T? item = _collection[index];
|
||||||
|
_collection[index] = null;
|
||||||
|
|
||||||
|
if (item == null) throw new ObjectNotFoundException(index);
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
T? item = _collection[index];
|
|
||||||
_collection[index] = null;
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
@ -40,49 +41,111 @@ public class ListGenObj<T> : ICollectionGenObj<T>
|
|||||||
|
|
||||||
public T? GetItem(int position)
|
public T? GetItem(int position)
|
||||||
{
|
{
|
||||||
if (position >= Count || position < 0)
|
try
|
||||||
{
|
{
|
||||||
|
if (position > Count)
|
||||||
|
throw new CollectionOverflowException(position);
|
||||||
|
if (position < 0)
|
||||||
|
throw new PositionOutOfCollectionException(position);
|
||||||
|
|
||||||
|
if (_collection[position] == null)
|
||||||
|
throw new ObjectNotFoundException(position);
|
||||||
|
|
||||||
|
return _collection[position];
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (PositionOutOfCollectionException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _collection[position];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? obj)
|
public int Insert(T? obj)
|
||||||
{
|
{
|
||||||
if (Count >= _maxCount || obj == null)
|
if (obj == null) { return -1; }
|
||||||
|
|
||||||
|
try // выход за границы, курируется CollectionOverflowException
|
||||||
{
|
{
|
||||||
|
if (Count >= _maxCount)
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException(Count);
|
||||||
|
}
|
||||||
|
|
||||||
|
_collection.Add(obj);
|
||||||
|
return Count;
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_collection.Add(obj);
|
|
||||||
return Count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Insert(T? obj, int position)
|
public int Insert(T? obj, int position)
|
||||||
{
|
{
|
||||||
if (position >= _maxCount || Count >= _maxCount ||
|
try
|
||||||
position < 0 || _collection[position] != null
|
|
||||||
|| obj == null)
|
|
||||||
{
|
{
|
||||||
|
if (position < 0 || position >= _maxCount) throw new PositionOutOfCollectionException(position);
|
||||||
|
if (Count >= _maxCount) throw new CollectionOverflowException(Count);
|
||||||
|
|
||||||
|
if (obj == null) throw new ObjectNotFoundException(position);
|
||||||
|
|
||||||
|
_collection.Insert(position, obj);
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
catch (PositionOutOfCollectionException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_collection.Insert(position, obj);
|
|
||||||
return position;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public T? Remove(int position)
|
public T? Remove(int position)
|
||||||
{
|
{
|
||||||
if (position >= Count || position < 0)
|
try {
|
||||||
// on the other positions items don't exist
|
if (position >= _maxCount || position < 0)
|
||||||
|
// on the other positions items don't exist
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException(position);
|
||||||
|
}
|
||||||
|
|
||||||
|
T? item = _collection[position];
|
||||||
|
_collection.RemoveAt(position);
|
||||||
|
|
||||||
|
if (item == null) throw new ObjectNotFoundException(position);
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException ex)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
catch (ObjectNotFoundException ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
T? item = _collection[position];
|
|
||||||
_collection.RemoveAt(position);
|
|
||||||
return item;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<T?> GetItems()
|
public IEnumerable<T?> GetItems()
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using ProjectCruiser.DrawningSamples;
|
using ProjectCruiser.DrawningSamples;
|
||||||
|
using ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
namespace ProjectCruiser.CollectionGenericObj;
|
namespace ProjectCruiser.CollectionGenericObj;
|
||||||
|
|
||||||
@ -55,9 +56,11 @@ public class StorageCollection<T>
|
|||||||
/// <param name="filename">Путь и имя файла</param>
|
/// <param name="filename">Путь и имя файла</param>
|
||||||
/// <returns>true - сохранение прошло успешно,
|
/// <returns>true - сохранение прошло успешно,
|
||||||
/// false - ошибка при сохранении данных</returns>
|
/// false - ошибка при сохранении данных</returns>
|
||||||
public bool SaveData(string filename)
|
public void SaveData(string filename)
|
||||||
{
|
{
|
||||||
if (_storages.Count == 0) { return false; }
|
if (_storages.Count == 0)
|
||||||
|
throw new NullReferenceException(" > No existing collections to save");
|
||||||
|
|
||||||
if (File.Exists(filename)) { File.Delete(filename); }
|
if (File.Exists(filename)) { File.Delete(filename); }
|
||||||
|
|
||||||
StringBuilder sb = new();
|
StringBuilder sb = new();
|
||||||
@ -79,21 +82,6 @@ public class StorageCollection<T>
|
|||||||
foreach (T? item in pair.Value.GetItems())
|
foreach (T? item in pair.Value.GetItems())
|
||||||
{
|
{
|
||||||
string data = item?.GetDataForSave() ?? string.Empty;
|
string data = item?.GetDataForSave() ?? string.Empty;
|
||||||
|
|
||||||
/*
|
|
||||||
string n = item.GetType().Name;
|
|
||||||
string data = null;
|
|
||||||
|
|
||||||
if (n != null && n == "DrawningCruiser")
|
|
||||||
{
|
|
||||||
data = ExtentionDrShip.GetDataForSave(item);
|
|
||||||
}
|
|
||||||
else if (n != null && n == "DrawningBase")
|
|
||||||
{
|
|
||||||
data = ExtentionDrShip.GetDataForSave(item);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (string.IsNullOrEmpty(data))
|
if (string.IsNullOrEmpty(data))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
@ -107,7 +95,6 @@ public class StorageCollection<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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Создание коллекции по типу
|
// Создание коллекции по типу
|
||||||
@ -122,12 +109,9 @@ public class StorageCollection<T>
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Загрузка информации по кораблям в хранилище из файла
|
// Загрузка информации по кораблям в хранилище из файла
|
||||||
public bool LoadData(string filename)
|
public void LoadData(string filename)
|
||||||
{
|
{
|
||||||
if (!File.Exists(filename))
|
if (!File.Exists(filename)) throw new FileNotFoundException();
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string bufferTextFromFile = "";
|
string bufferTextFromFile = "";
|
||||||
|
|
||||||
@ -145,14 +129,9 @@ public class StorageCollection<T>
|
|||||||
new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
|
||||||
|
|
||||||
if (strs == null || strs.Length == 0)
|
if (strs == null || strs.Length == 0)
|
||||||
{
|
throw new NullReferenceException("> No data to decode");
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!strs[0].Equals(_collectionKey))
|
if (!strs[0].Equals(_collectionKey))
|
||||||
{
|
throw new InvalidDataException("> Incorrect data");
|
||||||
//если нет такой записи, то это не те данные
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] companies = new string[strs.Length - 1];
|
string[] companies = new string[strs.Length - 1];
|
||||||
for (int k = 1; k < strs.Length; k++)
|
for (int k = 1; k < strs.Length; k++)
|
||||||
@ -171,10 +150,9 @@ public class StorageCollection<T>
|
|||||||
}
|
}
|
||||||
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
CollectionType collectionType = (CollectionType)Enum.Parse(typeof(CollectionType), record[1]);
|
||||||
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
ICollectionGenObj<T>? collection = StorageCollection<T>.CreateCollection(collectionType);
|
||||||
|
|
||||||
if (collection == null)
|
if (collection == null)
|
||||||
{
|
throw new NullReferenceException("> Failed to create collection");
|
||||||
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,
|
||||||
@ -184,15 +162,20 @@ public class StorageCollection<T>
|
|||||||
{
|
{
|
||||||
if (elem?.CreateDrawningCar() is T ship)
|
if (elem?.CreateDrawningCar() is T ship)
|
||||||
{
|
{
|
||||||
if (collection.Insert(ship) == -1)
|
try
|
||||||
{
|
{
|
||||||
return false;
|
if (collection.Insert(ship) == -1)
|
||||||
|
throw new IndexOutOfRangeException(
|
||||||
|
"> Failed to add to collection : " + record[3]);
|
||||||
|
}
|
||||||
|
catch (CollectionOverflowException e)
|
||||||
|
{
|
||||||
|
throw new CollectionOverflowException("Collection overflowed", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_storages.Add(record[0], collection);
|
_storages.Add(record[0], collection);
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
ProjectCruiser/Exceptions/CollectionOverflowException.cs
Normal file
18
ProjectCruiser/Exceptions/CollectionOverflowException.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
namespace ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
|
// Класс, описывающий ошибку переполнения коллекции
|
||||||
|
[Serializable]
|
||||||
|
internal class CollectionOverflowException : ApplicationException
|
||||||
|
{
|
||||||
|
public CollectionOverflowException(int count)
|
||||||
|
: base("Possible accsess of collection is over : " + count) { }
|
||||||
|
public CollectionOverflowException() : base() { }
|
||||||
|
public CollectionOverflowException(string message) : base(message) { }
|
||||||
|
public CollectionOverflowException(string message, Exception exception) :
|
||||||
|
base(message, exception)
|
||||||
|
{ }
|
||||||
|
protected CollectionOverflowException(SerializationInfo info,
|
||||||
|
StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
||||||
|
|
16
ProjectCruiser/Exceptions/ObjectNotFoundException.cs
Normal file
16
ProjectCruiser/Exceptions/ObjectNotFoundException.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
namespace ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
|
// Класс, описывающий ошибку, что по указанной позиции нет элемента
|
||||||
|
[Serializable]
|
||||||
|
internal class ObjectNotFoundException : ApplicationException
|
||||||
|
{
|
||||||
|
public ObjectNotFoundException(int i)
|
||||||
|
: base("Didn't find obj on this position : " + i) { }
|
||||||
|
public ObjectNotFoundException() : base() { }
|
||||||
|
public ObjectNotFoundException(string message) : base(message) { }
|
||||||
|
public ObjectNotFoundException(string message, Exception exception)
|
||||||
|
: base(message, exception) { }
|
||||||
|
protected ObjectNotFoundException(SerializationInfo info,
|
||||||
|
StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
using System.Runtime.Serialization;
|
||||||
|
namespace ProjectCruiser.Exceptions;
|
||||||
|
|
||||||
|
// Класс, описывающий ошибку выхода за границы коллекции
|
||||||
|
[Serializable]
|
||||||
|
internal class PositionOutOfCollectionException : ApplicationException
|
||||||
|
{
|
||||||
|
public PositionOutOfCollectionException(int i)
|
||||||
|
: base("Out of collection boarder. Position : " + i) { }
|
||||||
|
public PositionOutOfCollectionException() : base() { }
|
||||||
|
public PositionOutOfCollectionException(string message) : base(message) { }
|
||||||
|
public PositionOutOfCollectionException(string message,
|
||||||
|
Exception exception) : base(message, exception) { }
|
||||||
|
protected PositionOutOfCollectionException(SerializationInfo info,
|
||||||
|
StreamingContext contex) : base(info, contex) { }
|
||||||
|
}
|
@ -1,17 +1,42 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace ProjectCruiser
|
namespace ProjectCruiser
|
||||||
{
|
{
|
||||||
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()
|
||||||
{
|
{
|
||||||
// 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 ServiceForm2());
|
ServiceCollection services = new();
|
||||||
|
ConfigureServices(services);
|
||||||
|
|
||||||
|
using ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||||
|
Application.Run(serviceProvider.GetRequiredService<ServiceForm2>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Êîíôèãóðàöèÿ ñåðâèñà DI
|
||||||
|
/// <param name="services"></param>
|
||||||
|
private static void ConfigureServices(ServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddSingleton<ServiceForm2>().AddLogging(option =>
|
||||||
|
{
|
||||||
|
option.SetMinimumLevel(LogLevel.Information);
|
||||||
|
// [*] option.AddSerilog("serilog.config");
|
||||||
|
option.AddSerilog("serilog.config");
|
||||||
|
|
||||||
|
// instead of :
|
||||||
|
// option.SetMinimumLevel(LogLevel.Information);
|
||||||
|
// option.AddNLog("nlog.config");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,4 +8,14 @@
|
|||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Serilog.Extensions.Logging" Version="8.0.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="serilog.config">
|
||||||
|
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
@ -1,5 +1,9 @@
|
|||||||
using ProjectCruiser.CollectionGenericObj;
|
using ProjectCruiser.CollectionGenericObj;
|
||||||
using ProjectCruiser.DrawningSamples;
|
using ProjectCruiser.DrawningSamples;
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
// using NLog.Extensions.Logging;
|
||||||
|
|
||||||
namespace ProjectCruiser;
|
namespace ProjectCruiser;
|
||||||
|
|
||||||
public partial class ServiceForm2 : Form
|
public partial class ServiceForm2 : Form
|
||||||
@ -9,10 +13,16 @@ public partial class ServiceForm2 : Form
|
|||||||
|
|
||||||
private readonly StorageCollection<DrawningBase> _storageCollection;
|
private readonly StorageCollection<DrawningBase> _storageCollection;
|
||||||
|
|
||||||
public ServiceForm2()
|
// Логер
|
||||||
|
private readonly ILogger _logger;
|
||||||
|
|
||||||
|
// Конструктор > logger
|
||||||
|
public ServiceForm2(ILogger<ServiceForm2> logger)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
_storageCollection = new();
|
_storageCollection = new();
|
||||||
|
_logger = logger;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Выбор компании
|
// Выбор компании
|
||||||
@ -71,27 +81,39 @@ public partial class ServiceForm2 : Form
|
|||||||
// Передача объекта в другую форму
|
// Передача объекта в другую форму
|
||||||
private void btnChooseforTest_Click(object sender, EventArgs e)
|
private void btnChooseforTest_Click(object sender, EventArgs e)
|
||||||
{
|
{
|
||||||
|
// Add EXCEPTIONS [!]
|
||||||
if (_company == null)
|
if (_company == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
DrawningBase? car = null;
|
DrawningBase? ship = null;
|
||||||
int counter = 100;
|
int counter = 100;
|
||||||
while (car == null)
|
while (ship == null)
|
||||||
{
|
{
|
||||||
car = _company.GetRandomObject();
|
try
|
||||||
counter--;
|
|
||||||
if (counter <= 0)
|
|
||||||
{
|
{
|
||||||
break;
|
ship = _company.GetRandomObject();
|
||||||
|
counter--;
|
||||||
|
|
||||||
|
if (counter <= 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine(ex.Message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if (car == null)
|
|
||||||
|
if (ship == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
OceanForm1 form = new() { SetShip = car };
|
OceanForm1 form = new() { SetShip = ship };
|
||||||
form.ShowDialog();
|
form.ShowDialog();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,15 +212,17 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.SaveData(saveFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.SaveData(saveFileDialog.FileName);
|
||||||
MessageBox.Show(" < Saved succesfully >",
|
MessageBox.Show(" < Saved succesfully >",
|
||||||
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Saving to file : {filename}", saveFileDialog.FileName);
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("< Failed to save >", "Result :",
|
MessageBox.Show(ex.Message, "Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,16 +232,20 @@ public partial class ServiceForm2 : Form
|
|||||||
{
|
{
|
||||||
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
||||||
{
|
{
|
||||||
if (_storageCollection.LoadData(openFileDialog.FileName))
|
try
|
||||||
{
|
{
|
||||||
|
_storageCollection.LoadData(openFileDialog.FileName);
|
||||||
MessageBox.Show(" < Loaded succesfully >",
|
MessageBox.Show(" < Loaded succesfully >",
|
||||||
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||||
|
_logger.LogInformation("Loading from file : {filename}", openFileDialog.FileName);
|
||||||
|
|
||||||
RefreshListBoxItems();
|
RefreshListBoxItems();
|
||||||
}
|
}
|
||||||
else
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
MessageBox.Show("< Failed to load >", "Result :",
|
MessageBox.Show("< Failed to load >" + ex.Message,
|
||||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
"Result :", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
_logger.LogError("< Error > : {Message}", ex.Message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
ProjectCruiser/serilog.config
Normal file
13
ProjectCruiser/serilog.config
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<configuration>
|
||||||
|
<serilog xmlns="http://www.serilog-project.org/schemas/Serilog.xsd"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
autoReload="true" internalLogLevel="Info">
|
||||||
|
<targets>
|
||||||
|
<target xsi:type="File" name="tofile" fileName="shiplog-${shortdate}.log"/>
|
||||||
|
</targets>
|
||||||
|
<rules>
|
||||||
|
<logger name="*" minlevel="Debug" writeTo="tofile"/>
|
||||||
|
</rules>
|
||||||
|
</serilog>
|
||||||
|
</configuration>
|
Loading…
Reference in New Issue
Block a user