Initial commit

This commit is contained in:
glax 2024-02-01 21:41:31 +01:00
commit d066bea807
9 changed files with 171 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

13
.idea/.idea.GlaxLogger/.idea/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
</project>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

16
GlaxLogger.sln Normal file
View File

@ -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

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Glax</Authors>
<RepositoryUrl>https://git.bernloehr.eu/glax/GlaxLogger</RepositoryUrl>
<RepositoryType>git</RepositoryType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
</ItemGroup>
<PropertyGroup>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
</PropertyGroup>
<ItemGroup>
<None Include="..\LICENSE.md" Pack="true" PackagePath=""/>
</ItemGroup>
</Project>

74
GlaxLogger/Logger.cs Normal file
View File

@ -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<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> 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>(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
};
}
}

21
LICENSE.md Normal file
View File

@ -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.