title: How to use SQLite with NLog in .NET
body: <p>I am trying to use SQLite with NLog so that I can store logging to a SQLite database rather a flat file. I followed a good tutorial, but even then my code is not enabling is working as expected.</p>
  
  <p>My <code>App.config</code> file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;configuration&gt;
    &lt;connectionStrings&gt;
      &lt;add name="SQLite" connectionString="Data Source=Log.db3;Version=3;" providerName="System.Data.SQLite" /&gt;
    &lt;/connectionStrings&gt;
    &lt;startup&gt;
      &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /&gt;
    &lt;/startup&gt;
    &lt;system.data&gt;
      &lt;DbProviderFactories&gt;
        &lt;remove invariant="System.Data.SQLite" /&gt;
        &lt;add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" /&gt;
      &lt;/DbProviderFactories&gt;
    &lt;/system.data&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p><strong>And this is my NLog.config file</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;targets&gt;
      &lt;target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}"
            fileName="${basedir}/logs/${shortdate}/${level}.txt"
            keepFileOpen="false"/&gt;
      &lt;target name="db" xsi:type="Database" keepConnection="false"
              useTransactions="false"
              dbProvider="System.Data.SQLite"
              connectionString="Data Source=${basedir}\Log.db3;Version=3;"
              commandText="INSERT into Log(Timestamp, Loglevel, Callsite, Message) values(@Timestamp, @Loglevel, @Callsite, @Message)"&gt;
        &lt;parameter name="@Timestamp" layout="${longdate}"/&gt;
        &lt;parameter name="@Loglevel" layout="${level:uppercase=true}"/&gt;
        &lt;parameter name="@Callsite" layout="${callsite:filename=true}"/&gt;
        &lt;parameter name="@Message" layout="${message}"/&gt;
      &lt;/target&gt;
    &lt;/targets&gt;
    &lt;rules&gt;
      &lt;logger name="*" minlevel="Trace" writeTo="db" /&gt;
    &lt;/rules&gt;
  &lt;/nlog&gt;
  </code></pre>
  
  <p>And this is my code:</p>
  
  <pre><code> static class Program
      {
          static Logger log = LogManager.GetCurrentClassLogger();
          /// &lt;summary&gt;
          /// The main entry point for the application.
          /// &lt;/summary&gt;
          [STAThread]
          static void Main()
          {
              try
              {
                  Application.EnableVisualStyles();
                  Application.SetCompatibleTextRenderingDefault(false);
                  EnsureDb();
                  log.Info("Logging like a boss");
                  Application.Run(new Form1());
              }
              catch (Exception ex)
              {
                  throw;
              }
  
          }
  
          private static void EnsureDb()
          {
               if (File.Exists("Log.db3"))
                   return;
  
               using (SQLiteConnection connection = new SQLiteConnection(ConfigurationManager.ConnectionStrings["SQLite"].ToString()))
               using (SQLiteCommand command = new SQLiteCommand(
                   "CREATE TABLE Log (Timestamp TEXT, Loglevel TEXT, Callsite TEXT, Message TEXT)",
                   connection))
               {
                   connection.Open();
                   command.ExecuteNonQuery();
               }
          }
      }
  </code></pre>
  
  <p>I have copied this code from the Internet, but unfortunately it's not working. The log.db3 file always gets created, but no log entry reaches to the log table. How can I fix this?</p>