Search code examples
c#exceptionloggingserilogserilog-exceptions

Is it possible to disabled the reflection based destructurer in Serilog.Exceptions?


The package Serilog.Exceptions is nice for logging exceptions. It has a large list of supported exception types.

e.g. the current setup:

Log.Logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .CreateLogger();

Currently it falls back to a "reflection based destructurer" if no destructurer is found for a exception type.

Because of performance, but more important of predictability, we would like to disable the reflection fallback.

I tried configuring this with DestructuringOptions, e.g. disable some of the default destructors or setting the DestructuringDepth to 0.

Unfortunately, those options won't work:

  • the ReflectionBasedDestructurer isn't in the default destructor list
  • setting DestructuringDepth to 0 isn't allowed (exception)

Any idea how to configure Serilog.Exceptions for this?


Solution

  • This has been added to Serilog.Exceptions 5.4.0, see changelog. There is a new method WithoutReflectionBasedDestructurer.

    Usage

    Helper class

    public static class LoggerEnrichmentConfigurationExtensions
    {
        public static LoggerConfiguration WithExceptionDetailsWithoutReflection(
            this LoggerEnrichmentConfiguration loggerEnrichmentConfiguration)
        {
            if (loggerEnrichmentConfiguration is null)
            {
                throw new ArgumentNullException(nameof(loggerEnrichmentConfiguration));
            }
            var options = new DestructuringOptionsBuilder()
                .WithDefaultDestructurers()
                .WithoutReflectionBasedDestructurer() //new in 5.4.0!
                .WithIgnoreStackTraceAndTargetSiteExceptionFilter();
            var logEventEnricher = new ExceptionEnricher(options);
            return loggerEnrichmentConfiguration.With(logEventEnricher);
        }
    }
    

    Config

    Log.Logger = new LoggerConfiguration()
                    .ReadFrom.Configuration(configuration)
                    .Enrich.WithExceptionDetailsWithoutReflection()
                    .CreateLogger();