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;
|
|
|
|
|
2024-04-24 16:52:25 +02:00
|
|
|
namespace DotBased.Logging.Serilog;
|
2024-04-23 20:42:56 +02:00
|
|
|
|
|
|
|
public class SerilogAdapter : LogAdapterBase
|
|
|
|
{
|
|
|
|
public SerilogAdapter(global::Serilog.ILogger serilogLogger) : base("Serilog adapter")
|
|
|
|
{
|
|
|
|
_serilogLogger = serilogLogger;
|
|
|
|
_messageTemplateParser = new MessageTemplateParser();
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:09:11 +02:00
|
|
|
public const string SampleTemplate = "[{Timestamp:HH:mm:ss} - {Caller}] | {Level:u3}] {Message:lj}{NewLine}{Exception}";
|
2024-04-23 20:42:56 +02:00
|
|
|
|
|
|
|
private readonly global::Serilog.ILogger _serilogLogger;
|
|
|
|
private readonly MessageTemplateParser _messageTemplateParser;
|
|
|
|
|
2024-05-04 11:31:14 +02:00
|
|
|
public override void HandleLog(object? processor, LogCapsule? capsule)
|
2024-04-23 20:42:56 +02:00
|
|
|
{
|
|
|
|
if (capsule == null)
|
|
|
|
return;
|
|
|
|
var logger = _serilogLogger
|
2024-05-04 16:09:11 +02:00
|
|
|
.ForContext("Assembly", capsule.Logger.Caller.AssemblyName)
|
|
|
|
.ForContext("Source", capsule.Logger.Caller.Source)
|
|
|
|
.ForContext("Caller", 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|