From bbc32ef3d7a4dd2724556c1559a608fb43a4d0bf Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Wed, 19 Jun 2024 12:52:41 -0500 Subject: [PATCH] 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. --- Directory.Packages.props | 2 +- .../FlurlExceptionSanitizingEnricher.cs | 24 ++----------------- src/Recyclarr.Cli/Logging/LoggerFactory.cs | 3 ++- 3 files changed, 5 insertions(+), 24 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index a5c3b451..26ae6277 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -20,7 +20,7 @@ - + diff --git a/src/Recyclarr.Cli/Logging/FlurlExceptionSanitizingEnricher.cs b/src/Recyclarr.Cli/Logging/FlurlExceptionSanitizingEnricher.cs index 1c420d98..79966350 100644 --- a/src/Recyclarr.Cli/Logging/FlurlExceptionSanitizingEnricher.cs +++ b/src/Recyclarr.Cli/Logging/FlurlExceptionSanitizingEnricher.cs @@ -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)); } } } diff --git a/src/Recyclarr.Cli/Logging/LoggerFactory.cs b/src/Recyclarr.Cli/Logging/LoggerFactory.cs index c59f3e07..e22144ab 100644 --- a/src/Recyclarr.Cli/Logging/LoggerFactory.cs +++ b/src/Recyclarr.Cli/Logging/LoggerFactory.cs @@ -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); }