-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
Make it easier to use default destructurer for custom exceptions #276
Comments
Not sure about the naming |
@kmcclellan Thanks for reaching out. Let me understand your case better. Correct me if I am wrong, please. You have some custom exceptions for which you intentionally would like to avoid capturing all the properties and would rather just log the "common" exception properties like Is this the correct description of what you are trying to achieve? If so, why exactly is reflection based destructurer wrong solution in your case? You want to hide sensitive data, save space, or for some other reason? |
@krajek That's a good question, actually, since it points to deeper functionality that I found unexpected. My case is that using Serilog;
using Serilog.Exceptions;
using Serilog.Formatting.Json;
using System;
namespace SerilogExceptionDestructuring
{
class Program
{
static void Main()
{
Log.Logger = new LoggerConfiguration()
.WriteTo.Console(formatter: new JsonFormatter())
.Destructure.ByTransforming<Token>(
x => new { x.Id, Token = new string('*', x.Value.Length) })
.Enrich.WithExceptionDetails()
.CreateLogger();
try
{
var token = new Token { Id = 1, Value = "Don't show me!" };
Log.Information("Token created: {@Token}", token);
var exception = new TokenException();
exception.Data["@Token"] = token;
throw exception;
}
catch (Exception exception)
{
Log.Error(exception, "An exception occurred");
}
}
}
class Token
{
public int Id { get; set; }
public string Value { get; set; }
}
class TokenException : Exception { }
} {"Timestamp":"2020-12-23T14:41:15.2260722-05:00","Level":"Information","MessageTemplate":"Token created: {@Token}","Properties":{"Token":{"Id":1,"Token":"**************"}}}
{"Timestamp":"2020-12-23T14:41:15.2804143-05:00","Level":"Error","MessageTemplate":"An exception occurred","Exception":"SerilogExceptionDestructuring.TokenException: Exception of type 'SerilogExceptionDestructuring.TokenException' was thrown.\r\n at SerilogExceptionDestructuring.Program.Main() in C:\\Users\\kmcclellan\\sandbox\\SerilogExceptionDestructuring\\Program.cs:line 26","Properties":{"ExceptionDetail":{"Data":{"@Token":{"Id":1,"Value":"Don't show me!"}},"HResult":-2146233088,"Message":"Exception of type 'SerilogExceptionDestructuring.TokenException' was thrown.","Source":"SerilogExceptionDestructuring","Type":"SerilogExceptionDestructuring.TokenException"}}} You'll notice that "Don't show me!" appears in the {"ExceptionDetail":{"Type":"System.Exception","Data":{"@Token":{"Id":1,"Token":"**************"}},"HResult":-2146233088,"Message":"Exception of type 'System.Exception' was thrown.","Source":"SerilogExceptionDestructuring"}}} This is because the base |
+1 for this "For any exception destructurer, arguably, the core IDestructuringPolicys should take precedence for non-exception values within the exception tree. If ReflectionBasedDestructurer adhered to this, it would work fine for my case." :) |
What about adding some kind of GlobalExceptionDestructurer, which would handle all not handled exceptions? This option could be added here: like:
|
Describe the feature
If an exception type isn't recognized by this library, the fallback is to use the
ReflectionBasedDestructurer
. It's a common case to have custom exception types that I want to use with the baseExceptionDestructurer
instead. Currently there's no easy way to configure this to occur.My current workaround
While this isn't much code, it's counterintuitive and required analysis of this library's internals. It'd be better to have more obvious support for this use case.
Possible enhancements
Probably the simplest way to address this would be to leverage the existing
IDestructuringOptions.DisableReflectionBasedDestructurer
. Currently, setting this option causes the enricher to returnnull
for unrecognized exception types. It could be changed to fall back toExceptionDestructurer
instead (I doubt anyone is depending upon thatnull
value).Alternatively (or additionally), the options could support selecting between
ExceptionDestructurer
andReflectionBasedDestructurer
for individual types. Something like this maybe:Let me know if there is interest in a PR for one or both of these enhancements.
The text was updated successfully, but these errors were encountered: