Log4net不会记录到数据库

时间:2017-10-02 08:21:34

标签: c# asp.net-mvc log4net

我为log4net

添加了这些内容
  1. [assembly: log4net.Config.XmlConfigurator(Watch = true)]

      

    在Global.asax.cs

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

      

    在控制器中我想要完成日志记录

  3. log.InfoFormat($"Some Info");

      

    应记录的信息

  4. 对文本文件的记录有效,但它不会将任何数据写入数据库。

    这是我的lo4net.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>   
    

    有什么我忘记的吗? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

首先尝试登录到一个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。

答案 1 :(得分:1)

通过调试这一切,我发现无法连接到数据库的问题是PublicKeyToken。

connectionType中的PublicKeyToken应具有以下值:

.. PublicKeyToken = b77a5c561934e089“

重要说明:对于log4net连接,它始终是相同的PublicKeyToken!