Log4net 不记录到数据库

Log4net doesn't log to database

要开始登录 .NET 项目,我添加了 log4net。 尝试记录数据时,日志会写入本地文件,但不会写入数据库。

很可能错误在 log4net.config 中,因为我没有看到任何错误。我找不到好像找不到什么错误...

Global.asax.cs

[assembly: log4net.Config.XmlConfigurator(Watch = true)]    

创建日志的控制器

private static readonly log4net.ILog log = log4net.LogManager.GetLogger(nameof(SettingController));  

log.InfoFormat($"Some Info");

log4net.config

<configuration>
  <log4net>
    <root>
      <level value="ALL" />
      <appender-ref ref="RollingFileAppender" />
      <appender-ref ref="ADONetAppender" />
    </root>
    <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="Logfile.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="5MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %leveADONetAppenderl %logger - %message%newline" />
      </layout>
    </appender>

    <appender name="ADONetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />

      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
      <connectionString value="server=myServerName; Integrated Security=True; database=myDatabaseName" />

      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[User],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @user, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%thread" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%level" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%logger" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@user" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%user" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%message" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>
  </log4net>
</configuration>   

首先尝试登录到一个appender。在这种情况下,那个不工作。并启用内部调试:

<configuration>
...
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>

...

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add 
                    name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
</configuration>

Log4net FAQ

您可能会发现ADONetAppender 中的参数映射等问题。从那里您可以解决问题,然后再次启用第二个 appender。

我接受了@Peter 的回答,因为它为更广泛的“受众”提供了更有用的信息。


我犯的错误实际上是,我在连接字符串中设置了PublicKeyToken=null

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />

PublicKeyToken 保持 相同 (PublicKeyToken=b77a5c561934e089) 对于 all log4net 连接。

<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />