views:

322

answers:

3

Hi all...

I'm using Log4Net to log a multilayered enterprise application. I know that when i log with exception Log4Net automatically exposes the exception StackTarce, but i want to log the stacktrace for every log even if those are not exception throws. Why i need that?... Simply, i want to know the call origin of the log (drilldown the layers...)

Thank all...

Tiago Dias

A: 

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:

http://stackoverflow.com/questions/1906227/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.

Dag
A: 
try { throw new SomeExceptionType(); } catch (SomeExceptionType ex) { logger.Debug("", ex); }

You could override the normal logging method with this.

I am not very serious though, please don't downvote :)

HeavyWave
Indeed. But, you don't have to throw an exception just to get the stacktrace, use the `StackTrace` class directly. http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx
Peter Lillevold
Hi... thank u for your sugestion but i see performance penalty in your methodology plus i've tried your sugestion and the StackTrace was not present in my new throwed exception (don't know why)... But i did solve my problem... look under.
TiagoDias
A: 

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..

TiagoDias