Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft.Extensions.Logging Vs. NLog

Tags:

I see a lot of posts mentioning usage of Microsoft.Extensions.Logging together with NLog.

I'd like to better understand what Microsoft.Extensions.Logging is used for?

And specifically why is it needed or what's the benefit of using it together with NLog?

like image 986
AlexVPerl Avatar asked Oct 02 '19 20:10

AlexVPerl


People also ask

What is the use of Microsoft extensions logging?

Extensions. Logging provided by ASP.NET Core. This provides an ILogger interface that allows the provider of your choice to be used while minimizing the impact to existing code.

Which logging framework is best for .NET Core?

NLog is one of the most popular, and one of the best-performing logging frameworks for . NET. Setting up NLog is fairly simple. Developers can use Nuget to download the dependency, then edit the NLog.

How do you log in with NLog extensions?

Logging makes it possible to use NLog together with Microsoft ILogger-abstraction and Dependency Injection. It provides extension methods to register NLog as LoggingProvider for Microsoft Extension Logging using AddNLog() or UseNLog() . Note if using ASP.NET Core then instead install NLog.

What types of logs are supported in the NLog library?

NLog supports semantic/structured logging known from similar frameworks like Serilog and Microsoft. Extensions. Logging. With structured logging, you no longer log just simple text messages.


Video Answer


1 Answers

With NLog you could do:

var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Hello {Name}", "Earth"); 

That works for all platforms and all frameworks.

Microsoft.Extensions.Logging

With .NET Core, Microsoft introduced the ILogger abstraction from Microsoft.Extensions.Logging. You could use that logging abstraction in your project and integrate it with NLog.

For example in ASP.NET Core you could inject Microsoft.Extensions.Logging.ILogger<HomeController> and that could send the logs to NLog. (see Getting started with ASP.NET Core 2 · NLog/NLog Wiki)

using Microsoft.Extensions.Logging;  public class HomeController : Controller {     private readonly ILogger<HomeController> _logger;      public HomeController(ILogger<HomeController> logger)     {         _logger = logger;     }      public IActionResult Index()     {         _logger.LogInformation("Index page says hello {Name}", "Universe");         return View();     } 

Packages

For NLog and Microsoft.Extensions.Logging there are the following packages:

  • The .NET core logging abstractions are in the package Microsoft.Extensions.Logging.Abstractions
  • The NLog integration for those abstractions is in package NLog.Extensions.Logging. You could use this package for .NET Core console applications.
  • For ASP.NET Core, there is an optimized package NLog.Web.AspNetCore which uses NLog.Extensions.Logging

Comparison

NLog Pros

Pros of using NLog directly

  • Best performance
  • More options with the logger API, e.g. Logger.WithProperty(..)
  • Works in all platforms
  • No Dependency Injection needed which saves complexity.

Pros Microsoft.Extensions.Logging

Pros of using NLog via Microsoft.Extensions.Logging:

  • Fully integrated with ASP.NET Core, e.g. Microsoft also writes to the logger API and that will be also captured (and possible filtered) by NLog
  • Writing to the logging abstraction will make your code log-library independent.
  • Works well with .NET Core Dependency injection
  • new: You could configure NLog with appsettings.json - so with JSON instead of XML

Update: added - Using NLog with appsettings.json

like image 96
Julian Avatar answered Sep 17 '22 15:09

Julian