Using log4net in Your Azure App Service Applications

Maroun Maroun
Geek Culture
Published in
3 min readSep 12, 2021

In this article, we’ll configure our log4net to write our logs to Kudu, so they’ll be available for us to digest at any moment.

Image by hudsonhintze on Unsplash

What is log4net?

log4net is a logging utility for .NET applications, based on Java’s popular log4j. Here’s how Apache describes it:

The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the .NET runtime. For more information on log4net see the features document.

Exploring the powerful features of this tool is beyond the scope of this article, the only thing you need to know, is that log4net allows us to write application logs in a highly-customizable way.

Setup log4net in ASP.NET

Add the Log4Net plugin using NuGet

PM> Install-Package log4net -Version 2.0.12

Next, in order to configure Log4net as Microsoft Extensions Logging handler on our ASP.NET Core application, we need to install the below extension:

PM> Install-Package Microsoft.Extensions.Logging.Log4Net.AspNetCore -Version 5.0.3

At the project’s root level, create log4net.config file with the following content:

log4net.config

Let’s explain the relevant section for our logging to Kudu:

logfile appender section

In a nutshell, The appender is the section that’s responsible for sending the log messages to some destination. In our case, we’d like to send the logs to

D:\Home\LogFiles\Log4Net\application.log

As explained in Logging in .NET Core and ASP.NET Core, the default location for log files is in the D:\home\LogFiles\Application folder. In our case, we introduce an additional folder, Log4Net (that we’ll need to create in Kudu), that our logs will reside in.

Configuring log4net in the application

In the last part in configuring the log, we need to add the following to Program.cs:

Program.cs

That’s all! Let’s add some logs to our application:

SomeApplication.cs

Accessing the logs in the app service

As we mentioned before, we should create the Log4Net folder. Navigate to the Kudu console:

https://<your app name>.scm.azurewebsites.net/DebugConsole

and create a new folder as shown in the image below:

Creating the Log4Net folder

Now, publish your application, trigger the code, and you should see the newly created log file application.log with the lines “Hello user”.

Summary

If your application is already configured to work with log4net, and you’re moving to Azure app service, you can keep the library and only add an appender so your logs will be viewable from within the app service itself, without the need of changing the logging library.

Further reading:

--

--