DotBased/DotBased.Logging.Serilog/BasedSerilogAdapter.cs

54 lines
3.0 KiB
C#
Raw Normal View History

2024-04-23 20:42:56 +02:00
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Serilog.Events;
using Serilog.Parsing;
namespace DotBased.Logging.Serilog;
2024-04-23 20:42:56 +02:00
2024-06-16 21:08:26 +02:00
public class BasedSerilogAdapter(global::Serilog.ILogger serilogLogger) : LogAdapterBase("Serilog adapter")
2024-04-23 20:42:56 +02:00
{
2024-06-16 21:08:26 +02:00
private readonly MessageTemplateParser _messageTemplateParser = new();
2024-04-23 20:42:56 +02:00
public override void HandleLog(object? processor, LogCapsule? capsule)
2024-04-23 20:42:56 +02:00
{
if (capsule == null)
return;
2024-06-16 21:08:26 +02:00
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);
2024-04-23 20:42:56 +02:00
var template = _messageTemplateParser.Parse(capsule.Message);
IEnumerable<LogEventProperty>? properties = null;
if (capsule.Parameters != null && capsule.Parameters.Length != 0)
{
var tokenList = template.Tokens.OfType<PropertyToken>().ToList();
properties = capsule.Parameters.Zip(tokenList, (p, t) => new LogEventProperty(t.PropertyName, new ScalarValue(p)));
}
switch (capsule.Severity)
{
2024-05-04 17:30:01 +02:00
case LogSeverity.Verbose:
2024-04-23 20:42:56 +02:00
case LogSeverity.Trace:
2024-05-04 10:53:07 +02:00
default:
2024-04-23 20:42:56 +02:00
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Verbose, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
case LogSeverity.Debug:
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Debug, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
case LogSeverity.Info:
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Information, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
case LogSeverity.Warning:
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Warning, null, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
case LogSeverity.Error:
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Error, capsule.Exception, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
case LogSeverity.Fatal:
logger.Write(new LogEvent(capsule.TimeStamp, LogEventLevel.Fatal, capsule.Exception, template, properties ?? ArraySegment<LogEventProperty>.Empty, ActivityTraceId.CreateRandom(), ActivitySpanId.CreateRandom()));
break;
}
}
}