在Serilog异常解构 [英] Exception destructuring in Serilog

查看:324
本文介绍了在Serilog异常解构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Serilog具有解构对象的一种便捷方式如本例所示:

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

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

第一行引起记录异常以纯文本的记录(通过调用的ToString()),第二行导致记录器编写异常性质作为单独的字段。但是,这个过载的内容:

The first line causes the logger to log an exception as plain text (by calling ToString()), and the second line causes the logger to write exception properties as separate fields. But what about this overload:

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



这其中需要一个例外,作为第一个参数,它总是作为字符串写入。我想提出可能是启用日志记录的异常结构化的方式。是否有可能配置Serilog实现这一目标?

This one takes an exception as its first argument, and it is always written as a string. What I would like to make possible is to enable logging exception in a structured way. Is it possible to configure Serilog to achieve this?

更新。我想这个问题导致了日志记录异常的另一个方面:我怎样才能确保邮件与异常性丰富(因此它们被记录在一个结构化的方式来富汇像Elasticsearch),而无需编写所有的异常属性来呈现的文本消息(所以纯文本记录器没有填充的异常细节堆积如山)

UPDATE. I guess this question leads to another aspect of logging exceptions: how can I ensure that messages are enriched with exception properties (so they are logged in a structured way to the rich sinks like Elasticsearch) without writing all exception properties to the rendered text message (so plain text loggers are not filled with huge piles of exception details).

推荐答案

有一个的forum螺纹讨论这个时,其中一对解决方案的介绍。托马斯·博隆创造了一个例外解构'扩展,你可以找到一个要点

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}";

这篇关于在Serilog异常解构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆