log4NET 如何知道触发日志事件的行数
我很好奇 log4NET 如何使用 %L 来获取触发日志事件的行号。你能给我一些详细说明吗?
谢谢。
I am curious how does log4NET can use %L to get the # of line where a log event is fired. Could you give me some elaboration?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它使用 StackTrace,如log4net 文档中所述。
请注意 lognet 文档中的警告:
警告生成呼叫者位置信息的速度非常慢。除非执行速度不是问题,否则应避免使用它。
注意
System.Diagnostics.StackTrace 类对发布版本有这样的说法:
“对于调试构建配置,StackTrace 信息将提供最丰富的信息。默认情况下,调试构建包含调试符号,而发布构建则不包含。调试符号包含在由于优化期间发生的代码转换,构造 StackFrame 和 StackTrace 对象可能不会报告预期的方法调用。”
这意味着在发布版本中,调用者信息可能不完整或根本不存在!因此,在发布版本中不能依赖调用者位置信息。
It uses a StackTrace as described in the log4net documentation.
Note the caveats in the lognet documentation:
WARNING Generating caller location information is extremely slow. Its use should be avoided unless execution speed is not an issue.
CAUTION
The System.Diagnostics.StackTrace class has this to say about Release builds:
"StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects. StackTrace might not report as many method calls as expected, due to code transformations that occur during optimization."
This means that in a Release build the caller information may be incomplete or may not exist at all! Therefore caller location information cannot be relied upon in a Release build.