Logging and exceptions

  • Thread starter Thread starter Andy
  • Start date Start date
A

Andy

Hi all,

I'm wondering about when the best time to log an exception would be
(such as to the event log, or a text file, etc.)

If you have a multi tier application, and one of the lower layers
throws an exception, should that layer log the exception and bubble it
up (causing all higher levels to also log)? Or should the exception be
allowed to move up to the application and the application is solely
responsible for logging?

What has worked well for you?

Thanks
andy
 
Andy,

Personally, if there is an exception, I like to bubble it up through the
layers (I think you are in agreement on this). As for when to log, I would
think that when a call traverses layers, you should log the exception, and
then allow it to bubble up. This would require you to place catches at each
point where your layers are accessed, which can be tedious.

However, if you are using a technology where the exception is
automatically handled for you (like remoting, where if there is an
exception, that exception will be thrown on the client side), it might not
be such a bad idea to let the client handle this kind of stuff.

Hope this helps.
 
Nicholas,

Thanks for the input.

Yes, I am definatly going to bubble up the exceptions. As each
exception is caught by a layer, it wraps it in an Exception declared in
that layer. (I believe this is what is commonly done as well, but I'd
appreciate comments on this practice as well).

Andy
 
Andy,

This is pretty much what other technologies do. For example, if an
exception is thrown on the server, then that exception is caught, and then
placed in a RemotingException (the original exception is exposed through the
InnerException property). This way, if there is a problem in the remoting
itself, you can detect it through any exception except a RemotingException.
 
Speaking of logging,

Hello, I'm new to C#. I'm using Visual Studio and IIS, developing and
running web projects with C#. In this environment, when I execute

Console.writeln("Message");

the message does not appear in any window of my Vis. Studio that I know of,
and does not log anywhere that I know of.

How can I configure IIS to log these Console.writelns to a specific place
and/or configure visual studio to write them to a console window?

Thanks for any help,

Andrew

Nicholas Paldino said:
Andy,

This is pretty much what other technologies do. For example, if an
exception is thrown on the server, then that exception is caught, and then
placed in a RemotingException (the original exception is exposed through the
InnerException property). This way, if there is a problem in the remoting
itself, you can detect it through any exception except a RemotingException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andy said:
Nicholas,

Thanks for the input.

Yes, I am definatly going to bubble up the exceptions. As each
exception is caught by a layer, it wraps it in an Exception declared in
that layer. (I believe this is what is commonly done as well, but I'd
appreciate comments on this practice as well).

Andy
 
Oasis,

You shouldn't be using Console.WriteLine for ASP.NET applications.
Those messages are just going to be lost.

If you need to log certain things, you can attach listeners to the Trace
object (there are static methods to do this, and the class is in the
System.Diagnostics namespace). This will allow you to place messages in
your program to be written to the listeners. You can have the listeners
write output to a log file, a database, or whatever you wish. Additionally,
you can configure this without having to write any code (it would be
configured through the web.config file).

Take a look at the TraceListener class documentation for how configure
the trace listeners, as well as a list of pre-defined trace listeners.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oasis said:
Speaking of logging,

Hello, I'm new to C#. I'm using Visual Studio and IIS, developing and
running web projects with C#. In this environment, when I execute

Console.writeln("Message");

the message does not appear in any window of my Vis. Studio that I know
of,
and does not log anywhere that I know of.

How can I configure IIS to log these Console.writelns to a specific place
and/or configure visual studio to write them to a console window?

Thanks for any help,

Andrew

Nicholas Paldino said:
Andy,

This is pretty much what other technologies do. For example, if an
exception is thrown on the server, then that exception is caught, and
then
placed in a RemotingException (the original exception is exposed through
the
InnerException property). This way, if there is a problem in the
remoting
itself, you can detect it through any exception except a
RemotingException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Andy said:
Nicholas,

Thanks for the input.

Yes, I am definatly going to bubble up the exceptions. As each
exception is caught by a layer, it wraps it in an Exception declared in
that layer. (I believe this is what is commonly done as well, but I'd
appreciate comments on this practice as well).

Andy
 
Nicholas,


thank you very much for pointing me in the right direction!

oasis

Nicholas Paldino said:
Oasis,

You shouldn't be using Console.WriteLine for ASP.NET applications.
Those messages are just going to be lost.

If you need to log certain things, you can attach listeners to the Trace
object (there are static methods to do this, and the class is in the
System.Diagnostics namespace). This will allow you to place messages in
your program to be written to the listeners. You can have the listeners
write output to a log file, a database, or whatever you wish. Additionally,
you can configure this without having to write any code (it would be
configured through the web.config file).

Take a look at the TraceListener class documentation for how configure
the trace listeners, as well as a list of pre-defined trace listeners.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Oasis said:
Speaking of logging,

Hello, I'm new to C#. I'm using Visual Studio and IIS, developing and
running web projects with C#. In this environment, when I execute

Console.writeln("Message");

the message does not appear in any window of my Vis. Studio that I know
of,
and does not log anywhere that I know of.

How can I configure IIS to log these Console.writelns to a specific place
and/or configure visual studio to write them to a console window?

Thanks for any help,

Andrew

Nicholas Paldino said:
Andy,

This is pretty much what other technologies do. For example, if an
exception is thrown on the server, then that exception is caught, and
then
placed in a RemotingException (the original exception is exposed through
the
InnerException property). This way, if there is a problem in the
remoting
itself, you can detect it through any exception except a
RemotingException.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,

Thanks for the input.

Yes, I am definatly going to bubble up the exceptions. As each
exception is caught by a layer, it wraps it in an Exception declared in
that layer. (I believe this is what is commonly done as well, but I'd
appreciate comments on this practice as well).

Andy
 
Back
Top