DotNet.NLog.NetCore 3.1.0

Dot Net NLog  logger wrapper for Microsoft.Extensions.Logging.

Install-Package DotNet.NLog.NetCore -Version 3.1.0
dotnet add package DotNet.NLog.NetCore --version 3.1.0
<PackageReference Include="DotNet.NLog.NetCore" Version="3.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DotNet.NLog.NetCore --version 3.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

DotNet.NLogger.NetCore

Version 3.1.0
  • Support SDK v.3.1.101-*
Version 2.2.0
  • Support SDK v.2.2.0
Version 2.1.2
  • Add support the feature turn on/off scope logging
  • Support SDK v.2.1.3

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">

 <!-- https://github.com/NLog/NLog/wiki/Configuration-file  -->
 <targets>
   
   <!-- Null loggers -->
   
   <target xsi:type="Null" name="NullLog" />
   <target xsi:type="Null" name="SystemLog" />
   
   <!-- Console loggers -->
   <target xsi:type="Console" name="ConsoleInfoLog" />
   <target xsi:type="Console" name="ConsoleErrorLog" error="true" />


   <!-- File loggers -->

   <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>
   <!-- Loggers application section -->

   <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" />

   <!-- Other log info -->
   <logger name="Microsoft*"      minlevel="Trace" writeTo="SystemLog"      final="true" enabled="false" />
   <!-- discard all not consumed -->
   <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.

DotNet.NLogger.NetCore

Version 3.1.0
  • Support SDK v.3.1.101-*
Version 2.2.0
  • Support SDK v.2.2.0
Version 2.1.2
  • Add support the feature turn on/off scope logging
  • Support SDK v.2.1.3

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">

 <!-- https://github.com/NLog/NLog/wiki/Configuration-file  -->
 <targets>
   
   <!-- Null loggers -->
   
   <target xsi:type="Null" name="NullLog" />
   <target xsi:type="Null" name="SystemLog" />
   
   <!-- Console loggers -->
   <target xsi:type="Console" name="ConsoleInfoLog" />
   <target xsi:type="Console" name="ConsoleErrorLog" error="true" />


   <!-- File loggers -->

   <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>
   <!-- Loggers application section -->

   <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" />

   <!-- Other log info -->
   <logger name="Microsoft*"      minlevel="Trace" writeTo="SystemLog"      final="true" enabled="false" />
   <!-- discard all not consumed -->
   <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.

Release Notes

Singed assembly; version for netstandard2.0

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.1.0 177 2/15/2020
2.2.0 2,520 12/13/2018
2.1.2 492 9/4/2018
2.1.1 308 6/11/2018
2.1.0 255 6/4/2018
2.0.0 243 6/4/2018