Recently, I needed a logging block for a web project that I’m on. I initially wrote some code to log errors to the Event Log but the client changed this to also want logging to a text file.
I initially checked out PAG’s Logging Application Block and found it feature set to be very rich. It also takes a little extra effort and has some stuff that I don’t need. Specifically, one has to install EIF as a pre-requirement. That just seemed a little too much if I wanted some basic logging functionality.
After asking around, I was repeatedly pointed to Log4Net. This free library has several benefits and is easy to use. It works for any type of .NET application and runs on multiple .NET platforms (v1.0, v1.1, .NETCF, Mono, and SSCLI v1). It also has a variety of logging targets, a nice programmatic interface, is very configurable, and comes with lots of documentation. I admit that I feel strange recommending a different solution when Microsoft has one, but check out this comparison between EIF and log4net. Here is a couple of features that I like:
· Multiple loggers active
· Configurable, even at runtime
· A plethora of logging targets – and some are specific to ASP.NET
· Logger hierarchy
Here is a good article that helped me get started. And if you’re doing ASP.NET, do not forget to add the log4net attribute to Global.ASAX.