Getting Log4Net working with a .NET Core console app is simple. You may have found guides for getting Log4Net working on a .NET Framework project. The setup is similar but requires a few additional steps. This quick start assumes you are using Visual Studio 2019.
This process is the same for installing any other package using NuGet. Either the Package Manager Console or the Manage NuGet Packages GUI can be used.
Open the Package Manager Console and run the following command.
Install-Package log4net -Version 2.0.8
In solution explorer, right click on your project and select 'Manage NuGet Packages'.
In the Browse section, search for 'Log4Net', select the latest version and press install.
We are going to create a configuration file that will make log4net log all levels off logs to the console appender. You can learn more about levels and different types of appenders here.
In the root of your project create a new file called log4net.config
. This file should be completely empty.
Add the following code to the file. Below I have explained what this does.
<?xml version ="1.0" encoding="utf-8"?>
<configuration>
<log4net>
<root>
<level value = "ALL"/>
<appender-ref ref="Console"/>
</root>
<appender name ="Console" type="log4net.Appender.ConsoleAppender">
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value="%-5p %d{hh:mm:ss} %message%newline" />
</layout>
</appender>
</log4net>
</configuration>
Within the <log4net>
tag there are two important sections, <root>
& <appender>
.
This is where we can define what level of logs should be displayed and what appender to use.
You will notice that we have set the value of the level tag to 'ALL'. This means that all logs will be displayed. This value can be changed to one of 7 values which will display varying levels of logs. These levels are:
- ALL
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
- OFF
The next tag you will see if the <appender-ref/>
tag. This tag has the attribute ref
which we have set to "Console"
. This refers to the name of the appender we have defined in the <appender>
tag.
This tag has 2 attributes, name
and type
.
We have set the value of name
to "Console"
. Notice this is the same as the value of ref
in the <root>
tag.
The value of type
has been set to log4net.Appener.ConsoleAppender
. This means that we want to use the Console Appender. There are many different Appenders including ColorConsole, which allows for the console to display the log in different colors based on the its level. If you want to log to a file, research the RollingFileAppender.
Ensure that you are refrencing log4net. To do this include the following using statements.
using log4net;
using log4net.config;
In your projects program.cs
file, add the following code.
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
Remember to ensure that the config file we created earlier is being copied to the bin folder. To do this open up your projects .csproj
file and enter the following code in the <ItemGroup>
block.
<None Update="log4net.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
We are almost at the end. In your program.cs
file add the following line.
var _log4net = log4net.LogManager.GetLogger(typeof(Program));
We can now start logging! To write an info log see the following code
_log4net.Info("Hello Logging World");
This will display "Hello Logging World" to the console when the application is run!
You are now able to start logging in your project. There are a wide range of different options for logging within Log4Net which are defiantly worth researching. This is only the beginning!
Useful article. Thank you for providing your help.
May I apologise if I offend but I see a typo on the using statements. It is written as:
but should be
We also need:
And the entry for Copy config File is difficult in Visual Studio. I prefer to right click on the log4net.config file and set properties to "Copy to Output Directory" = "Copy if newer"