views:

200

answers:

3

I want to add logging to the ASP.NET 2.0 web site that I've inherited from a previous developer. The application just spits out exceptions when they occur, and there's no record of financial transactions that have executed.

I've been looking at Log4net, which I've used in the past, as well as NLog and BitFactory. What I really need to know is how to best implement a logging framework in my application, which runs inside SharePoint. I need something that doesn't bog down the application.

Is there a generally accepted logging pattern for ASP.NET? How did you implement logging in your web site?

+2  A: 

The system that I use in a lot of my ASP.NET applications is ELMAH (Error Logging Modules And Handlers). Don't know if it will offer all of the features you want, but what I really like about it is getting the Yellow Screen of Death and stack trace via email whenever an unhandled exception occurs.

TheTXI
Man, that is really awesome. Thanks!
Robert S.
A: 

The best I have found is log4.net. We use it to log unhandled errors and for debugging ASP.NET. It's small and doesn't crash. We use it on a pretty large site and I'd use it again.

I don't think you read my question fully.
Robert S.
Sure I did. I was seconding log4net by telling you that we use it for that purpose and that it works well. You asked "How did you implement logging in your web site?" and that was what I tried to answer.
+1  A: 

Any logging you do is going to be slower than doing no logging. Logging isn't about performance, it's about reliability and recovery. The best logging framework is the one you use religiously, the worst one is the one you don't use.

We use log4net in our applications, no complaints and have never looked back. Are there things I'd like to tweak? Sure. But we're not in the logging business, we write applications for clients, so we need a reliable* logging system, and log4net fits.

Having said that, we do wrap our debug logs in a check for debug logging. And in core places only we went beyond the typical if (log.isDebugEnabled) check and added in our own static variable check on each if statement. At the top of modules where we really care about performance (mostly parts of our ORM or other infrastructure pieces) we have

#region Log4Net
const string c_EnableDebugLogging = "com.techsoftinc.BusinessObjectsCore.EnableDebugLogging";
static readonly bool _EnableDebugLogging = Convert.ToBoolean(ConfigurationManager.AppSettings[c_EnableDebugLogging] ?? "false");

static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

//Do we support ultrafast logging? See What is REALLY the FASTEST way of (not) logging? in http://logging.apache.org/log4net/release/faq.html
static bool debugLogging
{
   get { return _EnableDebugLogging && log.IsDebugEnabled; }
}
#endregion

and then when we want to debug we have (for example)

if (debugLogging)
   log.DebugFormat("Executing sql: {0}", sql);

If _EnableDebugLogging is false then the JIT will collapse the debugLogging function to a false and then remove the entire if (debugLogging) statement since it can never be true. The JIT is literally dropping our debug logging on the floor if we don't need it.

*reliable == works well in all situations we've encountered. Reliable does not mean truely reliable in the computer science sense of the term.

WaldenL