What is Log4Net?
log4net is a logging library for .NET applications, which provides a flexible and configurable framework for logging messages from your application to various outputs such as consoles, files, databases, and more.
Working with Log4Net in C#
To start with log4net in a C# (.NET 8) application, We will need to follow these steps to be done:
Step 1: Create a Console Application
Open "Visual Studio" -> "Create a new project" -> Select "Console App" -> Enter project name as "LoggingApp" and click Next.
Step 2: Install log4net Package
Then, we need to install the log4net
package via NuGet Package Manager or using the .NET CLI. In Visual Studio, we can do this by right-clicking on our project in Solution Explorer, selecting "Manage NuGet Packages...", searching for log4net
, and installing it as mentioned below,
Step 3: Configure log4net in Project
We need to configure log4net to specify how logs should be output (e.g., to a file, console, etc.), so we add a new XML file named log4net.config
to our project with the following details:
<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<file value="application.log" />
<appendToFile value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
We need to make sure the Build Action of log4net.config
is set to "Content" and Copy to Output Directory is set to "Copy if newer" or "Copy always".
Step 4: Configure log4net in Program.cs
Also, we will have to modify the Program.cs
to configure log4net and use it for logging:
using log4net;
using log4net.Config;
using System;
namespace LoggingApp
{
class Program
{
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
static void Main(string[] args)
{
// Configure log4net using the config file
XmlConfigurator.Configure(new System.IO.FileInfo("log4net.config"));
// Log debug message
log.Debug("This is a debug message");
// Log info message
log.Info("This is an info message");
// Log warning message
log.Warn("This is a warning message");
// Log error message
log.Error("This is an error message");
// Log fatal message
log.Fatal("This is a fatal error message");
Console.WriteLine("Logging done. Press any key to exit...");
Console.ReadKey();
}
}
}
Step 5: Run the Application
Once we build and run the application. we will see the application.log file in the debug folder as shown below,
The log messages are printed to the "application.log" file according to the logging levels specified in log4net.config
as shown below,
2024-06-18 17:47:06,752 [1] DEBUG LoggingApp.Program - This is a debug message
2024-06-18 17:47:06,765 [1] INFO LoggingApp.Program - This is an info message
2024-06-18 17:47:06,765 [1] WARN LoggingApp.Program - This is a warning message
2024-06-18 17:47:06,766 [1] ERROR LoggingApp.Program - This is an error message
2024-06-18 17:47:06,766 [1] FATAL LoggingApp.Program - This is a fatal error message
Comments (0)