From 20f2c7c7e797e36d6642f22bef1a26b62e348f75 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:26:55 +0600 Subject: [PATCH 01/16] changes in StringConverter --- DTLib.Network/FSP.cs | 18 +++++++++--------- DTLib.Network/Package.cs | 8 ++++---- DTLib/Extensions/BaseConverter.cs | 2 +- DTLib/Extensions/StringConverter.cs | 10 +++++++--- DTLib/Extensions/Unmanaged.cs | 2 +- DTLib/Filesystem/File.cs | 6 +++--- 6 files changed, 25 insertions(+), 21 deletions(-) diff --git a/DTLib.Network/FSP.cs b/DTLib.Network/FSP.cs index fae6fa2..d418497 100644 --- a/DTLib.Network/FSP.cs +++ b/DTLib.Network/FSP.cs @@ -23,8 +23,8 @@ public class FSP lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); - MainSocket.SendPackage("requesting file download".ToBytes()); - MainSocket.SendPackage(filePath_server.ToBytes()); + MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8)); + MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8)); } DownloadFile(filePath_client); } @@ -44,8 +44,8 @@ public class FSP lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); - MainSocket.SendPackage("requesting file download".ToBytes()); - MainSocket.SendPackage(filePath_server.ToBytes()); + MainSocket.SendPackage("requesting file download".ToBytes(StringConverter.UTF8)); + MainSocket.SendPackage(filePath_server.ToBytes(StringConverter.UTF8)); } return DownloadFileToMemory(); } @@ -65,8 +65,8 @@ public class FSP lock (MainSocket) { BytesDownloaded = 0; - Filesize = MainSocket.GetPackage().BytesToString().ToUInt(); - MainSocket.SendPackage("ready".ToBytes()); + Filesize = MainSocket.GetPackage().BytesToString(StringConverter.UTF8).ToUInt(); + MainSocket.SendPackage("ready".ToBytes(StringConverter.UTF8)); int packagesCount = 0; byte[] buffer = new byte[5120]; int fullPackagesCount = (Filesize / buffer.Length).Truncate(); @@ -89,7 +89,7 @@ public class FSP // получение остатка if ((Filesize - fileStream.Position) > 0) { - MainSocket.SendPackage("remain request".ToBytes()); + MainSocket.SendPackage("remain request".ToBytes(StringConverter.UTF8)); buffer = MainSocket.GetPackage(); BytesDownloaded += (uint)buffer.Length; fileStream.Write(buffer, 0, buffer.Length); @@ -109,7 +109,7 @@ public class FSP Filesize = File.GetSize(filePath).ToUInt(); lock (MainSocket) { - MainSocket.SendPackage(Filesize.ToString().ToBytes()); + MainSocket.SendPackage(Filesize.ToString().ToBytes(StringConverter.UTF8)); MainSocket.GetAnswer("ready"); byte[] buffer = new byte[5120]; int packagesCount = 0; @@ -142,7 +142,7 @@ public class FSP if (!dirOnServer.EndsWith(Путь.Разд)) dirOnServer += Путь.Разд; Debug("b", "downloading manifest <", "c", dirOnServer + "manifest.dtsod", "b", ">"); - var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString()); + var manifest = new DtsodV23(DownloadFileToMemory(dirOnServer + "manifest.dtsod").BytesToString(StringConverter.UTF8)); Debug("g", $"found {manifest.Values.Count} files in manifest"); var hasher = new Hasher(); foreach (string fileOnServer in manifest.Keys) diff --git a/DTLib.Network/Package.cs b/DTLib.Network/Package.cs index 7c94e6e..45a0d4b 100644 --- a/DTLib.Network/Package.cs +++ b/DTLib.Network/Package.cs @@ -37,19 +37,19 @@ public static class Package if (data.Length == 0) throw new Exception($"SendPackage() error: package has zero size"); var list = new List(); - byte[] packageSize = data.Length.ToBytes(); + byte[] packageSize = data.Length.IntToBytes(); if (packageSize.Length == 1) list.Add(0); list.AddRange(packageSize); list.AddRange(data); socket.Send(list.ToArray()); } - public static void SendPackage(this Socket socket, string data) => SendPackage(socket, data.ToBytes()); + public static void SendPackage(this Socket socket, string data) => SendPackage(socket, data.ToBytes(StringConverter.UTF8)); // получает пакет и выбрасывает исключение, если пакет не соответствует образцу public static void GetAnswer(this Socket socket, string answer) { - string rec = socket.GetPackage().BytesToString(); + string rec = socket.GetPackage().BytesToString(StringConverter.UTF8); if (rec != answer) throw new Exception($"GetAnswer() error: invalid answer: <{rec}>"); } @@ -59,5 +59,5 @@ public static class Package socket.SendPackage(request); return socket.GetPackage(); } - public static byte[] RequestPackage(this Socket socket, string request) => socket.RequestPackage(request.ToBytes()); + public static byte[] RequestPackage(this Socket socket, string request) => socket.RequestPackage(request.ToBytes(StringConverter.UTF8)); } diff --git a/DTLib/Extensions/BaseConverter.cs b/DTLib/Extensions/BaseConverter.cs index 5ce7712..74ec1d4 100644 --- a/DTLib/Extensions/BaseConverter.cs +++ b/DTLib/Extensions/BaseConverter.cs @@ -35,7 +35,7 @@ public static class BaseConverter return output; } - public static byte[] ToBytes(this int num) + public static byte[] IntToBytes(this int num) { List output = new(); while (num != 0) diff --git a/DTLib/Extensions/StringConverter.cs b/DTLib/Extensions/StringConverter.cs index a3ce624..5c55f7c 100644 --- a/DTLib/Extensions/StringConverter.cs +++ b/DTLib/Extensions/StringConverter.cs @@ -2,11 +2,10 @@ public static class StringConverter { - public static ASCIIEncoding ASCII = new ASCIIEncoding(); public static Encoding UTF8 = new UTF8Encoding(false); public static Encoding UTF8BOM = new UTF8Encoding(true); - public static byte[] ToBytes(this string str) => UTF8.GetBytes(str); - public static string BytesToString(this byte[] bytes) => UTF8.GetString(bytes); + public static byte[] ToBytes(this string str, Encoding encoding) => encoding.GetBytes(str); + public static string BytesToString(this byte[] bytes, Encoding encoding) => encoding.GetString(bytes); // хеш в виде массива байт в строку (хеш изначально не в кодировке UTF8, так что метод выше не работает с ним) public static string HashToString(this byte[] hash) @@ -51,6 +50,11 @@ public static class StringConverter builder.Append(parts[i]); return builder.ToString(); } + + // String.Join(string...) does some low-level memory manipulations, that are faster than StringBuilder + public static string MergeToString(string part0, params string[] parts) + =>string.Join(part0, parts); + public static string MergeToString(this IEnumerable collection, string separator) { StringBuilder builder = new(); diff --git a/DTLib/Extensions/Unmanaged.cs b/DTLib/Extensions/Unmanaged.cs index 2eaff37..39ec9b0 100644 --- a/DTLib/Extensions/Unmanaged.cs +++ b/DTLib/Extensions/Unmanaged.cs @@ -6,7 +6,7 @@ public static class Unmanaged { public static unsafe IntPtr StringToHGlobalUTF8(this string s) { - byte[] buf = s.ToBytes(); + byte[] buf = s.ToBytes(StringConverter.UTF8); int bl = buf.Length; byte* ptr=(byte*)Marshal.AllocHGlobal(bl + 1); for (int i = 0; i < bl; i++) diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index 8ed8912..756b5da 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -39,7 +39,7 @@ public static class File return output; } - public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString(); + public static string ReadAllText(string file) => ReadAllBytes(file).BytesToString(StringConverter.UTF8); public static void WriteAllBytes(string file, byte[] content) { @@ -48,7 +48,7 @@ public static class File stream.Close(); } - public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes()); + public static void WriteAllText(string file, string content) => WriteAllBytes(file, content.ToBytes(StringConverter.UTF8)); public static void AppendAllBytes(string file, byte[] content) { @@ -57,7 +57,7 @@ public static class File stream.Close(); } - public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes()); + public static void AppendAllText(string file, string content) => AppendAllBytes(file, content.ToBytes(StringConverter.UTF8)); public static System.IO.FileStream OpenRead(string file) => Exists(file) From a94dd767990b52f075769df3a0ee941b487a2e4a Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:27:43 +0600 Subject: [PATCH 02/16] new logging system --- DTLib.Logging/DTLib.Logging.csproj | 23 +++++++++ DTLib.Logging/Global.cs | 11 +++++ DTLib.Logging/LogFormats/DefaultLogFormat.cs | 28 +++++++++++ DTLib.Logging/LogFormats/ILogFormat.cs | 10 ++++ DTLib.Logging/LogSeverity.cs | 9 ++++ DTLib.Logging/Loggers/CompositeLogger.cs | 38 ++++++++++++++ DTLib.Logging/Loggers/ConsoleLogger.cs | 33 +++++++++++++ DTLib.Logging/Loggers/FileLogger.cs | 52 ++++++++++++++++++++ DTLib.Logging/Loggers/ILogger.cs | 9 ++++ DTLib.Logging/Microsoft/LoggerService.cs | 18 +++++++ DTLib.Logging/Microsoft/MyLoggerWrapper.cs | 38 ++++++++++++++ DTLib.Tests/DTLib.Tests.csproj | 1 + DTLib.Tests/Program.cs | 2 +- {DTLib/Logging => DTLib.Tests}/Tester.cs | 8 +-- DTLib.sln | 6 +++ 15 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 DTLib.Logging/DTLib.Logging.csproj create mode 100644 DTLib.Logging/Global.cs create mode 100644 DTLib.Logging/LogFormats/DefaultLogFormat.cs create mode 100644 DTLib.Logging/LogFormats/ILogFormat.cs create mode 100644 DTLib.Logging/LogSeverity.cs create mode 100644 DTLib.Logging/Loggers/CompositeLogger.cs create mode 100644 DTLib.Logging/Loggers/ConsoleLogger.cs create mode 100644 DTLib.Logging/Loggers/FileLogger.cs create mode 100644 DTLib.Logging/Loggers/ILogger.cs create mode 100644 DTLib.Logging/Microsoft/LoggerService.cs create mode 100644 DTLib.Logging/Microsoft/MyLoggerWrapper.cs rename {DTLib/Logging => DTLib.Tests}/Tester.cs (62%) diff --git a/DTLib.Logging/DTLib.Logging.csproj b/DTLib.Logging/DTLib.Logging.csproj new file mode 100644 index 0000000..4dc0bc5 --- /dev/null +++ b/DTLib.Logging/DTLib.Logging.csproj @@ -0,0 +1,23 @@ + + + + net6.0;net48 + 10 + disable + disable + true + embedded + False + Debug;Release + AnyCPU;x64;x86;arm64 + + + + + + + + + + + diff --git a/DTLib.Logging/Global.cs b/DTLib.Logging/Global.cs new file mode 100644 index 0000000..6f7c1f3 --- /dev/null +++ b/DTLib.Logging/Global.cs @@ -0,0 +1,11 @@ +global using System; +global using System.Collections; +global using System.Collections.Generic; +global using System.Linq; +global using System.Text; +global using System.Threading.Tasks; +global using DTLib.Extensions; +global using DTLib.Filesystem; + +namespace DTLib.Logging.New; + diff --git a/DTLib.Logging/LogFormats/DefaultLogFormat.cs b/DTLib.Logging/LogFormats/DefaultLogFormat.cs new file mode 100644 index 0000000..144f637 --- /dev/null +++ b/DTLib.Logging/LogFormats/DefaultLogFormat.cs @@ -0,0 +1,28 @@ +namespace DTLib.Logging.New; + +public class DefaultLogFormat : ILogFormat +{ + + public bool PrintTimeStamp { get; set; } + public bool PrintContext { get; set; } + public bool PrintSeverity { get; set; } + + public DefaultLogFormat(bool printTimeStamp = false, bool printContext = true, bool printSeverity = true) + { + PrintTimeStamp = printTimeStamp; + PrintContext = printContext; + PrintSeverity = printSeverity; + } + + public string CreateMessage(string context, LogSeverity severity, object message) + { + var sb = new StringBuilder(); + if (PrintTimeStamp) sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']'); + if(PrintContext) sb.Append('[').Append(context).Append(']'); + if(PrintSeverity) sb.Append('[').Append(severity.ToString()).Append(']'); + if (sb.Length != 0) sb.Append(": "); + sb.Append(message.ToString()); + sb.Append('\n'); + return sb.ToString(); + } +} \ No newline at end of file diff --git a/DTLib.Logging/LogFormats/ILogFormat.cs b/DTLib.Logging/LogFormats/ILogFormat.cs new file mode 100644 index 0000000..3b1834f --- /dev/null +++ b/DTLib.Logging/LogFormats/ILogFormat.cs @@ -0,0 +1,10 @@ +namespace DTLib.Logging.New; + +public interface ILogFormat +{ + bool PrintTimeStamp { get; set; } + bool PrintContext { get; set; } + bool PrintSeverity { get; set; } + + string CreateMessage(string context, LogSeverity severity, object message); +} \ No newline at end of file diff --git a/DTLib.Logging/LogSeverity.cs b/DTLib.Logging/LogSeverity.cs new file mode 100644 index 0000000..dd8ddec --- /dev/null +++ b/DTLib.Logging/LogSeverity.cs @@ -0,0 +1,9 @@ +namespace DTLib.Logging.New; + +public enum LogSeverity +{ + Debug=1, + Info=2, + Warn=4, + Error=8 +} \ No newline at end of file diff --git a/DTLib.Logging/Loggers/CompositeLogger.cs b/DTLib.Logging/Loggers/CompositeLogger.cs new file mode 100644 index 0000000..d6a0c0c --- /dev/null +++ b/DTLib.Logging/Loggers/CompositeLogger.cs @@ -0,0 +1,38 @@ +namespace DTLib.Logging.New; + +/// +/// This class can be used for unite many loggers into one +/// +public class CompositeLogger : ILogger +{ + public ILogFormat Format { get; } + protected ILogger[] _loggers; + + public CompositeLogger(ILogFormat format, params ILogger[] loggers) + { + Format = format; + _loggers = loggers; + } + + public CompositeLogger(params ILogger[] loggers) : this(new DefaultLogFormat(), loggers) + {} + + + public void Log(string context, LogSeverity severity, object message, ILogFormat format) + { + for (int i = 0; i < _loggers.Length; i++) + _loggers[i].Log(context, severity, message, format); + } + + public void Log(string context, LogSeverity severity, object message) + => Log(context, severity, message, Format); + + + public void Dispose() + { + for (int i = 0; i < _loggers.Length; i++) + { + _loggers[i].Dispose(); + } + } +} \ No newline at end of file diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs new file mode 100644 index 0000000..6f9e660 --- /dev/null +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -0,0 +1,33 @@ +namespace DTLib.Logging.New; + +// вывод лога в консоль и файл +public class ConsoleLogger : ILogger +{ + readonly object consolelocker = new(); + public ILogFormat Format { get; } + + public ConsoleLogger(ILogFormat format) + => Format = format; + + public ConsoleLogger() : this(new DefaultLogFormat()) + {} + + + public void Log(string context, LogSeverity severity, object message, ILogFormat format) + { + var msg = format.CreateMessage(context, severity, message); + lock (consolelocker) + ColoredConsole.Write(msg); + } + + public void Log(string context, LogSeverity severity, object message) + => Log(context, severity, message, Format); + + + public void Dispose() + { + lock (consolelocker) {} + } + + ~ConsoleLogger() => Dispose(); +} diff --git a/DTLib.Logging/Loggers/FileLogger.cs b/DTLib.Logging/Loggers/FileLogger.cs new file mode 100644 index 0000000..bfd1de7 --- /dev/null +++ b/DTLib.Logging/Loggers/FileLogger.cs @@ -0,0 +1,52 @@ +namespace DTLib.Logging.New; + +public class FileLogger : ILogger +{ + public ILogFormat Format { get; } + + public string LogfileName { get; protected set; } + public System.IO.FileStream LogfileStream { get; protected set; } + + public FileLogger(string logfile, ILogFormat format) + { + Format = format; + LogfileName = logfile; + LogfileStream = File.OpenAppend(logfile); + } + + public FileLogger(string logfile) : this(logfile, new DefaultLogFormat()) + {} + + public FileLogger(string dir, string programName, ILogFormat format) + : this($"{dir}{Путь.Разд}{programName}_{DateTime.Now.ToString(MyTimeFormat.ForFileNames)}.log", format) + {} + + public FileLogger(string dir, string programName) : this(dir, programName, new DefaultLogFormat()) + {} + + public void Log(string context, LogSeverity severity, object message, ILogFormat format) + { + var msg = format.CreateMessage(context, severity, format).ToBytes(StringConverter.UTF8); + lock (LogfileStream) + { + LogfileStream.Write(msg); + LogfileStream.Flush(); + } + } + + public void Log(string context, LogSeverity severity, object message) + => Log(context, severity, message, Format); + + public virtual void Dispose() + { + try + { + LogfileStream?.Flush(); + LogfileStream?.Close(); + LogfileStream?.Dispose(); + } + catch (ObjectDisposedException) { } + } + + ~FileLogger() => Dispose(); +} diff --git a/DTLib.Logging/Loggers/ILogger.cs b/DTLib.Logging/Loggers/ILogger.cs new file mode 100644 index 0000000..3fbbdfb --- /dev/null +++ b/DTLib.Logging/Loggers/ILogger.cs @@ -0,0 +1,9 @@ +namespace DTLib.Logging.New; + +public interface ILogger : IDisposable +{ + + ILogFormat Format { get; } + void Log(string context, LogSeverity severity, object message); + void Log(string context, LogSeverity severity, object message, ILogFormat format); +} \ No newline at end of file diff --git a/DTLib.Logging/Microsoft/LoggerService.cs b/DTLib.Logging/Microsoft/LoggerService.cs new file mode 100644 index 0000000..e24b827 --- /dev/null +++ b/DTLib.Logging/Microsoft/LoggerService.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; + +namespace DTLib.Logging.New.Microsoft; + +public class LoggerService : IServiceProvider +{ + ILogger _logger; + + public LoggerService(ILogger logger) + { + _logger = logger; + } + + public object GetService(Type serviceType) + { + return new MyLoggerWrapper(_logger); + } +} \ No newline at end of file diff --git a/DTLib.Logging/Microsoft/MyLoggerWrapper.cs b/DTLib.Logging/Microsoft/MyLoggerWrapper.cs new file mode 100644 index 0000000..1295d6d --- /dev/null +++ b/DTLib.Logging/Microsoft/MyLoggerWrapper.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.Logging; + +namespace DTLib.Logging.New.Microsoft; + +internal class MyLoggerWrapper : ILogger +{ + private ILogger _logger; + public MyLoggerWrapper(ILogger logger)=> + _logger = logger; + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + { + string message = formatter(state, exception); + _logger.Log(nameof(TCaller), LogSeverity_FromLogLevel(logLevel), message); + } + + private bool _isEnabled=true; + public bool IsEnabled(LogLevel logLevel) => _isEnabled; + + public IDisposable BeginScope(TState state) + { + throw new NotImplementedException(); + } + + static LogSeverity LogSeverity_FromLogLevel(LogLevel l) + => l switch + { + LogLevel.Trace => LogSeverity.Debug, + LogLevel.Debug => LogSeverity.Debug, + LogLevel.Information => LogSeverity.Info, + LogLevel.Warning => LogSeverity.Warn, + LogLevel.Error => LogSeverity.Error, + LogLevel.Critical => LogSeverity.Error, + LogLevel.None => throw new NotImplementedException("LogLevel.None is not supported"), + _ => throw new ArgumentOutOfRangeException(nameof(l), l, null) + } + ; +} \ No newline at end of file diff --git a/DTLib.Tests/DTLib.Tests.csproj b/DTLib.Tests/DTLib.Tests.csproj index cce9ae1..240d82e 100644 --- a/DTLib.Tests/DTLib.Tests.csproj +++ b/DTLib.Tests/DTLib.Tests.csproj @@ -21,6 +21,7 @@ + diff --git a/DTLib.Tests/Program.cs b/DTLib.Tests/Program.cs index de48fb1..a30fcb4 100644 --- a/DTLib.Tests/Program.cs +++ b/DTLib.Tests/Program.cs @@ -8,7 +8,7 @@ global using DTLib; global using DTLib.Extensions; global using DTLib.Filesystem; global using DTLib.Dtsod; -global using static DTLib.Logging.Tester; +global using static DTLib.Tests.TesterLog; global using static DTLib.Tests.Program; using DTLib.Logging; diff --git a/DTLib/Logging/Tester.cs b/DTLib.Tests/Tester.cs similarity index 62% rename from DTLib/Logging/Tester.cs rename to DTLib.Tests/Tester.cs index 36709e9..d868e1a 100644 --- a/DTLib/Logging/Tester.cs +++ b/DTLib.Tests/Tester.cs @@ -1,9 +1,9 @@ using System.Diagnostics; -using System.Globalization; +using DTLib.Logging; -namespace DTLib.Logging; +namespace DTLib.Tests; -public static class Tester +public static class TesterLog { public static void LogOperationTime(string op_name, int repeats, Action operation) { @@ -13,6 +13,6 @@ public static class Tester operation(); clock.Stop(); double time=(double)(clock.ElapsedTicks)/Stopwatch.Frequency/repeats; - Log("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(MyTimeFormat.ForText),"y"," seconds"); + PublicLog.Log("y",$"operation ","b",op_name,"y"," lasted ","b",time.ToString(MyTimeFormat.ForText),"y"," seconds"); } } \ No newline at end of file diff --git a/DTLib.sln b/DTLib.sln index d8569c9..b461fe0 100644 --- a/DTLib.sln +++ b/DTLib.sln @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Dtsod", "DTLib.Dtsod\ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Network", "DTLib.Network\DTLib.Network.csproj", "{24B7D0A2-0462-424D-B3F5-29A6655FE472}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Logging", "DTLib.Logging\DTLib.Logging.csproj", "{00B76172-32BB-4B72-9891-47FAEF63386C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {24B7D0A2-0462-424D-B3F5-29A6655FE472}.Debug|Any CPU.Build.0 = Debug|Any CPU {24B7D0A2-0462-424D-B3F5-29A6655FE472}.Release|Any CPU.ActiveCfg = Release|Any CPU {24B7D0A2-0462-424D-B3F5-29A6655FE472}.Release|Any CPU.Build.0 = Release|Any CPU + {00B76172-32BB-4B72-9891-47FAEF63386C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {00B76172-32BB-4B72-9891-47FAEF63386C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {00B76172-32BB-4B72-9891-47FAEF63386C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {00B76172-32BB-4B72-9891-47FAEF63386C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 75982124dc462bdb22144201e284a38c9356ed80 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:28:36 +0600 Subject: [PATCH 03/16] old FileLogger updated --- DTLib/Logging/FileLogger.cs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/DTLib/Logging/FileLogger.cs b/DTLib/Logging/FileLogger.cs index 3d65ee8..eb674aa 100644 --- a/DTLib/Logging/FileLogger.cs +++ b/DTLib/Logging/FileLogger.cs @@ -1,6 +1,4 @@ -using System.Globalization; - -namespace DTLib.Logging; +namespace DTLib.Logging; public class FileLogger : IDisposable { @@ -24,16 +22,16 @@ public class FileLogger : IDisposable { LastLogMessageTime = DateTime.Now.ToString(MyTimeFormat.ForText); LogfileStream.WriteByte('['.ToByte()); - LogfileStream.Write(LastLogMessageTime.ToBytes()); - LogfileStream.Write("]: ".ToBytes()); + LogfileStream.Write(LastLogMessageTime.ToBytes(StringConverter.UTF8)); + LogfileStream.Write("]: ".ToBytes(StringConverter.UTF8)); if (msg.Length == 1) - LogfileStream.Write(msg[0].ToBytes()); + LogfileStream.Write(msg[0].ToBytes(StringConverter.UTF8)); else { var strb = new StringBuilder(); for (ushort i = 1; i < msg.Length; i += 2) strb.Append(msg[i]); - LogfileStream.Write(strb.ToString().ToBytes()); + LogfileStream.Write(strb.ToString().ToBytes(StringConverter.UTF8)); } LogfileStream.WriteByte('\n'.ToByte()); LogfileStream.Flush(); From 7223cc4b3c9748248a333c3c01d07df1260475f3 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:48:14 +0600 Subject: [PATCH 04/16] added Ben.Demystifier submodule --- .gitmodules | 3 +++ DTLib.Logging/DTLib.Logging.csproj | 11 +++++++---- DTLib.sln | 6 ++++++ 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e231db6 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "DTLib.Logging/Ben.Demystifier"] + path = DTLib.Logging/Ben.Demystifier + url = https://github.com/Timerix22/Ben.Demystifier.git diff --git a/DTLib.Logging/DTLib.Logging.csproj b/DTLib.Logging/DTLib.Logging.csproj index 4dc0bc5..9fbb931 100644 --- a/DTLib.Logging/DTLib.Logging.csproj +++ b/DTLib.Logging/DTLib.Logging.csproj @@ -13,11 +13,14 @@ - + - - + + + + + + - diff --git a/DTLib.sln b/DTLib.sln index b461fe0..f24c434 100644 --- a/DTLib.sln +++ b/DTLib.sln @@ -19,6 +19,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Network", "DTLib.Netw EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Logging", "DTLib.Logging\DTLib.Logging.csproj", "{00B76172-32BB-4B72-9891-47FAEF63386C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ben.Demystifier", "DTLib.Logging\Ben.Demystifier\src\Ben.Demystifier\Ben.Demystifier.csproj", "{AC7CB524-4D59-42E0-9F96-1C201A92494B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {00B76172-32BB-4B72-9891-47FAEF63386C}.Debug|Any CPU.Build.0 = Debug|Any CPU {00B76172-32BB-4B72-9891-47FAEF63386C}.Release|Any CPU.ActiveCfg = Release|Any CPU {00B76172-32BB-4B72-9891-47FAEF63386C}.Release|Any CPU.Build.0 = Release|Any CPU + {AC7CB524-4D59-42E0-9F96-1C201A92494B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC7CB524-4D59-42E0-9F96-1C201A92494B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC7CB524-4D59-42E0-9F96-1C201A92494B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC7CB524-4D59-42E0-9F96-1C201A92494B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 44c3b19927621723fe39f9b0728c5ee6400ad586 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:48:39 +0600 Subject: [PATCH 05/16] netstandard2.0 --- DTLib.Logging/Ben.Demystifier | 1 + 1 file changed, 1 insertion(+) create mode 160000 DTLib.Logging/Ben.Demystifier diff --git a/DTLib.Logging/Ben.Demystifier b/DTLib.Logging/Ben.Demystifier new file mode 160000 index 0000000..f41b890 --- /dev/null +++ b/DTLib.Logging/Ben.Demystifier @@ -0,0 +1 @@ +Subproject commit f41b89059a2a5b943e0a25564d49e93dc9ee188a From 355b7140c86c8ce2f1952fc77116d0eae81aad62 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 18:51:27 +0600 Subject: [PATCH 06/16] Ben.Demystifier --- DTLib.Logging/Ben.Demystifier | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DTLib.Logging/Ben.Demystifier b/DTLib.Logging/Ben.Demystifier index f41b890..f64d655 160000 --- a/DTLib.Logging/Ben.Demystifier +++ b/DTLib.Logging/Ben.Demystifier @@ -1 +1 @@ -Subproject commit f41b89059a2a5b943e0a25564d49e93dc9ee188a +Subproject commit f64d655f490602b5858baeed3572ea2a9b850013 From db18bbe790145546fa7bc8cb85cd745e1d22296a Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 19:11:14 +0600 Subject: [PATCH 07/16] LoggerService --- DTLib.Logging/DTLib.Logging.csproj | 2 +- DTLib.Logging/Microsoft/LoggerService.cs | 11 +++-------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/DTLib.Logging/DTLib.Logging.csproj b/DTLib.Logging/DTLib.Logging.csproj index 9fbb931..8805129 100644 --- a/DTLib.Logging/DTLib.Logging.csproj +++ b/DTLib.Logging/DTLib.Logging.csproj @@ -13,7 +13,7 @@ - + diff --git a/DTLib.Logging/Microsoft/LoggerService.cs b/DTLib.Logging/Microsoft/LoggerService.cs index e24b827..0845964 100644 --- a/DTLib.Logging/Microsoft/LoggerService.cs +++ b/DTLib.Logging/Microsoft/LoggerService.cs @@ -1,18 +1,13 @@ -using Microsoft.Extensions.Logging; +using Microsoft.Extensions.DependencyInjection; namespace DTLib.Logging.New.Microsoft; -public class LoggerService : IServiceProvider +public class LoggerService : ServiceDescriptor { ILogger _logger; - public LoggerService(ILogger logger) + public LoggerService(DTLib.Logging.New.ILogger logger) : base( typeof(ILogger), new MyLoggerWrapper(logger)) { _logger = logger; } - - public object GetService(Type serviceType) - { - return new MyLoggerWrapper(_logger); - } } \ No newline at end of file From 73979336011c8eabfc23afcf2c5e6ef10eccb629 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 21:19:01 +0600 Subject: [PATCH 08/16] added colors to ConsoleLogger --- DTLib.Logging/Loggers/ConsoleLogger.cs | 11 ++++- DTLib.sln | 3 +- DTLib/ColoredConsole.cs | 56 +++++++++++++------------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs index 6f9e660..1674f7a 100644 --- a/DTLib.Logging/Loggers/ConsoleLogger.cs +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -17,12 +17,21 @@ public class ConsoleLogger : ILogger { var msg = format.CreateMessage(context, severity, message); lock (consolelocker) - ColoredConsole.Write(msg); + ColoredConsole.Write(ColorFromSeverity(severity),msg); } public void Log(string context, LogSeverity severity, object message) => Log(context, severity, message, Format); + private static ConsoleColor ColorFromSeverity(LogSeverity severity) + => severity switch + { + LogSeverity.Debug => ConsoleColor.Gray, + LogSeverity.Info => ConsoleColor.White, + LogSeverity.Warn => ConsoleColor.Yellow, + LogSeverity.Error => ConsoleColor.Red, + _ => throw new ArgumentOutOfRangeException(nameof(severity), severity, null) + }; public void Dispose() { diff --git a/DTLib.sln b/DTLib.sln index f24c434..15aec6c 100644 --- a/DTLib.sln +++ b/DTLib.sln @@ -9,8 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTLib.Tests", "DTLib.Tests\ EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6308F24E-A4FF-46B3-B72F-30E05DDCB1D5}" ProjectSection(SolutionItems) = preProject - .gitignore = .gitignore README.md = README.md + .gitignore = .gitignore + .gitmodules = .gitmodules EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTLib.Dtsod", "DTLib.Dtsod\DTLib.Dtsod.csproj", "{ADE425F5-8645-47F0-9AA8-33FA748D36BE}" diff --git a/DTLib/ColoredConsole.cs b/DTLib/ColoredConsole.cs index b23f61b..44209e6 100644 --- a/DTLib/ColoredConsole.cs +++ b/DTLib/ColoredConsole.cs @@ -30,36 +30,36 @@ public static class ColoredConsole _ => throw new Exception($"ColoredConsole.ParseColor({color}) error: incorrect color"), }; + public static void Write(ConsoleColor color,string msg) + { + Console.ForegroundColor = color; + Console.Write(msg); + Console.ForegroundColor = ConsoleColor.Gray; + } + + public static void Write(string msg) => Write(ConsoleColor.Gray, msg); + // вывод цветного текста public static void Write(params string[] input) { - if (input.Length == 1) + if (input.Length % 2 != 0) + throw new Exception("ColoredConsole.Write() error: every text string must have color string before"); + + for (ushort i = 0; i < input.Length; i++) { - if (Console.ForegroundColor != ConsoleColor.Gray) - Console.ForegroundColor = ConsoleColor.Gray; - Console.Write(input[0]); + Console.ForegroundColor = ParseColor(input[i++]); + Console.Write(input[i]); } - else if (input.Length % 2 == 0) - { - StringBuilder strB = new(); - for (ushort i = 0; i < input.Length; i++) - { - ConsoleColor c = ParseColor(input[i]); - if (Console.ForegroundColor != c) - { - Console.Write(strB.ToString()); - Console.ForegroundColor = c; - strB.Clear(); - } - strB.Append(input[++i]); - } - if (strB.Length > 0) - Console.Write(strB.ToString()); - } - else throw new Exception("ColoredConsole.Write() error: every text string must have color string before"); + Console.ForegroundColor = ConsoleColor.Gray; } public static void WriteLine() => Console.WriteLine(); + public static void WriteLine(ConsoleColor color,string msg) + { + Console.ForegroundColor = color; + Console.WriteLine(msg); + Console.ForegroundColor = ConsoleColor.Gray; + } public static void WriteLine(params string[] input) { @@ -68,11 +68,13 @@ public static class ColoredConsole } // ввод цветного текста - public static string Read(string color) + public static string Read(ConsoleColor color) { - ConsoleColor c = ParseColor(color); - if (Console.ForegroundColor != c) - Console.ForegroundColor = c; - return Console.ReadLine(); + Console.ForegroundColor = color; + var r = Console.ReadLine(); + Console.ForegroundColor = ConsoleColor.Gray; + return r; } + + public static string Read(string color) => Read(ParseColor(color)); } From b3d8757afc3eb157996592feeddcb5004bc41a3b Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 21:35:20 +0600 Subject: [PATCH 09/16] LogFormat --- DTLib.Logging/DTLib.Logging.csproj | 2 +- DTLib.Logging/LogFormats/DefaultLogFormat.cs | 14 ++++++++++---- DTLib.Logging/Microsoft/MyLoggerWrapper.cs | 6 +++--- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/DTLib.Logging/DTLib.Logging.csproj b/DTLib.Logging/DTLib.Logging.csproj index 8805129..ff0927c 100644 --- a/DTLib.Logging/DTLib.Logging.csproj +++ b/DTLib.Logging/DTLib.Logging.csproj @@ -13,7 +13,7 @@ - + diff --git a/DTLib.Logging/LogFormats/DefaultLogFormat.cs b/DTLib.Logging/LogFormats/DefaultLogFormat.cs index 144f637..21cf0d9 100644 --- a/DTLib.Logging/LogFormats/DefaultLogFormat.cs +++ b/DTLib.Logging/LogFormats/DefaultLogFormat.cs @@ -17,10 +17,16 @@ public class DefaultLogFormat : ILogFormat public string CreateMessage(string context, LogSeverity severity, object message) { var sb = new StringBuilder(); - if (PrintTimeStamp) sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']'); - if(PrintContext) sb.Append('[').Append(context).Append(']'); - if(PrintSeverity) sb.Append('[').Append(severity.ToString()).Append(']'); - if (sb.Length != 0) sb.Append(": "); + if (PrintTimeStamp) + sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']'); + if (PrintContext && PrintSeverity) + sb.Append('[').Append(context).Append('/').Append(severity.ToString().Append(']')); + else if(PrintContext) + sb.Append('[').Append(context).Append(']'); + else if(PrintSeverity) + sb.Append('[').Append(severity.ToString()).Append(']'); + if (sb.Length != 0) + sb.Append(": "); sb.Append(message.ToString()); sb.Append('\n'); return sb.ToString(); diff --git a/DTLib.Logging/Microsoft/MyLoggerWrapper.cs b/DTLib.Logging/Microsoft/MyLoggerWrapper.cs index 1295d6d..251f96b 100644 --- a/DTLib.Logging/Microsoft/MyLoggerWrapper.cs +++ b/DTLib.Logging/Microsoft/MyLoggerWrapper.cs @@ -2,10 +2,10 @@ using Microsoft.Extensions.Logging; namespace DTLib.Logging.New.Microsoft; -internal class MyLoggerWrapper : ILogger +public class MyLoggerWrapper : global::Microsoft.Extensions.Logging.ILogger { - private ILogger _logger; - public MyLoggerWrapper(ILogger logger)=> + private DTLib.Logging.New.ILogger _logger; + public MyLoggerWrapper(DTLib.Logging.New.ILogger logger)=> _logger = logger; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) From f7175c4c2c0965779134536dd54012da291bca32 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 21:41:30 +0600 Subject: [PATCH 10/16] Microsoft -> DependencyInjection --- .../{Microsoft => DependencyInjection}/LoggerService.cs | 8 +++++--- .../{Microsoft => DependencyInjection}/MyLoggerWrapper.cs | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) rename DTLib.Logging/{Microsoft => DependencyInjection}/LoggerService.cs (50%) rename DTLib.Logging/{Microsoft => DependencyInjection}/MyLoggerWrapper.cs (86%) diff --git a/DTLib.Logging/Microsoft/LoggerService.cs b/DTLib.Logging/DependencyInjection/LoggerService.cs similarity index 50% rename from DTLib.Logging/Microsoft/LoggerService.cs rename to DTLib.Logging/DependencyInjection/LoggerService.cs index 0845964..c544609 100644 --- a/DTLib.Logging/Microsoft/LoggerService.cs +++ b/DTLib.Logging/DependencyInjection/LoggerService.cs @@ -1,12 +1,14 @@ using Microsoft.Extensions.DependencyInjection; -namespace DTLib.Logging.New.Microsoft; +namespace DTLib.Logging.DependencyInjection; public class LoggerService : ServiceDescriptor { - ILogger _logger; + DTLib.Logging.New.ILogger _logger; - public LoggerService(DTLib.Logging.New.ILogger logger) : base( typeof(ILogger), new MyLoggerWrapper(logger)) + public LoggerService(DTLib.Logging.New.ILogger logger) : base( + typeof(Microsoft.Extensions.Logging.ILogger), + new MyLoggerWrapper(logger)) { _logger = logger; } diff --git a/DTLib.Logging/Microsoft/MyLoggerWrapper.cs b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs similarity index 86% rename from DTLib.Logging/Microsoft/MyLoggerWrapper.cs rename to DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs index 251f96b..d095122 100644 --- a/DTLib.Logging/Microsoft/MyLoggerWrapper.cs +++ b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs @@ -1,8 +1,9 @@ +using DTLib.Logging.New; using Microsoft.Extensions.Logging; -namespace DTLib.Logging.New.Microsoft; +namespace DTLib.Logging.DependencyInjection; -public class MyLoggerWrapper : global::Microsoft.Extensions.Logging.ILogger +public class MyLoggerWrapper : Microsoft.Extensions.Logging.ILogger { private DTLib.Logging.New.ILogger _logger; public MyLoggerWrapper(DTLib.Logging.New.ILogger logger)=> From 63dd4ae25c8c7f4aeffcd65ed1ebd5f7acc3faea Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 21:45:52 +0600 Subject: [PATCH 11/16] format fix --- DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs | 2 +- DTLib.Logging/LogFormats/DefaultLogFormat.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs index d095122..b685344 100644 --- a/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs +++ b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs @@ -12,7 +12,7 @@ public class MyLoggerWrapper : Microsoft.Extensions.Logging.ILogger(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { string message = formatter(state, exception); - _logger.Log(nameof(TCaller), LogSeverity_FromLogLevel(logLevel), message); + _logger.Log(typeof(TCaller).Name, LogSeverity_FromLogLevel(logLevel), message); } private bool _isEnabled=true; diff --git a/DTLib.Logging/LogFormats/DefaultLogFormat.cs b/DTLib.Logging/LogFormats/DefaultLogFormat.cs index 21cf0d9..a51344f 100644 --- a/DTLib.Logging/LogFormats/DefaultLogFormat.cs +++ b/DTLib.Logging/LogFormats/DefaultLogFormat.cs @@ -20,7 +20,7 @@ public class DefaultLogFormat : ILogFormat if (PrintTimeStamp) sb.Append('[').Append(DateTime.Now.ToString(MyTimeFormat.ForText)).Append(']'); if (PrintContext && PrintSeverity) - sb.Append('[').Append(context).Append('/').Append(severity.ToString().Append(']')); + sb.Append('[').Append(context).Append('/').Append(severity.ToString()).Append(']'); else if(PrintContext) sb.Append('[').Append(context).Append(']'); else if(PrintSeverity) From 0b5ba1a6e7b9130e115e634f5fae183e38a5cdd3 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 22:07:15 +0600 Subject: [PATCH 12/16] Severity checking --- .../DependencyInjection/MyLoggerWrapper.cs | 6 +++--- DTLib.Logging/LogSeverity.cs | 21 +++++++++++++++---- DTLib.Logging/Loggers/CompositeLogger.cs | 8 +++++++ DTLib.Logging/Loggers/ConsoleLogger.cs | 10 ++++++++- DTLib.Logging/Loggers/FileLogger.cs | 7 +++++++ DTLib.Logging/Loggers/ILogger.cs | 6 ++++++ 6 files changed, 50 insertions(+), 8 deletions(-) diff --git a/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs index b685344..98bbbef 100644 --- a/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs +++ b/DTLib.Logging/DependencyInjection/MyLoggerWrapper.cs @@ -5,14 +5,14 @@ namespace DTLib.Logging.DependencyInjection; public class MyLoggerWrapper : Microsoft.Extensions.Logging.ILogger { - private DTLib.Logging.New.ILogger _logger; + public DTLib.Logging.New.ILogger Logger; public MyLoggerWrapper(DTLib.Logging.New.ILogger logger)=> - _logger = logger; + Logger = logger; public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) { string message = formatter(state, exception); - _logger.Log(typeof(TCaller).Name, LogSeverity_FromLogLevel(logLevel), message); + Logger.Log(typeof(TCaller).Name, LogSeverity_FromLogLevel(logLevel), message); } private bool _isEnabled=true; diff --git a/DTLib.Logging/LogSeverity.cs b/DTLib.Logging/LogSeverity.cs index dd8ddec..81d3d67 100644 --- a/DTLib.Logging/LogSeverity.cs +++ b/DTLib.Logging/LogSeverity.cs @@ -2,8 +2,21 @@ namespace DTLib.Logging.New; public enum LogSeverity { - Debug=1, - Info=2, - Warn=4, - Error=8 + Debug, + Info, + Warn, + Error +} + +internal static class LogSeverityHelper +{ + public static bool CheckSeverity(this ILogger logger, LogSeverity severity) + => severity switch + { + LogSeverity.Debug => logger.DebugLogEnabled, + LogSeverity.Info => logger.InfoLogEnabled, + LogSeverity.Warn => logger.WarnLogEnabled, + LogSeverity.Error => logger.ErrorLogenabled, + _ => throw new ArgumentOutOfRangeException(nameof(severity), severity, "unknown severity") + }; } \ No newline at end of file diff --git a/DTLib.Logging/Loggers/CompositeLogger.cs b/DTLib.Logging/Loggers/CompositeLogger.cs index d6a0c0c..2b3a3e2 100644 --- a/DTLib.Logging/Loggers/CompositeLogger.cs +++ b/DTLib.Logging/Loggers/CompositeLogger.cs @@ -5,7 +5,12 @@ namespace DTLib.Logging.New; /// public class CompositeLogger : ILogger { + public bool DebugLogEnabled { get; set; } = true; + public bool InfoLogEnabled { get; set; } = true; + public bool WarnLogEnabled { get; set; } = true; + public bool ErrorLogenabled { get; set; } = true; public ILogFormat Format { get; } + protected ILogger[] _loggers; public CompositeLogger(ILogFormat format, params ILogger[] loggers) @@ -20,6 +25,9 @@ public class CompositeLogger : ILogger public void Log(string context, LogSeverity severity, object message, ILogFormat format) { + if(!this.CheckSeverity(severity)) + return; + for (int i = 0; i < _loggers.Length; i++) _loggers[i].Log(context, severity, message, format); } diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs index 1674f7a..7a249cf 100644 --- a/DTLib.Logging/Loggers/ConsoleLogger.cs +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -3,8 +3,13 @@ // вывод лога в консоль и файл public class ConsoleLogger : ILogger { - readonly object consolelocker = new(); + public bool DebugLogEnabled { get; set; } = true; + public bool InfoLogEnabled { get; set; } = true; + public bool WarnLogEnabled { get; set; } = true; + public bool ErrorLogenabled { get; set; } = true; public ILogFormat Format { get; } + + readonly object consolelocker = new(); public ConsoleLogger(ILogFormat format) => Format = format; @@ -15,6 +20,9 @@ public class ConsoleLogger : ILogger public void Log(string context, LogSeverity severity, object message, ILogFormat format) { + if(!this.CheckSeverity(severity)) + return; + var msg = format.CreateMessage(context, severity, message); lock (consolelocker) ColoredConsole.Write(ColorFromSeverity(severity),msg); diff --git a/DTLib.Logging/Loggers/FileLogger.cs b/DTLib.Logging/Loggers/FileLogger.cs index bfd1de7..ae5cfff 100644 --- a/DTLib.Logging/Loggers/FileLogger.cs +++ b/DTLib.Logging/Loggers/FileLogger.cs @@ -2,6 +2,10 @@ public class FileLogger : ILogger { + public bool DebugLogEnabled { get; set; } = true; + public bool InfoLogEnabled { get; set; } = true; + public bool WarnLogEnabled { get; set; } = true; + public bool ErrorLogenabled { get; set; } = true; public ILogFormat Format { get; } public string LogfileName { get; protected set; } @@ -26,6 +30,9 @@ public class FileLogger : ILogger public void Log(string context, LogSeverity severity, object message, ILogFormat format) { + if(!this.CheckSeverity(severity)) + return; + var msg = format.CreateMessage(context, severity, format).ToBytes(StringConverter.UTF8); lock (LogfileStream) { diff --git a/DTLib.Logging/Loggers/ILogger.cs b/DTLib.Logging/Loggers/ILogger.cs index 3fbbdfb..e2643d9 100644 --- a/DTLib.Logging/Loggers/ILogger.cs +++ b/DTLib.Logging/Loggers/ILogger.cs @@ -4,6 +4,12 @@ public interface ILogger : IDisposable { ILogFormat Format { get; } + bool DebugLogEnabled { get; set; } + bool InfoLogEnabled { get; set; } + bool WarnLogEnabled { get; set; } + bool ErrorLogenabled { get; set; } + void Log(string context, LogSeverity severity, object message); void Log(string context, LogSeverity severity, object message, ILogFormat format); + } \ No newline at end of file From c7016371a53efa266453e0ab4aacaae4fe260e9f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 17 Nov 2022 22:08:29 +0600 Subject: [PATCH 13/16] DebugLogEnabled = false --- DTLib.Logging/Loggers/CompositeLogger.cs | 2 +- DTLib.Logging/Loggers/ConsoleLogger.cs | 2 +- DTLib.Logging/Loggers/FileLogger.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/DTLib.Logging/Loggers/CompositeLogger.cs b/DTLib.Logging/Loggers/CompositeLogger.cs index 2b3a3e2..aacca48 100644 --- a/DTLib.Logging/Loggers/CompositeLogger.cs +++ b/DTLib.Logging/Loggers/CompositeLogger.cs @@ -5,7 +5,7 @@ namespace DTLib.Logging.New; /// public class CompositeLogger : ILogger { - public bool DebugLogEnabled { get; set; } = true; + public bool DebugLogEnabled { get; set; } = false; public bool InfoLogEnabled { get; set; } = true; public bool WarnLogEnabled { get; set; } = true; public bool ErrorLogenabled { get; set; } = true; diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs index 7a249cf..fc7c707 100644 --- a/DTLib.Logging/Loggers/ConsoleLogger.cs +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -3,7 +3,7 @@ // вывод лога в консоль и файл public class ConsoleLogger : ILogger { - public bool DebugLogEnabled { get; set; } = true; + public bool DebugLogEnabled { get; set; } = false; public bool InfoLogEnabled { get; set; } = true; public bool WarnLogEnabled { get; set; } = true; public bool ErrorLogenabled { get; set; } = true; diff --git a/DTLib.Logging/Loggers/FileLogger.cs b/DTLib.Logging/Loggers/FileLogger.cs index ae5cfff..3489be8 100644 --- a/DTLib.Logging/Loggers/FileLogger.cs +++ b/DTLib.Logging/Loggers/FileLogger.cs @@ -2,7 +2,7 @@ public class FileLogger : ILogger { - public bool DebugLogEnabled { get; set; } = true; + public bool DebugLogEnabled { get; set; } = false; public bool InfoLogEnabled { get; set; } = true; public bool WarnLogEnabled { get; set; } = true; public bool ErrorLogenabled { get; set; } = true; From 6ee7fbc3f642e22564088f724d6cd540dbe05ec8 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 18 Nov 2022 00:56:05 +0600 Subject: [PATCH 14/16] MergeToString fix --- DTLib/Extensions/StringConverter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DTLib/Extensions/StringConverter.cs b/DTLib/Extensions/StringConverter.cs index 5c55f7c..6dc3032 100644 --- a/DTLib/Extensions/StringConverter.cs +++ b/DTLib/Extensions/StringConverter.cs @@ -51,9 +51,9 @@ public static class StringConverter return builder.ToString(); } - // String.Join(string...) does some low-level memory manipulations, that are faster than StringBuilder - public static string MergeToString(string part0, params string[] parts) - =>string.Join(part0, parts); + // String.Join(sep,string...) does some low-level memory manipulations, that are faster than StringBuilder + public static string MergeToString(params string[] parts) + =>string.Join(null, parts); public static string MergeToString(this IEnumerable collection, string separator) { From 80964f193f4013725f69d2bf227c7834c591b7a2 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 18 Nov 2022 02:23:20 +0600 Subject: [PATCH 15/16] LoggerExtensions --- DTLib.Logging/Loggers/CompositeLogger.cs | 4 --- DTLib.Logging/Loggers/ConsoleLogger.cs | 3 -- DTLib.Logging/Loggers/FileLogger.cs | 8 ++--- DTLib.Logging/Loggers/ILogger.cs | 1 - DTLib.Tests/Dtsod/TestAutoarr.cs | 14 ++++----- DTLib.Tests/Dtsod/TestDtsodV23.cs | 38 ++++++++++++------------ DTLib.Tests/Dtsod/TestDtsodV24.cs | 32 ++++++++++---------- DTLib.Tests/Dtsod/TestPInvoke.cs | 14 ++++----- DTLib.Tests/Program.cs | 9 +++--- 9 files changed, 56 insertions(+), 67 deletions(-) diff --git a/DTLib.Logging/Loggers/CompositeLogger.cs b/DTLib.Logging/Loggers/CompositeLogger.cs index aacca48..256bc6a 100644 --- a/DTLib.Logging/Loggers/CompositeLogger.cs +++ b/DTLib.Logging/Loggers/CompositeLogger.cs @@ -32,10 +32,6 @@ public class CompositeLogger : ILogger _loggers[i].Log(context, severity, message, format); } - public void Log(string context, LogSeverity severity, object message) - => Log(context, severity, message, Format); - - public void Dispose() { for (int i = 0; i < _loggers.Length; i++) diff --git a/DTLib.Logging/Loggers/ConsoleLogger.cs b/DTLib.Logging/Loggers/ConsoleLogger.cs index fc7c707..3ffe8dd 100644 --- a/DTLib.Logging/Loggers/ConsoleLogger.cs +++ b/DTLib.Logging/Loggers/ConsoleLogger.cs @@ -27,9 +27,6 @@ public class ConsoleLogger : ILogger lock (consolelocker) ColoredConsole.Write(ColorFromSeverity(severity),msg); } - - public void Log(string context, LogSeverity severity, object message) - => Log(context, severity, message, Format); private static ConsoleColor ColorFromSeverity(LogSeverity severity) => severity switch diff --git a/DTLib.Logging/Loggers/FileLogger.cs b/DTLib.Logging/Loggers/FileLogger.cs index 3489be8..459d3c6 100644 --- a/DTLib.Logging/Loggers/FileLogger.cs +++ b/DTLib.Logging/Loggers/FileLogger.cs @@ -33,23 +33,19 @@ public class FileLogger : ILogger if(!this.CheckSeverity(severity)) return; - var msg = format.CreateMessage(context, severity, format).ToBytes(StringConverter.UTF8); + var msg = format.CreateMessage(context, severity, message); lock (LogfileStream) { - LogfileStream.Write(msg); + LogfileStream.Write(msg.ToBytes(StringConverter.UTF8)); LogfileStream.Flush(); } } - - public void Log(string context, LogSeverity severity, object message) - => Log(context, severity, message, Format); public virtual void Dispose() { try { LogfileStream?.Flush(); - LogfileStream?.Close(); LogfileStream?.Dispose(); } catch (ObjectDisposedException) { } diff --git a/DTLib.Logging/Loggers/ILogger.cs b/DTLib.Logging/Loggers/ILogger.cs index e2643d9..ab6eedf 100644 --- a/DTLib.Logging/Loggers/ILogger.cs +++ b/DTLib.Logging/Loggers/ILogger.cs @@ -9,7 +9,6 @@ public interface ILogger : IDisposable bool WarnLogEnabled { get; set; } bool ErrorLogenabled { get; set; } - void Log(string context, LogSeverity severity, object message); void Log(string context, LogSeverity severity, object message, ILogFormat format); } \ No newline at end of file diff --git a/DTLib.Tests/Dtsod/TestAutoarr.cs b/DTLib.Tests/Dtsod/TestAutoarr.cs index cbeaa9d..0605159 100644 --- a/DTLib.Tests/Dtsod/TestAutoarr.cs +++ b/DTLib.Tests/Dtsod/TestAutoarr.cs @@ -15,24 +15,24 @@ public static class TestAutoarr public static void Fill(Autoarr ar) { - Info.Log("c", "----------[TestAutoarr/Fill]----------"); + OldLogger.Log("c", "----------[TestAutoarr/Fill]----------"); for(uint i=0;i ar) { - Info.Log("c", "----------[TestAutoarr/Print]---------"); + OldLogger.Log("c", "----------[TestAutoarr/Print]---------"); foreach (KVPair pair in ar) - Info.Log("h", pair.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("h", pair.ToString()); + OldLogger.Log("g", "test completed"); } public static void Free(Autoarr ar) { - Info.Log("c", "----------[TestAutoarr/Free]----------"); + OldLogger.Log("c", "----------[TestAutoarr/Free]----------"); ar.Dispose(); - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } } \ No newline at end of file diff --git a/DTLib.Tests/Dtsod/TestDtsodV23.cs b/DTLib.Tests/Dtsod/TestDtsodV23.cs index c397943..5f7ac9a 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV23.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV23.cs @@ -16,73 +16,73 @@ public static class TestDtsodV23 public static void TestBaseTypes() { - Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----"); + OldLogger.Log("c", "-----[TestDtsodV23/TestBaseTypes]-----"); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}base_types.dtsod")); foreach (var pair in dtsod) - Info.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); + OldLogger.Log("g", "test completed"); } public static void TestLists() { - Info.Log("c", "-------[TestDtsodV23/TestLists]-------"); + OldLogger.Log("c", "-------[TestDtsodV23/TestLists]-------"); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}lists.dtsod")); foreach (var pair in dtsod) { - Info.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", + OldLogger.Log("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", $" count: {pair.Value.Count}"); foreach (var el in pair.Value) - Info.Log("b", '\t'+el.GetType().Name + ' ', "c", el.ToString()); + OldLogger.Log("b", '\t'+el.GetType().Name + ' ', "c", el.ToString()); } - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } public static void TestComplexes() { - Info.Log("c", "-----[TestDtsodV23/TestComplexes]-----"); + OldLogger.Log("c", "-----[TestDtsodV23/TestComplexes]-----"); DtsodV23 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")); foreach (var complex in dtsod) { - Info.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key, + OldLogger.Log("b", complex.Value.GetType().Name + ' ', "w", complex.Key, "b", " size: ", "c", complex.Value.Keys.Count.ToString()); foreach (var pair in (DtsodV23) complex.Value) - Info.Log("b", '\t' + pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', + OldLogger.Log("b", '\t' + pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); } - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } public static void TestReSerialization() { - Info.Log("c", "--[TestDtsodV23/TestReSerialization]--"); + OldLogger.Log("c", "--[TestDtsodV23/TestReSerialization]--"); var dtsod = new DtsodV23(new DtsodV23(new DtsodV23( new DtsodV23(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString()); - Info.Log("y", dtsod.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("y", dtsod.ToString()); + OldLogger.Log("g", "test completed"); } public static void TestSpeed() { - Info.Log("c", "-------[TestDtsodV23/TestSpeed]-------"); + OldLogger.Log("c", "-------[TestDtsodV23/TestSpeed]-------"); IDtsod dtsod=null; string text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); LogOperationTime("V21 deserialization",64,()=>dtsod=new DtsodV21(text)); LogOperationTime("V21 serialization", 64, () => _=dtsod.ToString()); LogOperationTime("V23 deserialization", 64, () => dtsod = new DtsodV23(text)); LogOperationTime("V23 serialization", 64, () => _ = dtsod.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } public static void TestMemoryConsumption() { - Info.Log("c", "----[TestDtsodV23/TestMemConsumpt]----"); + OldLogger.Log("c", "----[TestDtsodV23/TestMemConsumpt]----"); string text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); var a = GC.GetTotalMemory(true); var dtsods = new DtsodV23[64]; for (int i = 0; i < dtsods.Length; i++) dtsods[i] = new(text); var b = GC.GetTotalMemory(true); - Info.Log("b", "at the start: ","c",$"{a/1024} kb\n", + OldLogger.Log("b", "at the start: ","c",$"{a/1024} kb\n", "b", "at the end: ", "c", $"{b / 1024} kb\n{dtsods.Count()}","b"," dtsods initialized"); - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } } diff --git a/DTLib.Tests/Dtsod/TestDtsodV24.cs b/DTLib.Tests/Dtsod/TestDtsodV24.cs index f5d829b..5e840bb 100644 --- a/DTLib.Tests/Dtsod/TestDtsodV24.cs +++ b/DTLib.Tests/Dtsod/TestDtsodV24.cs @@ -17,57 +17,57 @@ public static class TestDtsodV24 public static void TestBaseTypes() { - Info.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----"); + OldLogger.Log("c", "-----[TestDtsodV24/TestBaseTypes]-----"); DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}base_types.dtsod")); foreach (var pair in dtsod) - Info.Log("b", pair.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("b", pair.ToString()); + OldLogger.Log("g", "test completed"); } public static void TestComplexes() { - Info.Log("c", "-----[TestDtsodV24/TestComplexes]-----"); + OldLogger.Log("c", "-----[TestDtsodV24/TestComplexes]-----"); DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}complexes.dtsod")); - Info.Log("h", dtsod.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("h", dtsod.ToString()); + OldLogger.Log("g", "test completed"); } public static void TestLists() { - Info.Log("c", "-------[TestDtsodV24/TestLists]-------"); + OldLogger.Log("c", "-------[TestDtsodV24/TestLists]-------"); DtsodV24 dtsod = new(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}lists.dtsod")); foreach (KVPair pair in dtsod) { var list = new Autoarr(pair.value.VoidPtr, false); - Info.Log("b", pair.key.HGlobalUTF8ToString(), "w", $" length: {list.Length}"); + OldLogger.Log("b", pair.key.HGlobalUTF8ToString(), "w", $" length: {list.Length}"); foreach (var el in list) { - Info.Log("h", '\t' + el.ToString()); + OldLogger.Log("h", '\t' + el.ToString()); if (el.TypeCode == KerepTypeCode.AutoarrUnitypePtr) { var ar = new Autoarr(el.VoidPtr, false); foreach (var k in ar) { - Info.Log($"\t\t{k.ToString()}"); + OldLogger.Log($"\t\t{k.ToString()}"); } } } } - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } public static void TestReSerialization() { - Info.Log("c", "--[TestDtsodV24/TestReSerialization]--"); + OldLogger.Log("c", "--[TestDtsodV24/TestReSerialization]--"); var dtsod = new DtsodV24(new DtsodV24(new DtsodV24( new DtsodV24(File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString()); - Info.Log("h", dtsod.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("h", dtsod.ToString()); + OldLogger.Log("g", "test completed"); } public static void TestSpeed() { - Info.Log("c", "-------[TestDtsodV24/TestSpeed]-------"); + OldLogger.Log("c", "-------[TestDtsodV24/TestSpeed]-------"); IDtsod dtsod=null; string _text = File.ReadAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV23{Путь.Разд}messages.dtsod"); string text = ""; @@ -77,6 +77,6 @@ public static class TestDtsodV24 File.WriteAllText($"Dtsod{Путь.Разд}TestResources{Путь.Разд}DtsodV24{Путь.Разд}messages.dtsod",text); LogOperationTime("V24 deserialization", 64, () => dtsod = new DtsodV24(text)); LogOperationTime("V24 serialization", 64, () => text = dtsod.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } } \ No newline at end of file diff --git a/DTLib.Tests/Dtsod/TestPInvoke.cs b/DTLib.Tests/Dtsod/TestPInvoke.cs index f95b199..8c8caa8 100644 --- a/DTLib.Tests/Dtsod/TestPInvoke.cs +++ b/DTLib.Tests/Dtsod/TestPInvoke.cs @@ -16,7 +16,7 @@ public static class TestPInvoke static public void TestUTF8() { - Info.Log("c", "--------[TestPInvoke/TestUTF8]--------", "b", ""); + OldLogger.Log("c", "--------[TestPInvoke/TestUTF8]--------", "b", ""); IntPtr ptr; string str="_$\"\\\\'''\ta ыыы000;2;=:%d;```"; for(int i=0; i<1000; i++) @@ -24,7 +24,7 @@ public static class TestPInvoke ptr = Unmanaged.StringToHGlobalUTF8(str); str = Unmanaged.HGlobalUTF8ToString(ptr); } - Info.Log("y", str); + OldLogger.Log("y", str); } [DllImport("kerep", CallingConvention = CallingConvention.Cdecl)] @@ -32,9 +32,9 @@ public static class TestPInvoke public static void TestPrintf() { - Info.Log("c", "---------[TestPInvoke/Printf]---------", "b", ""); + OldLogger.Log("c", "---------[TestPInvoke/Printf]---------", "b", ""); pinvoke_print("ъъ~ 中文"); - Info.Log("g", "test completed"); + OldLogger.Log("g", "test completed"); } [DllImport("kerep", CallingConvention = CallingConvention.Cdecl)] @@ -42,11 +42,11 @@ public static class TestPInvoke public static unsafe void TestMarshalling() { - Info.Log("c", "---------[TestAutoarr/TestMarshalling]----------"); + OldLogger.Log("c", "---------[TestAutoarr/TestMarshalling]----------"); string msg = "ъъ~ 中文"; test_marshalling(msg, out var kptr); KVPair k = *(KVPair*)kptr; - Info.Log("b", k.ToString()); - Info.Log("g", "test completed"); + OldLogger.Log("b", k.ToString()); + OldLogger.Log("g", "test completed"); } } \ No newline at end of file diff --git a/DTLib.Tests/Program.cs b/DTLib.Tests/Program.cs index a30fcb4..a5f58f3 100644 --- a/DTLib.Tests/Program.cs +++ b/DTLib.Tests/Program.cs @@ -10,17 +10,18 @@ global using DTLib.Filesystem; global using DTLib.Dtsod; global using static DTLib.Tests.TesterLog; global using static DTLib.Tests.Program; -using DTLib.Logging; +using DTLib.Logging.New; namespace DTLib.Tests; public static class Program { - public static readonly ConsoleLogger Info = new("logs", "DTLib.Tests"); + public static readonly Logging.ConsoleLogger OldLogger = new("logs", "DTLib.Tests"); + public static readonly ILogger NewLogger = new CompositeLogger(new ConsoleLogger(), new FileLogger(OldLogger.LogfileName)); public static void Main() { - PublicLog.LogEvent += Info.Log; + Logging.PublicLog.LogEvent += OldLogger.Log; Console.OutputEncoding = Encoding.UTF8; Console.InputEncoding = Encoding.UTF8; Console.Title="tester"; @@ -32,7 +33,7 @@ public static class Program TestDtsodV24.TestAll(); } catch (Exception ex) - { Info.Log("r", ex.ToString()); } + { NewLogger.LogException("Main", ex); } Console.ResetColor(); } } \ No newline at end of file From 34922b7cc9f953bcf0a84b984879e172a0adb45e Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 18 Nov 2022 02:43:14 +0600 Subject: [PATCH 16/16] LoggerExtensions --- DTLib.Logging/Loggers/LoggerExtensions.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 DTLib.Logging/Loggers/LoggerExtensions.cs diff --git a/DTLib.Logging/Loggers/LoggerExtensions.cs b/DTLib.Logging/Loggers/LoggerExtensions.cs new file mode 100644 index 0000000..3fee831 --- /dev/null +++ b/DTLib.Logging/Loggers/LoggerExtensions.cs @@ -0,0 +1,12 @@ +using Ben.Demystifier; + +namespace DTLib.Logging.New; + +public static class LoggerExtensions +{ + public static void Log(this ILogger logger, string context, LogSeverity severity, object message) + => logger.Log(context, severity, message, logger.Format); + + public static void LogException(this ILogger logger, string context, Exception ex) + => logger.Log(context, LogSeverity.Error, ex.Demystify()); +} \ No newline at end of file