AdoNetAppender Log4Net 无法使用 System.Data.SQLite

AdoNetAppender Log4Net not working using System.Data.SQLite

我正在尝试使用 log4net 的 AdoNetAppender 将日志写入 Sqlite,但我发现它不起作用。

App.Config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <configSections>
      <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
   </configSections>
   <log4net>
      <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
         <bufferSize value="100" />
         <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.81.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
         <connectionString value="Data Source=|DataDirectory|\Log4net.db;Synchronous=Off;Pooling=true;FailIfMissing=false" />
         <commandText value="INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" />
         <CommandType value="Text" />
         <parameter>
            <parameterName value="@Date" />
            <dbType value="DateTime" />
            <layout type="log4net.Layout.RawTimeStampLayout" />
         </parameter>
         <parameter>
            <parameterName value="@Level" />
            <dbType value="String" />
            <layout type="log4net.Layout.PatternLayout">
               <conversionPattern value="%level" />
            </layout>
         </parameter>
         <parameter>
            <parameterName value="@Logger" />
            <dbType value="String" />
            <layout type="log4net.Layout.PatternLayout">
               <conversionPattern value="%logger" />
            </layout>
         </parameter>
         <parameter>
            <parameterName value="@Message" />
            <dbType value="String" />
            <layout type="log4net.Layout.PatternLayout">
               <conversionPattern value="%message" />
            </layout>
         </parameter>
      </appender>
      <root>
         <level value="DEBUG" />
         <appender-ref ref="AdoNetAppender" />
         <!--<appender-ref ref="DebugAppender" />-->
      </root>
   </log4net>
   <appSettings>
      <add key="log4net.Internal.Debug" value="true" />
   </appSettings>
   <system.diagnostics>
      <trace autoflush="true">
         <listeners>
            <add name="textWriterTraceListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Windows\Temp\log4net.txt" />
         </listeners>
      </trace>
   </system.diagnostics>
   <startup>
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
   </startup>
   <system.data>
      <DbProviderFactories>
         <remove invariant="System.Data.SQLite.EF6" />
         <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
      </DbProviderFactories>
   </system.data>
</configuration>

Log4net.db 存储在项目的 bin\Debug 中。

申请:

using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = Environment.CurrentDirectory;
            AppDomain.CurrentDomain.SetData("DataDirectory", path);
            log4net.Config.XmlConfigurator.Configure();
            ILog log = LogManager.GetLogger(typeof(Program).FullName);
            log.Error("Hello");
        }
    }
}

运行代码后,我使用 "SQLiteStudio" 到 select 来自 Log4net.db 的数据,但我什么也没看到。我的代码发生了什么。

您尝试使用以下连接字符串连接到数据库:

     <connectionType value="System.Data.SQLite.SQLiteConnection, System.Data.SQLite, Version=1.0.81.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />

SQLite 或其依赖项不在您的 bin 目录中。