mirror of
https://github.com/hmaxnl/DotBased.git
synced 2025-01-18 18:14:20 +01:00
Added serilog enricher
This commit is contained in:
parent
c1d18d5b47
commit
03daea46e7
27
DotBased.Logging.Serilog/BasedSerilog.cs
Normal file
27
DotBased.Logging.Serilog/BasedSerilog.cs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
|
namespace DotBased.Logging.Serilog;
|
||||||
|
|
||||||
|
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 static LoggerConfiguration UseBasedExtension(this LoggerConfiguration loggerConfiguration)
|
||||||
|
{
|
||||||
|
loggerConfiguration.Enrich.FromLogContext().Enrich.With(new BasedSerilogEnricher());
|
||||||
|
return loggerConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The extra properties this implementation adds to serilog
|
||||||
|
/// </summary>
|
||||||
|
public static class ExtraProperties
|
||||||
|
{
|
||||||
|
public const string AssemblyProp = "Assembly";
|
||||||
|
public const string SourceProp = "Source";
|
||||||
|
public const string CallerProp = "Caller";
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,27 +7,18 @@ using Serilog.Parsing;
|
||||||
|
|
||||||
namespace DotBased.Logging.Serilog;
|
namespace DotBased.Logging.Serilog;
|
||||||
|
|
||||||
public class SerilogAdapter : LogAdapterBase
|
public class BasedSerilogAdapter(global::Serilog.ILogger serilogLogger) : LogAdapterBase("Serilog adapter")
|
||||||
{
|
{
|
||||||
public SerilogAdapter(global::Serilog.ILogger serilogLogger) : base("Serilog adapter")
|
private readonly MessageTemplateParser _messageTemplateParser = new();
|
||||||
{
|
|
||||||
_serilogLogger = serilogLogger;
|
|
||||||
_messageTemplateParser = new MessageTemplateParser();
|
|
||||||
}
|
|
||||||
|
|
||||||
public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller}] | {Level:u3}] {Message:lj}{NewLine}{Exception}";
|
|
||||||
|
|
||||||
private readonly global::Serilog.ILogger _serilogLogger;
|
|
||||||
private readonly MessageTemplateParser _messageTemplateParser;
|
|
||||||
|
|
||||||
public override void HandleLog(object? processor, LogCapsule? capsule)
|
public override void HandleLog(object? processor, LogCapsule? capsule)
|
||||||
{
|
{
|
||||||
if (capsule == null)
|
if (capsule == null)
|
||||||
return;
|
return;
|
||||||
var logger = _serilogLogger
|
var logger = serilogLogger
|
||||||
.ForContext("Assembly", capsule.Logger.Caller.AssemblyName)
|
.ForContext(BasedSerilog.ExtraProperties.AssemblyProp, capsule.Logger.Caller.AssemblyName)
|
||||||
.ForContext("Source", capsule.Logger.Caller.Source)
|
.ForContext(BasedSerilog.ExtraProperties.SourceProp, capsule.Logger.Caller.Source)
|
||||||
.ForContext("Caller", capsule.Logger.Caller.Name);
|
.ForContext(BasedSerilog.ExtraProperties.CallerProp, capsule.Logger.Caller.Name);
|
||||||
|
|
||||||
var template = _messageTemplateParser.Parse(capsule.Message);
|
var template = _messageTemplateParser.Parse(capsule.Message);
|
||||||
IEnumerable<LogEventProperty>? properties = null;
|
IEnumerable<LogEventProperty>? properties = null;
|
25
DotBased.Logging.Serilog/BasedSerilogEnricher.cs
Normal file
25
DotBased.Logging.Serilog/BasedSerilogEnricher.cs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
using Serilog.Core;
|
||||||
|
using Serilog.Events;
|
||||||
|
|
||||||
|
namespace DotBased.Logging.Serilog;
|
||||||
|
|
||||||
|
public class BasedSerilogEnricher : ILogEventEnricher
|
||||||
|
{
|
||||||
|
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
|
||||||
|
{
|
||||||
|
var asmPropValue = "ASM";
|
||||||
|
var sourcePropValue = "Unknown";
|
||||||
|
if (logEvent.Properties.TryGetValue("SourceContext", out var sourceContextValue))
|
||||||
|
asmPropValue = sourceContextValue.ToString().Replace("\"", "");
|
||||||
|
if (logEvent.Properties.TryGetValue("Application", out var appValue))
|
||||||
|
sourcePropValue = appValue.ToString().Replace("\"", "");
|
||||||
|
|
||||||
|
var assemblyProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.AssemblyProp, asmPropValue);
|
||||||
|
var sourceProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.SourceProp, sourcePropValue);
|
||||||
|
var callerProperty = propertyFactory.CreateProperty(BasedSerilog.ExtraProperties.CallerProp, sourcePropValue);
|
||||||
|
|
||||||
|
logEvent.AddPropertyIfAbsent(assemblyProperty);
|
||||||
|
logEvent.AddPropertyIfAbsent(sourceProperty);
|
||||||
|
logEvent.AddPropertyIfAbsent(callerProperty);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user