Retrace will reach End of Life on March 31, 2027. Click here to learn more.

How to Configure log4net for .NET Core

By: mwatson
  |  February 13, 2017
How to Configure log4net for .NET Core

Recently the log4net team published new NuGet packages that support .NETStandard 1.3, which means it should work with .NET Core. I decided to spend some time today to make sure our Stackify logging libraries work with log4net and .NET Core. We already published updates for NLog and Serilog. It was about time we had log4net support out there as well.

Getting Started: Configure() log4net

Adding a reference to the log4net NuGet package was no problem. If you have worked with log4net for any amount of time, you know you have to add the magical line of code for configuring it. Although, you can see from my screenshot below that it doesn’t work like expected.

image Configure() log4net

XmlConfigurator.Configure() requires some parameters that we didn’t usually provide in the past.

image XmlConfigurator.Configure

Configuration Workaround: Loading It Manually

Normally log4net is smart enough to load its configuration from a web.config or log4net.config file. In .NET Core, it doesn’t seem to know how. I searched all over the internet, looked through the log4net repo, downloaded the source, and beat my head on the wall to no avail.

It makes sense that it can’t read from a web.config/app.config because they don’t even exist in .NET Core.

But, it also doesn’t automatically try to read a log4net.config file. As a side note, NLog does the same thing. You have to manually configure it now in .NET Core.

After a little reverse engineering, I figured out how to manually load my log4net.config file and configure log4net.

var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());
XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));

I was able to get this to work with a .NET Core app targeting both netcoreapp1.0 and net452. Add that to your Program.cs as the first thing your code does.

Also Read-https://stackify.com/hiring-a-devops-engineer-useful-tips-and-best-practices/

log4net with .NET Core Is ALLIIVVEEE!

So after a little trial and error, I was finally able to figure it out. I hope this can be helpful to others who are trying to figure this out. They do list some limitations with the current 2.0.7 version of log4net. Hopefully, we see further improvements to all of this.

Also Read-https://stackify.com/pair-programming-styles/

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More
indobet toto slot toto toto bobatoto daftar logototo patentoto toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k https://nolimithoki.dev/ dinasti33 slot slot jepang toto toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor situs gacor slot gacor slot gacor slot gacor slot gacor Slot88 situs slot slot gacor Slot88 slot gacor slot terbaru slot gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast slot resmi slot slot resmi slot bayitoto link gacor slot gacor kari4d slot gacor slot gacor slot gacor pascol4d kari4d AMANAHTOTO kari4d bragatoto slot gacor slot game pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d roma77 judi bola slot depo 10k mpo slot Girang4d KARI4D StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k hoki99 pascol4d pkv games roma77 Slot poker slot situs slot PREMANTOTO AMANAHTOTO AMANAHTOTO AMANAHTOTO dominoqq vespa36 kari4d vespa36 leon188 leon188 vespa36 gacor mpo login TOTO SLOT hoki99 KENANGAN889 KENANGAN889 KENANGAN889 KARI4D parlay parlay parlay KENANGAN889
indobet toto slot toto toto bobatoto daftar logototo patentoto toto bayitoto popotogel prize hongkong malam ini joker11 lingkartoto depo 5k https://nolimithoki.dev/ dinasti33 slot slot jepang toto toto flokitoto slot gacor logototo slot depo 5k bobatoto bobatoto bobatoto sontogel mpo slot situs toto slot situs slot bobatoto kari4d kari4d kari4d kari4d pascol4d pascol4d pascol4d bento11 link slot gacor toto toto slot toto situs slot situs slot situs slot https://www.asupan188.dev/ slot gacor gampang menang toto slot gacor bwo303 situs toto toto slot slot gacor slot88 slot gacor situs slot link slot slot gacor pucuk4d PASCOL4D situs slot link slot slotvip situs toto toto slot situs slot situs slot situs slot toto slot gacor slot777 situs judi bola POPOTOGEL : Situs Resmi Bandar Toto Togel Macau dengan Prediksi Gokil Pasti Akurat link slot slot gacor situs slot slot gacor slot gacor situs slot link slot gacor Slot88 Slot88 slot gacor situs toto toto slot pulsa slot777 link slot gacor situs gacor slot gacor slot gacor slot gacor slot gacor Slot88 situs slot slot gacor Slot88 slot gacor slot terbaru slot gacor situs slot slot gacor situs slot Situs Slot POPOTOGEL: Regulasi Bandar Situs Togel Resmi & Bola Mix Parlay Asia Terpercaya StreamEast slot resmi slot slot resmi slot bayitoto link gacor slot gacor kari4d slot gacor slot gacor slot gacor pascol4d kari4d AMANAHTOTO kari4d bragatoto slot gacor slot game pucuk4d rokokbet rokokbet kari4d pascol4d LOGIN PASCOL4D pucuk4d roma77 judi bola slot depo 10k mpo slot Girang4d KARI4D StreamEast AMANAHTOTO jutatoto PARLAY slot88 slot depo 5k hoki99 pascol4d pkv games roma77 Slot poker slot situs slot PREMANTOTO AMANAHTOTO AMANAHTOTO AMANAHTOTO dominoqq vespa36 kari4d vespa36 leon188 leon188 vespa36 gacor mpo login TOTO SLOT hoki99 KENANGAN889 KENANGAN889 KENANGAN889 KARI4D parlay parlay parlay KENANGAN889