Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing stack trace to log file only when exception thrown

I want to write the stack trace only when I have exceptions, currently I do it like this

 layout="${longdate}|${level}|${message} ${exception:format=tostring} | ${stacktrace}"

So I always get it in my log file.

EDIT:

I use this layout for all my logging, so when I dont have any exceptions I also get the stack trace.But I need it only when i have some exception

when I have an exception I have following output, and it what I need

2011-07-01 22:59:02.3782|Debug|fffffffffffffffffffffffffffff System.Exception: Exception of type 'System.Exception' was thrown. | AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main

But without exception :

2011-07-01 22:57:26.7117|Trace|fffffffffffffffffffffffffffff  | AppDomain.ExecuteAssembly => AppDomain._nExecuteAssembly => Program.Main

But I want only

2011-07-01 22:57:26.7117|Trace|fffffffffffffffffffffffffffff

Need ideas how to do so...

like image 359
Night Walker Avatar asked Jul 01 '11 20:07

Night Walker


People also ask

How do I print exception stack trace in log file?

To print a stack trace to log you Should declare logger and method info(e. toString()) or log(Level.INFO, e. toString()). Logging is the process of writing log messages during the execution of a program to get error and warning messages as well as info messages.

Should stack traces be logged?

Logging the stack traces of runtime exceptions assists developers in diagnosing runtime failures. However, unnecessary logging of exception stack traces can have many negative impacts such as polluting log files.

Should you log before throwing exception?

It is recommended for you to use specific exceptions when declaring catch and throw clauses. For debugging purposes, you should ensure that you log your exceptions.

Which log file should a developer use to search for exception stack traces?

Initially, developers should look in the application logs for the stack trace, because often, the stack trace tells you the exact line or function call that caused a problem.


1 Answers

you can use the following layout:

${longdate}|${level}|${message} ${onexception:${exception:format=tostring} | ${stacktrace}}
like image 60
ogun Avatar answered Oct 26 '22 23:10

ogun