Exception destructuring in Serilog

前端 未结 3 1173
一个人的身影
一个人的身影 2021-02-05 02:59

Serilog has a convenient way of destructuring objects as shown in this example:

logger.Debug(exception, \"This is an {Exception} text\", exception);
logger.Debug         


        
3条回答
  •  长发绾君心
    2021-02-05 03:39

    There's a forum thread discussing this, in which a couple of solutions are presented. Thomas Bolon has created an 'exception destructuring' extension you can find in a Gist.

    In this case you use only this syntax:

    logger.Debug(exception, "This is an exception");
    

    There's no need to add the exception into the format string.

    To ensure the exception is printed to text sinks, just make sure {Exception} is included in the output template. The standard built-in ones already have this, e.g.:

    outputTemplate: "{Timestamp} [{Level}] {Message}{NewLine}{Exception}";
    

提交回复
热议问题