NLog is a highly efficient logging framework which helps us enabling logging to almost all logging source format including Console, File, Database logging, etc.

High-end logging requirements like Database or File or Rolling File logging provider are still not available through the .NET Core framework.

Currently, the available ASP.NET Core version logging framework is already very rich and gives us a lot of flexibility of logging to different logging providers like Console, Event, EventSource, etc.

We shall be targeting SQL Databaseas logging source.

NLog supports below database provider as well,

  • System.Data.OracleClient
  • Oracle.DataAccess.Client
  • System.Data.SQLite
  • Npgsql
  • MySql.Data.MySqlClient

Getting started

Create ASP.NET Core API

NLog is available as NuGet package. Please use the latest available version available for the ASP.NET Core version.

Additionally please install below NuGet package for SQL database provider support


You can also also install the packages from Nuget Package Manager,

NLog Database Configuration

NLog support multiple ways of managing the configuration like you can use code or combination of code and config file etc.

DDL – Create SQL Table schema

I have used below script to create the NlogDBLog table.

Sample DDL file can be found on GitHub,

Please update the above script for any customization if needed.

After running the above script our database schema looks as below,

Create NLog.Config

Below is sample config file,

In the above Configuration we have defined below,

  • name – Our custom target name for the given provider identification. It should be the same as writeTo field in Rules tag.
  • type – Target type Database or File
  • connectionString -Define connection string
  • commandText -Please mention the DML command here targetting NlogDBLog created using DDL commands

Loading Configuration

Please update the Main method for adding the Database logging as shown in below-highlighted code in Program.cs

Below is the complete code base,

Please update HostBuilder as below using UseNLog,

Let’s do the logging using ILogger instance in any part of the code,

Below is how the logs will captured in the NLog SQL table,

Adding Database provider

If not using SQL provider then please use fully qualified name of the provider connection type.

MySQL Client

SQL Client

SQLite client


NLog simplifies and helps us enabling logging in a few simple steps and address the Database logging requirements easily. Currently, high-end logging requirements like Database or Files are not yet available through the .NET Core framework and certainly, we need to depend on custom or existing external solutions and NLog suffices that requirement easily.

Leave a comment

Your email address will not be published.