commit d066bea8071578ff2f940fa75d44d3bf94c12d23 Author: glax Date: Thu Feb 1 21:41:31 2024 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/.idea/.idea.GlaxLogger/.idea/.gitignore b/.idea/.idea.GlaxLogger/.idea/.gitignore new file mode 100644 index 0000000..fcd76f9 --- /dev/null +++ b/.idea/.idea.GlaxLogger/.idea/.gitignore @@ -0,0 +1,13 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Rider ignored files +/modules.xml +/contentModel.xml +/projectSettingsUpdater.xml +/.idea.GlaxLogger.iml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/.idea.GlaxLogger/.idea/encodings.xml b/.idea/.idea.GlaxLogger/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.GlaxLogger/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.GlaxLogger/.idea/indexLayout.xml b/.idea/.idea.GlaxLogger/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.GlaxLogger/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.GlaxLogger/.idea/vcs.xml b/.idea/.idea.GlaxLogger/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.GlaxLogger/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/GlaxLogger.sln b/GlaxLogger.sln new file mode 100644 index 0000000..6389ead --- /dev/null +++ b/GlaxLogger.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GlaxLogger", "GlaxLogger\GlaxLogger.csproj", "{17E0C53C-A186-4989-BB58-A21196F8AA1D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {17E0C53C-A186-4989-BB58-A21196F8AA1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {17E0C53C-A186-4989-BB58-A21196F8AA1D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {17E0C53C-A186-4989-BB58-A21196F8AA1D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {17E0C53C-A186-4989-BB58-A21196F8AA1D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/GlaxLogger/GlaxLogger.csproj b/GlaxLogger/GlaxLogger.csproj new file mode 100644 index 0000000..4e5bb4e --- /dev/null +++ b/GlaxLogger/GlaxLogger.csproj @@ -0,0 +1,24 @@ + + + + net7.0 + enable + enable + Glax + https://git.bernloehr.eu/glax/GlaxLogger + git + + + + + + + + LICENSE.md + + + + + + + diff --git a/GlaxLogger/Logger.cs b/GlaxLogger/Logger.cs new file mode 100644 index 0000000..7ffc9c7 --- /dev/null +++ b/GlaxLogger/Logger.cs @@ -0,0 +1,74 @@ +using System.Text; +using Microsoft.Extensions.Logging; + +namespace GlaxLogger; + +public class Logger : ILogger +{ + private readonly LogLevel _filterLevel; + private readonly FileStream _allMessageLogfile, _filteredLogfile; + private readonly ConsoleColor _defaultForegroundColor = Console.ForegroundColor; + private readonly ConsoleColor _defaultBackgroundColor = Console.BackgroundColor; + + + public Logger(LogLevel filteredLevel = LogLevel.Warning, string? outputFolderPath = null) + { + this._filterLevel = filteredLevel; + string logFolderPath = outputFolderPath ?? Environment.CurrentDirectory; + this._filteredLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:hh:mm:ss}-filtered.log"), FileMode.Create, FileAccess.Write, FileShare.Read); + this._allMessageLogfile = new FileStream(Path.Join(logFolderPath, $"{DateTime.Now:hh:mm:ss}.log"), FileMode.Create, FileAccess.Write, FileShare.Read); + } + + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + string logLevelStr = logLevel.ToString()[..3].ToUpper(); + string timeStr = $"[{DateTime.UtcNow:HH:mm:ss.fff}]"; + string messageStr = formatter.Invoke(state, exception); + string fullMessage = $"{logLevelStr} [{timeStr}] {messageStr}"; + byte[] fullMessageBytes = Encoding.UTF8.GetBytes(fullMessage); + _allMessageLogfile.Write(fullMessageBytes, 0, fullMessageBytes.Length); + if (!IsEnabled(logLevel)) + return; + _filteredLogfile.Write(fullMessageBytes, 0, fullMessageBytes.Length); + Console.ForegroundColor = ForegroundColorForLogLevel(logLevel); + Console.BackgroundColor = BackgroundColorForLogLevel(logLevel); + Console.Write(logLevelStr); + Console.ResetColor(); + // ReSharper disable once LocalizableElement + Console.Write($" {timeStr} "); + Console.WriteLine(messageStr); + } + + public bool IsEnabled(LogLevel logLevel) + { + return logLevel >= _filterLevel; + } + + public IDisposable? BeginScope(TState state) where TState : notnull + { + return null; + } + + private ConsoleColor ForegroundColorForLogLevel(LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Error or LogLevel.Critical => ConsoleColor.Black, + LogLevel.Debug => ConsoleColor.Black, + LogLevel.Information => ConsoleColor.White, + _ => _defaultForegroundColor + }; + } + + private ConsoleColor BackgroundColorForLogLevel(LogLevel logLevel) + { + return logLevel switch + { + LogLevel.Error or LogLevel.Critical => ConsoleColor.Red, + LogLevel.Debug => ConsoleColor.Yellow, + LogLevel.Information => ConsoleColor.Black, + _ => _defaultBackgroundColor + }; + } + +} \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..4908012 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Glax + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE.