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

Introduce a way to format Exceptions #26

Closed
oscarmorasu opened this issue Apr 12, 2019 · 4 comments
Closed

Introduce a way to format Exceptions #26

oscarmorasu opened this issue Apr 12, 2019 · 4 comments

Comments

@oscarmorasu
Copy link

In your current implementation exceptions are formatted as "ex.ToString()".
It would be great to format the Exception as JSON, spitting out the Message, Type, Method and StackTrace as JSON properties. The StackTrace could be formatted as a JSON array, where each line is one string in the array. You can see that concept in action in this PR.
This is extremelly helpful when aggregating exceptions by Type or Message in tools like Splunk.
I ended implementing this in my own ITextFormatter, but I think this should be part of the library.
If/when you decide to iterate on the JSON formatter, I would be more than happy to contribute with my solution.
Since Exceptions are a very important element of error logging, have you considered introducing an IExceptionFormatter, with a method FormatException, and sink authors could implement their own formatting (in this case JSON).

@nblumhardt
Copy link
Member

Hi @oscarmorasu , thanks for the note. SerilogExceptions is an add-on to Serilog that explores some of this space, and might be handy for you.

Unfortunately, in .NET, Exception is mutable, and is mutated when re-thrown. This makes it extremely difficult to deal with exception formatting nicely in sinks that work asynchronously. We have a couple of options for handling this and expect it'll be a part of "3.0" when that work eventually materializes (iterations on the core logging pipeline are deliberately very slow/conservative, but we'll do this eventually :-))

I think right now we can accept this as a data point, but can't move it forward in the short term.

Thanks!

@adamchester
Copy link
Member

I'd like to add a +1 data point here too.

For example, I think it would be a great local development experience if the console sink was able to easily highlight exceptions, and I'd prefer not to have to re-parse them from the ToString() output, it would be nicer if Serilog captured them as immutable structured data in the first place.

@sungam3r
Copy link
Contributor

@oscarmorasu I solved this problem with my own enrichers (essentially the same as Serilog.Exceptions).

@nblumhardt
Copy link
Member

Hey all! I think https://github.com/serilog/serilog-expressions now provides a reasonable place to consider hooking in this kind of functionality. I don't think it's something we'd tackle in Serilog.Expressions.

Since it's a broad and tricky thing to get right I think it's best to close this and just consider specific proposals. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants