简体   繁体   English

使用Log4Net记录InnerException

[英]Logging InnerException using Log4Net

How do I log inner exception with Log4NET? 如何使用Log4NET记录内部异常?

This is my current conversion pattern: 这是我目前的转换模式:

<conversionPattern value="%date [%appdomain] %-5level %logger [%property{NDC}] - %message%newline" />

Console and File Appenders automatically print the exception. 控制台和文件添加程序会自动打印例外。 The exception: message, stack trace, and all inner exceptions (again with stack trace) are logged on separate lines and do not follow the conversion pattern. 异常:消息,堆栈跟踪和所有内部异常(同样是堆栈跟踪)记录在单独的行上,并且不遵循转换模式。

I am not even sure if you could configure log4net not to print it. 我什至不确定您是否可以配置log4net不打印它。

Update: It is possible to configure the appender to not print the stacktrace: Log4Net - Logging out the Exception stacktrace only for certain files 更新:可以将附加程序配置为不打印stacktrace: Log4Net-仅注销某些文件的异常stacktrace

%exception %例外

a formatted form of the exception object in the log entry, if the entry contains an exception; 日志条目中异常对象的格式化格式(如果条目包含异常); otherwise, this format expression adds nothing to the log entry 否则,此格式表达式不会向日志条目添加任何内容

Reference: http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx 参考: http : //www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx

I believe your exception would contain the inner exception: 我相信您的异常将包含内部异常:

Edit: use the ILog.Error() method instead of ILog.ErrorFormat(). 编辑:使用ILog.Error()方法代替ILog.ErrorFormat()。 As per documentation , ErrorFormat() does not take an Exception object to include in the log event 根据文档 ,ErrorFormat()不会将Exception对象包含在日志事件中

Just managed to verify this in the sourcecode for Log4Net version 2.0 and I can see there is no consideration for logging InnerException. 刚刚设法在Log4Net 2.0版的源代码中对此进行了验证,我可以看到没有考虑记录InnerException。 That is the current limitation of Log4Net (version 2) that it does not support rendering of InnerException out of the box. 这是Log4Net(版本2)的当前限制,即它不支持开箱即用地呈现InnerException。 That's why you would start using NLog as it has builtin support for InnerException rendering - link 这就是为什么您将开始使用NLog的原因,因为它内置了对InnerException呈现的支持- 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM