Skip to content

andrejlapajne/Net5.API.Sample.NLog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NLog configuration within appsettings.json in .NET 5

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.

Sample project setup

I created a new .NET 5.0 web API project using the standard template wizard, out-of-the-box.

Create new project wizard step:

Create a new project

Configure new project wizard step:

Configure the new project

Additional project information wizard step:

Additional project information

Required configuration to setup NLog

Add the required NLog packages

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

NuGet packages

Update Startup.cs

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();

            ...
        }
        ...        
    }
}

Add NLog section to appsettings.json

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"
            }
        ]
    },
    ...
}

References

About

NLog configuration sample with .NET 5.0 API project

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages