The purpose of this repository is to provide the simplest example possible of a working configuration of NLog in a .NET 5.0 webapi project.
I wanted to use only the appsettings.json configuration file (as should be with .NET core onwards) to store the NLog configuration, for several reasons (single configuration file, JSON, etc.) and I did not want to duplicate the connection string to the database in the NLog database target, as it was already specified in an earlier section of the appsettings.json file.
Trying to achieve that, I came accross only partial documentation and examples... while the result at the end was actually fairly simple because it is already fully supported by NLog. Therefore, I decided to publish this example scenario, gathering all the information I found, as it might be useful to someone hopefully.
I created a new .NET 5.0 web API project using the standard template wizard, out-of-the-box.
Create new project wizard step:
Configure new project wizard step:
Additional project information wizard step:
The following NuGet packages are required and must be added to the project:
- NLog: core NLog
- NLog.Extensions.Logging: NLog configuration support
- NLog.Web.AspNetCore: NLog support for .NET core
Startup.cs must be slightly adapted to tell NLog to initialize the configuration from appsettings.json instead of default NLog.config:
...
using NLog.Web;
...
namespace Net5.API.Sample
{
public class Startup
{
...
public void ConfigureServices(IServiceCollection services)
{
NLog.LogManager.Setup().LoadConfigurationFromAppSettings();
...
}
...
}
}
The example presented below shows a database and a SEQ target, with the connection string in the database target reusing the main connection string defined earlier on, in the standard ConnectionStrings section.
{
...
"ConnectionStrings": {
"Sample.NLog": "Server=myHost;Initial Catalog=myDb; Trusted_Connection=True;"
},
...
"NLog": {
"autoReload": true,
"throwConfigExceptions": true,
"extensions": [],
"targets": {
"async": true,
"database": {
"type": "Database",
"dbProvider": "Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient",
"connectionString": "${configsetting:item=ConnectionStrings.Sample.NLog}",
"commandText": "INSERT INTO [LogEntries] ([Message], [Level], [Date]) VALUES (@Message, @Level, @Date)",
"parameters": [
{
"name": "@Message",
"layout": "${message:truncate=1000}"
},
{
"name": "@Level",
"layout": "${event-properties:levelId}"
},
{
"name": "@Date",
"layout": "${event-properties:date}",
"dbType": "DbType.DateTime"
}
]
},
"seq": {
"type": "Seq",
"serverUrl": "",
"apiKey": "",
"properties": [
{
"name": "threadId",
"value": "${threadid}",
"as": "id"
},
{
"name": "name",
"value": "${machinename}"
}
]
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "database"
},
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "seq"
}
]
},
...
}
- NLog configuration
- NLog configuration file
- NLog database target
- NLog logging troubleshooting
- NLog new exception handling rules
- NLog configuration with appsettings.json
- NLog ConfigSetting layout renderer
- StackOverflow - NLog with .NET core and appsettings.json
- StakOverflow - NLog use connection string name in appsettings.json