Wallsmedia.Microsoft.Extensions.Logging.NLog 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Wallsmedia.Microsoft.Extensions.Logging.NLog --version 3.0.0                
NuGet\Install-Package Wallsmedia.Microsoft.Extensions.Logging.NLog -Version 3.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Wallsmedia.Microsoft.Extensions.Logging.NLog" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Wallsmedia.Microsoft.Extensions.Logging.NLog --version 3.0.0                
#r "nuget: Wallsmedia.Microsoft.Extensions.Logging.NLog, 3.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Wallsmedia.Microsoft.Extensions.Logging.NLog as a Cake Addin
#addin nuget:?package=Wallsmedia.Microsoft.Extensions.Logging.NLog&version=3.0.0

// Install Wallsmedia.Microsoft.Extensions.Logging.NLog as a Cake Tool
#tool nuget:?package=Wallsmedia.Microsoft.Extensions.Logging.NLog&version=3.0.0                

Microsoft.Extensions.Logging.NLog is an adapter between NLog and Microsoft.Extensions.Logging.

It allows to simplify using NLog by utilizing ILoggerFactory and ILogger interfaces in an application.

NLog is a flexible and free logging platform for various .NET platforms, including .NET standard. NLog makes it easy to write to several targets. (database, file, console) and change the logging configuration on-the-fly.

Adding Microsoft.Extensions.Logging.NLog

You have to define two configurations:

Create the NLog configuration xml

Create NLogLoggerSettings configuration section in "appsettings.json".

The NLogLoggerSettings section defines the Category Name "filter" and Category Name "mapper".

{
"NLogLoggerSettings": {

    "IncludeScopes": true,

    "AcceptedCategoryNames": [ /* Filter of category name */
      "ConsoleInfo",   /* The category name is accepted as a "NLog logger name" */
      "CommonInfo",    /* The category name is accepted as a "NLog logger name" */
      "ConsoleError",  /* The category name is accepted as a "NLog logger name" */
      "FatalError",    /* The category name is accepted as a "NLog logger name" */
      "BusinessError", /* The category name is accepted as a "NLog logger name" */
      "*Error*",       /* The category name that contains "Error" is accepted as a "NLog logger name" */
      "*Info",         /* The category name that ends with "Info" is accepted as a "NLog logger name" */
      "Com*",          /* The category name that starts with "Com" is accepted as a "NLog logger name" */
      "*"              /* Any category name will be accepted  as a "NLog logger name" */
    ],

    /* Map category name "ABC" to "NLog logger name" = "ConsoleError" */
    "AcceptedAliasesCategoryNames:ABD": "ConsoleError"  
    
    /* Map category name that ends with "*Hosted" to "NLog logger name" = "ConsoleError" */
    "AcceptedAliasesCategoryNames:*Hosted": "ConsoleError"  

    /* Map category name that starts with "Microsoft.AspNetCore*" to "NLog logger name" = "ConsoleError" */
    "AcceptedAliasesCategoryNames:Microsoft.AspNetCore*": "ConsoleError" 

    /* Map category name that contains "*AspNetCore*" to "NLog logger name" = "ConsoleError"*/
    "AcceptedAliasesCategoryNames:*AspNetCore*": "ConsoleError"

    /* Map any category  to "NLog logger name" = "ConsoleError" */
    "AcceptedAliasesCategoryNames:*": "ConsoleError"

  }
}
  • The AcceptedCategoryNames - "category name filter" is used to filter-in category names. It is expected that the category name is exact match to <logger name="...." in the NLog xml configuration.

  • The AcceptedAliasesCategoryNames - "category name mapper" is used to filter-in category names and map them onto new name that expected to be match to <logger name="..." in the NLog xml configuration.

Web Host Builder configuration

After defining the configurations, add in the Web Host Builder configuring of Microsoft.Extensions.Logging.LoggerFactory the following initialization code:


     .ConfigureLogging((hostingContext, logging) =>
       {

        // ** Add Microsoft.Extensions.Logging.NLog

        string logPath = Path.Combine(hostingContext.HostingEnvironment.ContentRootPath, $"nlog.{hostingContext.HostingEnvironment.EnvironmentName}.config");
        if (!File.Exists(logPath))
        {
            throw new MissingMemberException($"Missing NLog configuration file '{logPath}'");
        }
        var nLoggingConfiguration = new XmlLoggingConfiguration(logPath);

        var logJsonCgf = hostingContext.Configuration.GetSection(nameof(NLogLoggerSettings));
        if (!logJsonCgf.Exists())
        {
            throw new MissingMemberException($"Missing configuration section '{nameof(NLogLoggerSettings)}'");
        }

        logging.AddNLogLogger(logJsonCgf, nLoggingConfiguration);
      }

Example projects

See sample of pure NLog style project Using Adaptation NLog in .Net Core Rest Web Application

See sample of pure .Net Core Logger ⇒ NLog style project Using Logger + NLog in .Net Core Rest Web Application

Microsoft.Extensions.Logging - Configuration

If you decided to use additional filtering from Microsoft.Extensions.Logging over the filtering that provided with Microsoft.Extensions.Logging.NLog by adding configuration:

 logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));

So, It is recommend to read "how to configure the Logging'" section from ASP.NET CORE web guide https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.0#configuration.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 3.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 1,184 2/15/2020
3.0.0 801 9/30/2019
2.2.0 5,275 12/13/2018
2.1.0 1,385 10/21/2018

Released for  Microsoft.AspNetCore.App 3.0.0