开发者

How should I log exceptions in ASP.NET?

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


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.


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


For logging exceptions, check out the project: ELMAH

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


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


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.


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.


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


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.


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.


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.


  • Trace and Debug classes are very useful.

Link

  • log4net is very good option.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜