Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploring usage of Serilog's destructuring policies #354

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,58 @@ public void WhenExceptionContainsDictionaryWithNonScalarValue_ShouldNotThrow()
.Which.Name.Should().Be("System.Collections.Generic.List`1[System.Int32]");
}

private class DefaultExceptionDestructurer<T> : 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<TokenException>(),
});
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<Token>(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<JProperty>().Which.Value
.Should().BeOfType<JObject>()
.Which.Properties().Should().ContainSingle(x => x.Name == "@Token").Which;
tokenObject.Value.Should().BeOfType<JObject>().Which
.Properties().Should().ContainSingle(x => x.Name == "Token").Which
.Should().BeOfType<JProperty>().Which.Value
.Should().BeOfType<JValue>().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<IEnumerable<int>, object>();
Expand Down
16 changes: 12 additions & 4 deletions Tests/Serilog.Exceptions.Test/Destructurers/LogJsonOutputUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ public static class LogJsonOutputUtils
{
public static JObject LogAndDestructureException(
Exception exception,
IDestructuringOptions? destructuringOptions = null)
IDestructuringOptions? destructuringOptions = null,
Func<LoggerConfiguration, LoggerConfiguration>? 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");
Expand Down