'How can I configure NLog in .NET Core with appsettings.json instead of an nlog.config file?
The NLog documentation explains how to configure NLog for .NET Core applications by using an nlog.config XML file. However, I'd prefer to have just one configuration file for my application - appsettings.json. For .NET Framework apps, it's possible to put the NLog configuration in app.config or web.config. Is it possible to put the NLog config in appsettings.json in the same way?
For example, how could I put this configuration example from the NLog documentation for ASP.NET Core 2 into appsettings.json?
<?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="Info"
internalLogFile="c:\temp\internal-nlog.txt">
<!-- enable asp.net core layout renderers -->
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip non-critical Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" maxlevel="Info" final="true" /> <!-- BlackHole without writeTo -->
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Solution 1:[1]
Adding to Rolf Kristensen's response.
appsettings.json with database target.
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"internalLogLevel": "info",
"internalLogFile": "c:\\temp\\internal-nlog.txt",
"extensions": [
{ "assembly": "NLog.Extensions.Logging" }
],
"targets": {
"database": {
"type": "Database",
"commandText": "INSERT INTO dbo.log (MachineName,Logged,Level,Message,Logger,Callsite,Exception) values (@MachineName,@Logged,@Level,@Message,@Logger,@Callsite,@Exception)",
"parameters": [
{
"name": "@MachineName",
"layout": "${machinename}"
},
{
"name": "@Logged",
"layout": "${date}"
},
{
"name": "@Level",
"layout": "${level}"
},
{
"name": "@Message",
"layout": "${message}"
},
{
"name": "@Logger",
"layout": "${logger}"
},
{
"name": "@Callsite",
"layout": "${callsite}"
},
{
"name": "@Exception",
"layout": "${exception:tostring}"
}
],
"dbProvider": "System.Data.SqlClient",
"connectionString": "Data Source=database server;Initial Catalog=database ;Trusted_Connection=False; User Id=AppUser;Password=AppUserPassword;"
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "database"
}
]
}
Solution 2:[2]
You can do it like this when you have a console application (I'm using Net 5):
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
...
}).ConfigureLogging((hostContext, logBuilder) =>
{
logBuilder.AddNLog(new NLogLoggingConfiguration(hostContext.Configuration.GetSection("NLog"))).SetMinimumLevel(LogLevel.Trace);
})
.UseConsoleLifetime();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rolf Kristensen |
| Solution 2 | Patrick Koorevaar |
