refactor: Upgrade Serilog.Expressions to v5.0.0

In addition to the package upgrade, code was also refactored to use the
new `Inspect()` method to access properties on the exception object.
pull/268/head
Robert Dailey 10 months ago
parent 72c4596347
commit bbc32ef3d7

@ -20,7 +20,7 @@
<PackageVersion Include="ReactiveUI.Blazor" Version="20.1.1" />
<PackageVersion Include="Serilog" Version="4.0.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="8.0.1" />
<PackageVersion Include="Serilog.Expressions" Version="4.0.0" />
<PackageVersion Include="Serilog.Expressions" Version="5.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageVersion Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageVersion Include="Spectre.Console" Version="0.49.1" />

@ -1,11 +1,10 @@
using System.Text;
using Recyclarr.Http;
using Serilog.Core;
using Serilog.Events;
namespace Recyclarr.Cli.Logging;
public class FlurlExceptionSanitizingEnricher : ILogEventEnricher
internal class FlurlExceptionSanitizingEnricher : ILogEventEnricher
{
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
{
@ -16,28 +15,9 @@ public class FlurlExceptionSanitizingEnricher : ILogEventEnricher
var sanitizedMessage = Sanitize.ExceptionMessage(logEvent.Exception);
// Use a builder to handle whether to use a newline character for the full exception message without checking if
// the sanitized message is null or whitespace more than once.
var fullBuilder = new StringBuilder();
if (!string.IsNullOrWhiteSpace(sanitizedMessage))
{
MakeProperty("SanitizedExceptionMessage", sanitizedMessage);
fullBuilder.Append($"{sanitizedMessage}\n");
}
// ReSharper disable once InvertIf
if (logEvent.Exception.StackTrace is not null)
{
fullBuilder.Append(logEvent.Exception.StackTrace);
MakeProperty("SanitizedExceptionFull", fullBuilder.ToString());
}
return;
void MakeProperty(string propertyName, object value)
{
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty(propertyName, value));
logEvent.AddOrUpdateProperty(propertyFactory.CreateProperty("SanitizedExceptionMessage", sanitizedMessage));
}
}
}

@ -29,7 +29,8 @@ public class LoggerFactory(IAppPaths paths, LoggingLevelSwitch levelSwitch)
private static ExpressionTemplate GetFileTemplate()
{
var template = "[{@t:HH:mm:ss} {@l:u3}] " + GetBaseTemplateString() +
"{#if SanitizedExceptionFull is not null}: {SanitizedExceptionFull}{#end}\n";
"{#if SanitizedExceptionMessage is not null}: {SanitizedExceptionMessage}{#end}\n" +
"{Inspect(@x).StackTrace}";
return new ExpressionTemplate(template);
}

Loading…
Cancel
Save