DotNet.NLog.NetCore 9.0.0

dotnet add package DotNet.NLog.NetCore --version 9.0.0                
NuGet\Install-Package DotNet.NLog.NetCore -Version 9.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="DotNet.NLog.NetCore" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNet.NLog.NetCore --version 9.0.0                
#r "nuget: DotNet.NLog.NetCore, 9.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 DotNet.NLog.NetCore as a Cake Addin
#addin nuget:?package=DotNet.NLog.NetCore&version=9.0.0

// Install DotNet.NLog.NetCore as a Cake Tool
#tool nuget:?package=DotNet.NLog.NetCore&version=9.0.0                

DotNet.NLogger.NetCore

Version 9.0.0

Supports:

  • .Net 9.x,
  • netstandard 2.0; Net 4.8 with ref to Microsoft.Extensions.xxxx 9.0.0

DotNet.NLogger.NetCore 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.

Nuget.org

Adding DotNet.NLogger.NetCore

You have to define two configurations:

Create the NLog configuration xml

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     autoReload="true"
     internalLogLevel="Warn"
     internalLogFile="c:\temp\internal-nlog.txt">

 
 <targets>
   
   
   
   <target xsi:type="Null" name="NullLog" />
   <target xsi:type="Null" name="SystemLog" />
   
   
   <target xsi:type="Console" name="ConsoleInfoLog" />
   <target xsi:type="Console" name="ConsoleErrorLog" error="true" />


   

   <target xsi:type="File" name="CommonInfoLogFile"
           fileName="\Logs\RestWebApplication\Info\RestWebApp_CommonInfo-P_${processid}-${shortdate:universalTime=true}.log"
           layout="${longdate:universalTime=true}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message}| ${exception}" />
   
   <target xsi:type="File" name="BusinessErrorLogFile"
           fileName="\Logs\RestWebApplication\BusinessError\RestWebApp_BusinessError-P_${processid}-${shortdate:universalTime=true}.log"
           layout="${longdate:universalTime=true}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message}| ${exception}" />

   <target xsi:type="File" name="FatalErrorLogFile"
           fileName="\Logs\RestWebApplication\Error\RestWebApp_FatalError-P_${processid}-${shortdate:universalTime=true}.log"
           layout="${longdate:universalTime=true}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message}| ${exception:innerFormat=Message,Method,StackTrace:maxInnerExceptionLevel=1:format=Message,Method,StackTrace}" />
 
 </targets>

 <rules>
   

   <logger name="FatalError" writeTo="FatalErrorLogFile,CommonInfoLogFile" minlevel="Error"  final="true" enabled="true" />
   <logger name="BusinessError" writeTo="BusinessErrorLogFile,CommonInfoLogFile" minlevel="Error"  final="true" enabled="true" />

   <logger name="CommonInfo" writeTo="CommonInfoLogFile"               minlevel="Info"   final="true" enabled="true" />
   
   <logger name="ConsoleError" writeTo="ConsoleErrorLog,FatalErrorLogFile,CommonInfoLogFile" minlevel="Error"  final="true" enabled="true" />
   <logger name="ConsoleInfo" writeTo="ConsoleInfoLog,CommonInfoLogFile"  minlevel="Info"   final="true" enabled="true" />

   
   <logger name="Microsoft*"      minlevel="Trace" writeTo="SystemLog"      final="true" enabled="false" />
   
   <logger name="*" minlevel="Trace" writeTo="NullLog" />
 </rules>

</nlog>

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 DotNet.NLogger.NetCore

        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 DotNet.NLogger.NetCore by adding configuration:

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

or it will be added by default in .Net Core > 2.0+ see WebHost.cs)

So, It is recommend to read "how to configure the Logging'" section from ASP.NET CORE web guide Log filtering.

DotNet.Memory.Logger

.NET Memory Logger is a simple extension to log into memory by using ConcurrentQueue<T> collections

Version 7.0.0

Supports:

  • .Net 7.x,
  • netstandard 2.0, netstandard 2.1, Net 4.8 with ref to Microsoft.Extensions.xxxx 7.0.0
Version 6.1.0

Supports:

  • .Net 6.x,
  • netstandard 2.0, netstandard 2.1, Net 4.8 with ref to Microsoft.Extensions.xxxx 6.0.0
Version 6.0.0
  • Add support of .Net 6.x and netstandard 2.1
Version 5.0.0
  • Add support of .Net 5.x and netstandard 2.1
Version 3.1.0
  • Support SDK v.3.1.101-*
Version 2.1.2
  • Add support the feature turn on/off scope logging
  • Support SDK v.2.1.1

Adding DotNet.Memory.Logger

Add in the Web Host Builder configuring of Microsoft.Extensions.Logging.LoggerFactory the following initialization code:


     .ConfigureLogging((hostingContext, logging) =>
       {
           // ** Add DotNet.Memory.Logger
          logJsonCgf = hostingContext.Configuration.GetSection(nameof(MemoryLoggerSettings));
    
          if (!logJsonCgf.Exists())
          {
              throw new MissingMemberException($"Missing configuration section '{nameof(MemoryLoggerSettings)}'");
          }
    
          logging.AddMemoryLogger(logJsonCgf);
      }

Create NLogLoggerSettings configuration section in "appsettings.json".

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

{
"MemoryLoggerSettings": {

    "IncludeScopes": true,

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

    "MemoryCacheSize":4096


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

Example project

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

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.  net9.0 is compatible. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
9.0.0 105 12/8/2024
8.0.0 797 6/18/2024
7.0.0 5,199 11/10/2022
6.1.0 416 11/10/2022
6.0.0 583 8/3/2022
5.0.0 1,317 2/7/2021
3.1.0 2,969 2/15/2020
2.2.0 30,104 12/13/2018
2.1.2 1,949 9/4/2018
2.1.1 1,012 6/11/2018
2.1.0 945 6/4/2018
2.0.0 949 6/4/2018

net8.0 ; net48; netstandard2.0  MicroSoft.Extentions > 8.0.x