使用 Log4Net 记录 InnerException
如何使用 Log4NET 记录内部异常?
这是我当前的转换模式:
<conversionPattern value="%date [%appdomain] %-5level %logger [%property{NDC}] - %message%newline" />
How do I log inner exception with Log4NET?
This is my current conversion pattern:
<conversionPattern value="%date [%appdomain] %-5level %logger [%property{NDC}] - %message%newline" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控制台和文件附加器自动打印异常。异常:消息、堆栈跟踪和所有内部异常(再次使用堆栈跟踪)记录在单独的行上,并且不遵循转换模式。
我什至不确定您是否可以配置 log4net 不打印它。
更新:
可以将附加程序配置为不打印堆栈跟踪:
Log4Net - 仅注销异常堆栈跟踪对于某些文件
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.
Update:
It is possible to configure the appender to not print the stacktrace:
Log4Net - Logging out the Exception stacktrace only for certain files
参考: http://www.beefycode.com/post/Log4Net-Tutorial-pt-4-Layouts-and-Patterns.aspx
我相信您的异常将包含内部异常:
编辑:使用 ILog.Error () 方法而不是 ILog.ErrorFormat()。根据文档,ErrorFormat() 不接受要包含在日志事件中的异常对象
Reference: 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(). As per documentation, ErrorFormat() does not take an Exception object to include in the log event
刚刚设法在 Log4Net 版本 2.0 的源代码中验证了这一点,我可以看到没有考虑记录 InnerException。这是 Log4Net(版本 2)当前的限制,它不支持开箱即用的 InnerException 渲染。这就是您开始使用 NLog 的原因,因为它内置了对 InnerException 渲染的支持 - 链接
Just managed to verify this in the sourcecode for Log4Net version 2.0 and I can see there is no consideration for logging InnerException. That is the current limitation of Log4Net (version 2) that it does not support rendering of InnerException out of the box. That's why you would start using NLog as it has builtin support for InnerException rendering - link