Comments (4)
@krajek That's a good question, actually, since it points to deeper functionality that I found unexpected.
My case is that Exception.Data
contains instances of models for which I've configured custom destructuring policies. They destructure correctly for the standard exceptions, but are overridden by the ReflectionBasedDestructurer
.
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 TokenException
details. Throwing a base Exception
instead has the desired behavior:
{"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 ExceptionDestructurer
hands the exception Data
dictionary as-is to the property bag, rather than trying to destructure it. For any exception destructurer, arguably, the core IDestructuringPolicy
s should take precedence for non-exception values within the exception tree. If ReflectionBasedDestructurer
adhered to this, it would work fine for my case.
from serilog.exceptions.
+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." :)
from serilog.exceptions.
Not sure about the naming WithDefaultException
but that method seems like a reasonable idea. How about something like ConfigureException(bool useReflection)
? Thoughts @krajek?
from serilog.exceptions.
@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 Message
and StackTrace
. In your case, the flexibility of reflection based destructurer is a downside.
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?
from serilog.exceptions.
Related Issues (20)
- Separated Column or Porperty for exception stacktrace HOT 2
- Exception with redefined property(new modifier) stops enrichment altogether HOT 13
- Dependency Dashboard
- Not showing Exception stack trace in "Exception" field of the log HOT 13
- Add destructurer for Refit ApiException HOT 4
- Filtering out any property from a direct property of ExceptionDetails HOT 3
- Create Custom Destructurers for More Exceptions HOT 7
- mapper [NpgsqlValue] cannot be changed from type [date] to [ObjectMapper] HOT 3
- EF6 - protection against logging entire db. HOT 1
- Entity Framework Logging Behaviour HOT 2
- Exceptions with large properties HOT 3
- Same Key Added Exception in ExceptionPropertiesBag HOT 5
- [TaskCanceledExceptionDestructurer] Destructured object type inconsistent HOT 1
- Maximum destructuring depth reached. HOT 3
- Demystified exception stack trace support HOT 1
- Serilog.Exceptions.EntityFrameworkCore docs warning unclear HOT 1
- Request that Serilog.Exceptions.EntityFrameworkCore description also includes Serilog appsetting.json example HOT 1
- FOSSA Compliance tool license missing for v4.1.0 HOT 1
- Exceptions not getting logged in Azure App Insights due its size
- Please update version of System.Data.SqlClient in Serilog.Exceptions.SqlServer
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serilog.exceptions.