Skip to content

Instantly share code, notes, and snippets.

@HarryAmmon
Last active October 18, 2023 15:21
Show Gist options
  • Save HarryAmmon/55ab3f73a069ad68d86f6ba203061000 to your computer and use it in GitHub Desktop.
Save HarryAmmon/55ab3f73a069ad68d86f6ba203061000 to your computer and use it in GitHub Desktop.
Log4Net .NET Core Console App Quickstart

Log4Net .NET Core Console App Quickstart

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.

Installing Log4Net

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.

Package Manger Console

Open the Package Manager Console and run the following command.

Install-Package log4net -Version 2.0.8

NuGet Packages GUI

In solution explorer, right click on your project and select 'Manage NuGet Packages'. Shows pop-up menu when project is right clicked.

In the Browse section, search for 'Log4Net', select the latest version and press install. Shows NuGet Browser with Log4Net selected

Creating Config File

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>.

<root>

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.

<appender>

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.

Calling configuration file in code.

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"));

Copy config File

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>

Start Logging

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!

Next Steps

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!

@rumplin
Copy link

rumplin commented Dec 18, 2020

Great write up, but please distinguish between what code goes into the Program class and what into the Main method in the Program class. This part might be confusing for new users. I would suggest to provide the whole code of the example.

@eduardoalba
Copy link

Good job! :)

@csharpforevermore
Copy link

csharpforevermore commented Mar 13, 2022

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:

using log4net;
using log4net.config;

but should be

using log4net;
using log4net.Config;

We also need:

using System.IO;
using System.Reflection;

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"

@PrashantKate007
Copy link

this is not woking in .net core 5 console appplication,
please go throng below link it's working in .Net Core 5 console app,

https://jakubwajs.wordpress.com/2019/11/28/logging-with-log4net-in-net-core-3-0-console-app/comment-page-1/?unapproved=3749&moderation-hash=de63c05c7449972c2afd0e03a2bea206#comment-3749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment