[REFACTOR] Refactored logging & added support for Microsoft.Extensions.Logging. Added test WebApi project

This commit is contained in:
max
2024-11-17 22:51:54 +01:00
parent 58739c2aea
commit 737cbcfd11
31 changed files with 398 additions and 97 deletions

View File

@@ -7,7 +7,7 @@ public static class BasedSerilog
/// <summary>
/// Default output template with the extra properties that can be used for serilog sinks.
/// </summary>
public const string OutputTemplate = "[{Timestamp:HH:mm:ss} - {Caller}->{Assembly}] | {Level:u3}] {Message:lj}{NewLine}{Exception}";
public const string OutputTemplate = "[{Timestamp:HH:mm:ss} {Level:u3} - {LoggerName}]{NewLine} {Message:lj}{NewLine}{Exception}";
public static LoggerConfiguration UseBasedExtension(this LoggerConfiguration loggerConfiguration)
{
@@ -20,8 +20,10 @@ public static class BasedSerilog
/// </summary>
public static class ExtraProperties
{
public const string LoggerName = "LoggerName";
public const string AssemblyProp = "Assembly";
public const string SourceProp = "Source";
public const string FullNameProp = "FullName";
public const string NamespaceProp = "Namespace";
public const string CallerProp = "Caller";
}
}

View File

@@ -16,9 +16,11 @@ public class BasedSerilogAdapter(global::Serilog.ILogger serilogLogger) : LogAda
if (capsule == null)
return;
var logger = serilogLogger
.ForContext(BasedSerilog.ExtraProperties.AssemblyProp, capsule.Logger.Caller.AssemblyName)
.ForContext(BasedSerilog.ExtraProperties.SourceProp, capsule.Logger.Caller.Source)
.ForContext(BasedSerilog.ExtraProperties.CallerProp, capsule.Logger.Caller.Name);
.ForContext(BasedSerilog.ExtraProperties.LoggerName, capsule.Logger.Name)
.ForContext(BasedSerilog.ExtraProperties.AssemblyProp, capsule.Logger.LoggerInformation.AssemblyName)
.ForContext(BasedSerilog.ExtraProperties.FullNameProp, capsule.Logger.LoggerInformation.TypeFullName)
.ForContext(BasedSerilog.ExtraProperties.NamespaceProp, capsule.Logger.LoggerInformation.TypeNamespace)
.ForContext(BasedSerilog.ExtraProperties.CallerProp, capsule.Logger.LoggerInformation.TypeName);
var template = _messageTemplateParser.Parse(capsule.Message);
IEnumerable<LogEventProperty>? properties = null;

View File

@@ -15,7 +15,7 @@ public class BasedSerilogEnricher : ILogEventEnricher
sourcePropValue = appValue.ToString().Replace("\"", "");
var assemblyProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.AssemblyProp, asmPropValue);
var sourceProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.SourceProp, sourcePropValue);
var sourceProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.FullNameProp, sourcePropValue);
var callerProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.CallerProp, sourcePropValue);
logEvent.AddPropertyIfAbsent(assemblyProperty);

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<LangVersion>default</LangVersion>
</PropertyGroup>