SQLCipher 数据库的 NLog 配置

NLog Configuration for SQLCipher Database

我们正在尝试使用 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) 数据库。我们收到了几个不同的错误,但当前相关的错误显示在下面的 NLog 内部日志文件中

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)

current NLog配置文件如下(密码和connectionString其实是正确的,我只是在这个例子中重命名了它们)

<?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>

我认为问题可能出在 dbProvider 属性,但我不确定需要什么才能使 NLog 与 SQLCipher(SQLite3 的加密版本)一起工作

提前致谢!

编辑(为 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)

你试过这样做吗(在 commandText 中插入 PRAGMA 键):

    <!-- 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>

另请参阅:https://www.zetetic.net/sqlcipher/sqlcipher-for-dotnet/