best way to log exceptions

G

Guest

Hi,

I have a class called Loger that has only a static method:

public class Logger
{
public static void LogException (Exception e)
{
//code to write the exception into a text file...
}
}

I have, for other hand a group of classes that does some operations and i
would like to log every exception inside each class.

Its a good way to call this static method every time the code execute a
catch statement?

for instance:

try
{
//do something
}
catch (Exception e)
{
Logger.LogException(e);
}

What do you think about it?
 
J

Joe Jax

B

Bruce Wood

While this doesn't directly bear on your question... IMHO logging
exceptions from catch blocks is often bad style.

The very fact that you're catching an exception means that your
application can somehow deal with it and keep going. If you have some
sort of application that has to keep running "no matter what," then
try...catch... log exception may be a reasonable approach. Nonetheless,
in most applications the very fact that you chose to catch an exception
means that, in some sense, you "know what to do about it," and
therefore why bother logging what you've successfully dealt with?

If you want to report all fatal exceptions in your application so that
they're logged in one place (so that users don't have to report the
gory details themselves), try looking at the
AppDomain.UnhandledException and Thread.ThreadException events. Let any
exceptions you don't know how to handle bubble up the stack until they
raise one of these events, and log them there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top