Logging With Log4Net



Last updated: January 28th, 2024

Introduction

Log4Net is a very powerful way to log messages in a .NET application. These messages include exception objects, strings and much more detail such as the thread ID. With Log4Net, messages are able to be logged to a variety of storage locations, including the file system and databases. It's possible to make custom appenders to store to other locations as well. For a more thorough tutorial on Log4Net, please read this Log4Net tutorial (includes sample code). My purpose here is to illustrate the most basic approach to using Log4Net with the file system and a SQL Server database in an ASP.NET application. The first thing to do is to install Log4Net as a NuGet package into the projects that you want logging in.

Setting Up Database Storage

I want to store Log4Net messages in a SQL Server database. To do that I need to make a table and stored procedure (any raw SQL statement will do).

Appenders

Now that I have my table and a stored procedure to pass values to, I need to set up some details in my web.config file. One thing to point out with the AdoNetAppender is that the bufferSize is set to 1. This is the count of messages that must be in the queue before writing them to the database. Increasing this number may help for performance but it's very confusing at first to not see log messages come in, that is why I've set it to 1.

Setup includes adding the configuration for Log4Net in the web.config:

After this I need to add the actual Log4Net element so that I can add the appenders which specify how and where the logging data will go (also in the web.config):

Calling Loggers

After the appenders are in place to catch the exceptions and messages, some configuration is required when the application starts up so that messages can be received by the appenders. In any class that logging should take place, the ILog interface needs to be called once per class file. From there, any call to the logger will expose the methods such as Info, Debug and others that will be picked up by the logger. The meanings of these methods is user defined through the appenders.

The following needs to be added the Global.asax file's application start event to use the appenders:

Common Issues

Some common issues that might occur are the buffer size being too high to see the log messages in the SQL appender. Another issue that I have run into is seeing all the messages that are written. Log4Net will only log 1 message per thread request so if there is a message that is logged and then another message that is logged later, the later one is the one that will be seen.

Conclusion

Now I have the ability to log exceptions and other messages to a file and a database. I can call the logging methods whenever I want to log my own custom messages. I could extend the logging abilities with new appenders if I wanted. Any project that is referenced in my assembly that is using Log4Net will be able to pass its messages to my appenders and log them. For example, the PayPal SDK uses Log4Net, as long as both the web application and the class library are using the same DLL and the logging level matches, I'll be able to see messages in that library in my database. There are lots of more advanced uses of Log4Net, here the point was to get going quickly and use the two most common appenders, logging to a file and a database. See Log4Net's homepage for more details on the way it works.


Comments

No Comments

Post Comment

Prove you are human 2 + 2 =

Tagged: Log4Net


Join my email list!



ryan
About Me

With 15 years in tech, I've excelled as a senior software engineer, specializing in ASP.NET, C#, SQL, Azure, and front-end technologies. I've led diverse projects across various sectors, from startups to global corporations, particularly during my decade in the San Francisco Bay Area.


Sign Up With SoftSys Hosting! (My host)