Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Library does not log extra exception detail for Console Logger#86

Answeredbykrajek
eloisetaylor5693 asked this question inQ&A
Discussion options

When using this setup, the extra exception properties are not output

private static readonly ILogger Logger = new LoggerConfiguration()            .Enrich.WithExceptionDetails()            .WriteTo.Console()            .CreateLogger();
You must be logged in to vote

Hi@eloisetaylor5693 , thanks for reaching out to us.

Serilog.Sinks.Console sink's default output template does not display enriched properties at all. Not forSerilog.Exceptions and not for any other sink (like standardEnrich.FromLogContext() orEnrich.WithProperty).

You need to specify custom template includingProperties like that:

.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception} {Properties:j}")

Then your output will look like that:

2018-12-17 07:30:13.045 +01:00 [ERR] An error occurredSystem.Exception: An exception message.   at SerilogExceptionsLibUsingConsoleLogger.Program.ThrowAnException() in C:\Users\kraj…

Replies: 4 comments

Comment options

Strange, could you prove a very small reproduction?

You must be logged in to vote
0 replies
Comment options

Simple example:

using Serilog;using Serilog.Exceptions;using System;namespace SerilogExceptionsLibUsingConsoleLogger{    public static class Program    {        private static readonly ILogger Logger = new LoggerConfiguration()            .Enrich.WithExceptionDetails()            .WriteTo.Console()            .CreateLogger();        public static void Main()        {            try            {                ThrowAnException();            }            catch (Exception ex)            {                Logger.Error(ex, "An error occurred");            }            finally            {                Console.ReadLine();            }        }        private static void ThrowAnException()        {            var exception = new Exception("An exception message.");            exception.Data.Add("someData", "This is some exception data");            throw exception;        }    }}

With packages:

<?xml version="1.0" encoding="utf-8"?><packages>  <package version="2.7.1" targetFramework="net461" />  <package version="4.1.0" targetFramework="net461" />  <package version="3.1.1" targetFramework="net461" /></packages>

Gives this output:

[15:10:10 ERR] An error occurredSystem.Exception: An exception message.   at SerilogExceptionsLibUsingConsoleLogger.Program.ThrowAnException() in C:\Code\SerilogExceptionsLibUsingConsoleLogger\SerilogExceptionsLibUsingConsoleLogger\Program.cs:line 35   at SerilogExceptionsLibUsingConsoleLogger.Program.Main() in C:\Code\SerilogExceptionsLibUsingConsoleLogger\SerilogExceptionsLibUsingConsoleLogger\Program.cs:line 18

But I would have expected to see more exception detail than exception.ToString() gives. For example I would expect to see the data properties in the logged error, based on the documentation for this library.

You must be logged in to vote
0 replies
Comment options

krajek
Dec 17, 2018
Collaborator

Hi@eloisetaylor5693 , thanks for reaching out to us.

Serilog.Sinks.Console sink's default output template does not display enriched properties at all. Not forSerilog.Exceptions and not for any other sink (like standardEnrich.FromLogContext() orEnrich.WithProperty).

You need to specify custom template includingProperties like that:

.WriteTo.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception} {Properties:j}")

Then your output will look like that:

2018-12-17 07:30:13.045 +01:00 [ERR] An error occurredSystem.Exception: An exception message.   at SerilogExceptionsLibUsingConsoleLogger.Program.ThrowAnException() in C:\Users\krajewa1\source\repos\ConsoleExceptions\ConsoleExceptions\Program.cs:line 40   at SerilogExceptionsLibUsingConsoleLogger.Program.Main() in C:\Users\krajewa1\source\repos\ConsoleExceptions\ConsoleExceptions\Program.cs:line 22 {"PROP": "VALUE", "ExceptionDetail": {"Type": "System.Exception", "Data": {"someData": "This is some exception data"}, "HResult": -2146233088, "Message": "An exception message.", "Source": "ConsoleExceptions"}}

What's more, many other sinks are the same. On the other hand, I think that you got a point, that we could be explicit about it in the docs. I will add some guidance regarding the topic.

Hope that helps.

You must be logged in to vote
0 replies
Answer selected byeloisetaylor5693
Comment options

krajek
Dec 17, 2018
Collaborator

I have extended theREADME.md with short description and an example in84b62d1.
That should help, thanks again@eloisetaylor5693 .

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
3 participants
@eloisetaylor5693@RehanSaeed@krajek
Converted from issue

This discussion was converted from issue #86 on December 15, 2020 15:24.


[8]ページ先頭

©2009-2025 Movatter.jp