diff --git a/Tests/Serilog.Exceptions.Test/Destructurers/ExceptionDestructurerTest.cs b/Tests/Serilog.Exceptions.Test/Destructurers/ExceptionDestructurerTest.cs index 75582b73..2b82a3c5 100644 --- a/Tests/Serilog.Exceptions.Test/Destructurers/ExceptionDestructurerTest.cs +++ b/Tests/Serilog.Exceptions.Test/Destructurers/ExceptionDestructurerTest.cs @@ -196,6 +196,58 @@ public void WhenExceptionContainsDictionaryWithNonScalarValue_ShouldNotThrow() .Which.Name.Should().Be("System.Collections.Generic.List`1[System.Int32]"); } + private class DefaultExceptionDestructurer : ExceptionDestructurer + { + public override Type[] TargetTypes { get; } = { typeof(T) }; + } + + [Fact] + public void GivenException_ContainingProperty_WithCustomDestructuringPolicy_ShouldApplyThePolicy() + { + // Arrange + var options = new DestructuringOptionsBuilder() + .WithDefaultDestructurers() + .WithDestructurers(new[] + { + new DefaultExceptionDestructurer(), + }); + var token = new Token(1, "Don't show me!"); + var exception = new TokenException(); + exception.Data["@Token"] = token; + static LoggerConfiguration LoggerConf(LoggerConfiguration x) => + x.Destructure.ByTransforming(x => new { x.Id, Token = new string('*', x.Value.Length) }); + + // Act + var rootObject = LogAndDestructureException(exception, options, LoggerConf); + + // Assert + var exceptionObject = ExtractExceptionDetails(rootObject); + var dataObject = exceptionObject.Properties().Should().ContainSingle(x => x.Name == "Data").Which; + var tokenObject = dataObject + .Should().BeOfType().Which.Value + .Should().BeOfType() + .Which.Properties().Should().ContainSingle(x => x.Name == "@Token").Which; + tokenObject.Value.Should().BeOfType().Which + .Properties().Should().ContainSingle(x => x.Name == "Token").Which + .Should().BeOfType().Which.Value + .Should().BeOfType().Which.Value.Should().Be("**************"); + } + + private class Token + { + public Token(int id, string value) + { + this.Id = id; + this.Value = value; + } + + public int Id { get; private init; } + + public string Value { get; private init; } + } + + class TokenException : Exception { } + public class DictNonScalarKeyException : Exception { public DictNonScalarKeyException() => this.Reference = new Dictionary, object>(); diff --git a/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs b/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs index 172818a2..eafe4d8c 100644 --- a/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs +++ b/Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs @@ -16,15 +16,23 @@ public static class LogJsonOutputUtils { public static JObject LogAndDestructureException( Exception exception, - IDestructuringOptions? destructuringOptions = null) + IDestructuringOptions? destructuringOptions = null, + Func? configureLogger = null) { // Arrange var jsonWriter = new StringWriter(); destructuringOptions ??= new DestructuringOptionsBuilder().WithDefaultDestructurers(); - ILogger logger = new LoggerConfiguration() + var loggerConfiguration = new LoggerConfiguration() .Enrich.WithExceptionDetails(destructuringOptions) - .WriteTo.Sink(new TestTextWriterSink(jsonWriter, new JsonFormatter())) - .CreateLogger(); + .WriteTo.Sink(new TestTextWriterSink(jsonWriter, new JsonFormatter())); + + if (configureLogger != null) + { + loggerConfiguration = configureLogger.Invoke(loggerConfiguration); + } + + + var logger = loggerConfiguration.CreateLogger(); // Act logger.Error(exception, "EXCEPTION MESSAGE");