Exceptions and Tracing

  • Thread starter Thread starter John Bowman
  • Start date Start date
J

John Bowman

Hi All,

For debugging purposes I'm trying to figure out how to make any and all
Exceptions (Exceptions or AppExceptions or custom exceptions) be
automatically logged to a text logging file. Is this possible? I've figured
out how to inherit my own AppException that basically incorporates the
TextWriterTraceListener class. This allows custom AppExceptions that I might
explicitly throw to be automatically logged. But how can I get the same type
of behavior for any other exception types that are thrown, which I do not
explicitly throw? If I'm going about this all wrong, please point me in the
right direction. Any example code would also be very helpful.

TIA,
 
not too sure about what exactly you wanted, but if you want to catch
all exceptions, just wrap a try/catch block around your code like so...

try
{
// your code here
}
catch(Exception ex)
{
// write ex.message to log file
}
 
John Bowman said:
For debugging purposes I'm trying to figure out
how to make any and all Exceptions (Exceptions
or AppExceptions or custom exceptions) be
automatically logged to a text logging file. Is this
possible?

I don't think so. Well, you can put a try-catch block in your Main
method, and do the logging there, but of course you won't be able to
continue your program where it left off afterwards. You can't
magically associate logging with any standard exception being created.
(If I understand correctly, this is the the sort of situation that the
new concept of "aspect-oriented programming" will be able to address,
but I suppose we won't see anything like that in C# for a few years!)

P.
 
Hi,

This might help - attach handlers to unhandled exception events like:
Application.ThreadException
AppDomain.UnhandledException

But in this case, handling will have to occur centralized in these handlers
(which I am not sure is applicable in your project).
 
Hi All,

Thanks for the info. Your answers more or less confirmed my suspicions. I'll
just have to log what/where I can.

John
 

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

Back
Top