


This article explains how to achieve a portable and elegant logging solution with NLog and SQLite on the .NET platform with a few configurations.
NLog
NLog is a free, lightweight logging library for .NET. It supports many targets (files, databases, console, network, email, and more). nlog-project.org
Install (NuGet Package Manager Console)
PM> Install-Package NLog
SQLite
SQLite is a simple embedded database engine. No separate server daemon is required; the database is a single file (e.g., .db3).
Install (NuGet Package Manager Console)
PM> Install-Package System.Data.SQLite
NLog configuration file
Keeping NLog configuration in a separate file is recommended so a misconfiguration affects only logging and not the whole app.
Install config scaffold
PM> Install-Package NLog.Config
Get them to work together
- Create the SQLite database (e.g., with DB Browser for SQLite) and the 
Logtable: 
CREATE TABLE Log (
  Timestamp TEXT,
  Loglevel  TEXT,
  Logger    TEXT,
  Callsite  TEXT,
  Message   TEXT
);
- Point NLog to SQLite by adding a database target in 
NLog.config: 
<?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"
      throwExceptions="false">
  <targets>
    <target name="db" xsi:type="Database"
            dbProvider="System.Data.SQLite"
            keepConnection="false"
            connectionString="Data Source=${basedir}\Log.db3;Version=3;"
            commandText="INSERT INTO Log(Timestamp, Loglevel, Logger, Callsite, Message)
                          VALUES(@Timestamp, @Loglevel, @Logger, @Callsite, @Message)" />
  </targets>
  <rules>
    <logger name="*" minlevel="Warn" writeTo="db" />
  </rules>
</nlog>
If you’re using newer package versions or providers, check the official docs for any changes to
dbProvideror connection string formats.
- Start logging
 
using NLog;
class Program
{
    private static readonly Logger log = LogManager.GetCurrentClassLogger();
    static void Main(string[] args)
    {
        log.Info("Logging like a boss");
    }
}
Source code
Sample project: github.com/aazzola/nlog-sqlite