How do you add a StackTrace to every log call in log4net?

橙三吉。 提交于 2019-12-23 15:28:21

问题


I'm using Log4Net to log a multilayer-ed Enterprise Application.

I know that when I log an exception with Log4Net, it automatically exposes the exception StackTrace, but I want to log the StackTrace for every log call, even if those are not exception throws.

Why do I need that?... I want to know the call origin of the log (drilldown the layers...)

Thank all...

Tiago Dias


回答1:


I came to a solution to my problem. I've wrap around the log4net in my own methods and i've created LoggingEvent intances. In each instance i've used a property with Environment.StackTrace.

This way i have StackTrace foreach log event in my application, even without exceptions being throwned.

Thank U all..




回答2:


You can get the caller method name and line number using %location, but not the entire stack trace without extending log4net. Check responses to this question:

Does log4net support including the call stack in a log message

Also check the PatternLayout documentation page on apache.org for other location details you can output:

http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

Not sure if this still applies on modern computers, but the log4net documentation warns that generating caller information is costly.



来源:https://stackoverflow.com/questions/2606514/how-do-you-add-a-stacktrace-to-every-log-call-in-log4net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!