AdoNetAppender Log4Net无法使用System.Data.SQLite工作

时间:2017-01-17 10:30:17

标签: log4net

我正在尝试使用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");
        }
    }
}

运行代码后,我使用&#34; SQLiteStudio&#34;从Log4net.db中选择数据,但我什么都没看到。我的代码会发生什么。

1 个答案:

答案 0 :(得分:0)

您尝试使用以下连接字符串建立与数据库的连接:

element

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