Walaitki
Walaitki

Reputation: 165

Logging in dotnet-core console-apps and class libraries?

log4net:

Microsoft.Extensions.Logging

Any other candidates?

Upvotes: 4

Views: 4566

Answers (1)

Peter Bons
Peter Bons

Reputation: 29870

First of all, the introduction of the Microsoft.Extensions.Logging needs some clarification, I recommend reading this site https://msdn.microsoft.com/en-us/magazine/mt694089.aspx which states:

Logging? Why on earth do we need a new logging framework? We already have NLog, Log4Net, Loggr, Serilog and the built-in Microsoft.Diagnostics.Trace/Debug/TraceSource, just to name a few.

...

Therefore, you’re probably tempted to write your own logging API wrapper that invokes whichever particular logging framework you or your company chooses this week.

...

What Microsoft is providing with Microsoft.Extensions.Logging is that wrapper so everyone doesn’t have to write their own.

Now, there are some out-of-the box sinks that Microsoft shipped, they are listened here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging#built-in-logging-providers

As you can see a lot of destinations are not there, like logging to a file.

Luckily for you and me there are already some .Net logging implementations that adopt the Microsoft.Extensions.Logging interfaces like ILogger. Some are listened here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging#third-party-logging-providers

If you are willing to bet on another logging framework I personally would recommend a logging framework that provides structured logging like Serilog (https://serilog.net/), which has out-of-the-box support for the new .Net Core logging interfaces.

Also, read this for the advantages of structured logging: https://softwareengineering.stackexchange.com/questions/312197/benefits-of-structured-logging-vs-basic-logging

EDIT: There is native support for .Net Core for log4net using this NuGet package: https://www.nuget.org/packages/RobertHargreaves.log4net.Trunk/ To configure it using the config file see this blogpost: https://stackify.com/making-log4net-net-core-work/

It does not seems to have native support for the Ilogger and ILoggerFactory interfaces of the Microsoft.Extensions.Logging system.

Upvotes: 7

Related Questions