10

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

8
  • 1
    Can you provide some more information on the type of application? Is it a client application (WinForms, WPF), web (ASP.NET Web Forms or MVC), Silverlight? Commented Jan 21, 2010 at 5:53
  • 1
    IMO, this question is not an exact dupe, to that link, Alastair. He specifically said exceptions . Personally, I log more than exceptions but I'm not about to start making assumptions here. "Stay On Target". Commented Jan 21, 2010 at 6:04
  • 1
    tinyurl.com/y9dj974 Quick Google Search gives quite a lot useful information. Commented Jan 21, 2010 at 6:18
  • 3
    @acidzombie24: but your question is #3 in the search results... Commented Jan 21, 2010 at 19:27
  • 2
    @Esteban: It's almost like a recursion problem which eventually might result in a .. well, you know. ;) Commented Jan 21, 2010 at 20:01

13 Answers 13

16

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent answer. I DL'd, played and implemented elmah into my app already. Thanks :)
@acidzombie: I'm glad ELMAH is working out for you. It's pretty AWESOME. :)
+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 aspexceptionreporter.codeplex.com It's a centralized ELMAH aggregation application.
ELMAH is fantastic. Love it. Use it everywhere.
@JamesEggers: Nice! I'm glad you mentioned it. We were about to start building something just like it for all of our web apps.
7

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).

Comments

3

For logging exceptions, check out the project: ELMAH

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

1 Comment

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.
3

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

2 Comments

I second that! And it does a great job :)
configured in only a few minutes....and the renderers make it that much better: github.com/nlog/nlog/wiki/Layout-Renderers
3

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.

Comments

2

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.

Comments

1

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

Comments

1

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.

Comments

1

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.

Comments

0

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.

Comments

0

Link

  • log4net is very good option.

Comments

0

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

Comments

-1

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.

1 Comment

the question is on "how to log exception" not "how to handle exceptions"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.