NLog Configuration for SQLCipher Database

0

We are attempting to use NLog (https://github.com/NLog/NLog) to log certain things to a table named Logs that is contained within our SQLCipher (https://github.com/sqlcipher/sqlcipher) Database. We are receiving several different errors, but the one that is currently relevant is shown in the NLog internal logging file below

2019-08-15 11:46:15.4789 Info Message Template Auto Format enabled
2019-08-15 11:46:15.5587 Info Adding target DatabaseTarget(Name=DBLog)
2019-08-15 11:46:15.6265 Info Found 38 configuration items
2019-08-15 11:46:15.7622 Info Configuration initialized.

2019-08-15 11:46:15.7751 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.6.6.10303. Product version: 4.6.6.

2019-08-15 11:47:04.6315 Error DatabaseTarget(Name=DBLog): Error when writing to database. Exception: Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 26: '**file is not a database**'.
   at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
   at Microsoft.Data.Sqlite.SqliteCommand.<PrepareAndEnumerateStatements>d__62.MoveNext()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader()
   at Microsoft.Data.Sqlite.SqliteCommand.ExecuteNonQuery()
   at NLog.Targets.DatabaseTarget.WriteEventToDatabase(LogEventInfo logEvent, String connectionString)
   at NLog.Targets.DatabaseTarget.Write(LogEventInfo logEvent)

The current NLog config file is as follows (the password and connectionString are actually correct, I've just renamed them in this example)

<?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"
      internalLogLovel="Trace"
      internalLogFile="file.txt"
      internalLogToConsole="true"
      internalLogIncludeTimestamp="true">

  <targets>

    <!-- DATABASE LOGGER-->
    <target name="DBLog"
            xsi:type="Database"
            dbProvider="Microsoft.Data.SQLite.SQLiteConnection, Microsoft.Data.SQLite"
            keepConnection="false"
            connectionString="Data Source=C:\Folder\DatabaseName.db"
            dbPassword="password"
            commandText="INSERT INTO Logs (Message) VALUES ('Test');">
    </target>

  </targets>

  <rules>
    <logger name="*" minlevel="Debug" writeTo="DBLog" />
  </rules>

</nlog>

I think the problem may lie in the dbProvider property, but I am unsure what is needed in order to get NLog to work with SQLCipher (encrypted version of SQLite3)

Thanks in advance!

Edit (adding for JAZ)

2019-08-16 08:59:51.4363 Error Database Target[DBLog]: Error initializing target Exception: System.TypeLoadException: Could not load type 'System.Data.SQLite' from assembly 'NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c'.
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
   at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
   at System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark)
   at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
   at NLog.Targets.DatabaseTarget.SetConnectionType()
   at NLog.Targets.DatabaseTarget.InitializeTarget()
   at NLog.Targets.Target.Initialize(LoggingConfiguration configuration)
wpf
sqlite
nlog
sqlcipher
asked on Stack Overflow Aug 15, 2019 by beJeb • edited Aug 16, 2019 by beJeb

1 Answer

0

Have you tried just doing this (Injecting the PRAGMA key in the commandText):

    <!-- DATABASE LOGGER-->
    <target name="DBLog"
            xsi:type="Database"
            dbProvider="Microsoft.Data.SQLite.SQLiteConnection, Microsoft.Data.SQLite"
            keepConnection="false"
            connectionString="Data Source=redacted\redacted.db"
            commandText="PRAGMA key = 'redacted';INSERT INTO Logs (Message) VALUES ('Test');">
    </target>

See also: https://www.zetetic.net/sqlcipher/sqlcipher-for-dotnet/

answered on Stack Overflow Aug 19, 2019 by Rolf Kristensen

User contributions licensed under CC BY-SA 3.0