views:

860

answers:

13

How should I log exceptions? I never tried logging in .NET before. Nor try to dump exceptions to a txt (or binary) file. I dont require a text file, just a way to view the logs with the file and line #.

-edit- using asp.net

+7  A: 

log4net is quite a beautiful and standard system to use. Well worth learning, and you can log to a variety of formats (or custom). (Edit: However, as far as I know, it's not supported on Silverlight yet, if that interests you, so you may need to implement your own system there).

Noon Silk
+2  A: 

The enterprise library logging application block is one option. It might be more than you need (or less), but it has worked well for me.

Eric Mickelsen
+14  A: 

ELMAH is particularly nice if you're using ASP.NET. One of my favorite features about ELMAH is not having to build the UI to view logged exceptions; ELMAH provides a handler that will report your exceptions. However, I've never used it for logging anything other than exceptions.

log4net, and Microsoft's EntLib are some other ideas that come to mind. These provide a lot more functionality than what you may need if all you really need is just exception logging.

Also, you may consider performance counters if what you're really trying to do is measure your application. Again, the benefit of using performance counters is that after publishing the data, you won't have to worry about build the UI to view/query the data.

Esteban Araya
Excellent answer. I DL'd, played and implemented elmah into my app already. Thanks :)
acidzombie24
@acidzombie: I'm glad ELMAH is working out for you. It's pretty AWESOME. :)
Esteban Araya
+1 for ELMAH. An awesome solution. If you find that you're using ELMAH for more than 1 application, check oug the ASP.Net Exception Reporter at CodePlex http://aspexceptionreporter.codeplex.com/ It's a centralized ELMAH aggregation application.
JamesEggers
ELMAH is fantastic. Love it. Use it everywhere.
Chris Lively
@JamesEggers: Nice! I'm glad you mentioned it. We were about to start building something just like it for all of our web apps.
Esteban Araya
Can this work for WCF service hosted on IIS?
Ismail
+3  A: 

For logging exceptions, check out the project: ELMAH

For other types of logging, you can't go past Log4Net.

Pure.Krome
Actually you can create your own exception class, log that, and have ELMAH ignore it for purposes of emailing you when real problems occur... So, I'd still go with it as well.
Chris Lively
+1  A: 

Log4Net is great and quick to learn. For lots of extra features and visualizations, there is Exceptioneer.

Jay
A: 

I agree with those that mentioned ELMAH.

The only fault with ELMAH is that it can't log exceptions that happen outside of an actual user initiated request. So if you use timers or have a lot of code running out of applicaiton_start type events you'll have to manually log those exceptions. The best part of ELMAH is that it is very easy to setup, and comes with a UI so you can actually look at the logs (why none of the other loggers seem to come with a UI is quite beyond my understanding).

Log4Net is a much more complete logging solution, and is especially good if you want to do diagnostic logging, informational logging, or otherwise want to log things that "are not errors". Easier to configure and use then Enterprise Library.

Enterprise Library's logging component is also popular, but EntLib is also about as light-weight and subtle as a freight-train, especially if you aren't using the rest of what EntLib has to offer.

Stephen M. Redd
A: 

Link

  • log4net is very good option.
PRR
+1  A: 

NLog is pretty nice. It's easier to configure than log4net...

Carles
I second that! And it does a great job :)
Stormenet
A: 

It seems you're quite new to the programming and recently tried to figure out more advanced topics. If you're just interested to see the file and line# of the exception, then you certainly no need to use any logging utility such as Log4net or Microsoft Enterprise Library.

Now onwards, start putting your code in try-catch block and handle exceptions (if any) in catch block.

try
{

}
catch(Exception ex)
{
   //handle exception
}

Here in exception, you can find stack trace, which will give you an exact idea of which method and line you code failed.

Hope this helps.

Rajeev Ranjan Lall
the question is on "how to log exception" not "how to handle exceptions"
PRR
A: 

NLOG looks good .. but does it just log general messages .. not Exceptions ..? or is that what an InfoException is? e.g. in debug mode you might want to count and log the number of times a loop is iterated...

Also has anyone else had difficulty unzipping the Log4Net downloads on http://logging.apache.org/log4net/download.html

Adam
+1  A: 

Not a direct answer, but one thing we've found useful for logging problems with our own code was creating a base exception class (inherited from Exception) and automatically logging the exception details, using Log4Net (or whatever), in all of the derived exceptions.

Obviously this won't log other types of exceptions (BCL, 3rd party) but it is useful.

The other thing about logging exceptions, if you want the full stack trace, use Exception.ToString() rather than just Exception.Message.

Si
+3  A: 

Actually, since ASP.NET 2.0, you don't really have to log any exceptions. ASP.NET Health Monitoring does that for you. By default, it will log exceptions, as warnings, to the Application event log.

This process an be customized through configuration.

See ASP.NET Health Monitoring Overview.

John Saunders
+1  A: 

While we're plugging all sorts of logging frameworks here, let me plug CuttingEdge.Logging. It's simple to setup and integrates well with ASP.NET, but lacks a viewer such as ELMAH has.

Steven