log4net写文件没问题,但无法插入数据库

时间:2016-08-10 10:33:39

标签: log4net log4net-configuration

这是我的配置文件包含log4net属性,如果我使用“RollingLogFileAppender”appender它正确写入文件但是我正在使用 “AdoNetAppender”并期望在插入行的消息列中看到相同的字符串。但不知怎的,这不起作用..

(表定义和查询配置来自apache文档所以它不能错我虽然连接字符串是我的问题,但是应用程序已经连接到同一个db我也共享连接字符串正被adonet使用)

<log4net debug="true">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\AkisLog\\Logs.txt" />
      <appendToFile value="true" />
      <rollingStyle value="Size" />
      <maxSizeRollBackups value="10" />
      <maximumFileSize value="10MB" />
      <staticLogFileName value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
      </layout>
    </appender>
    <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
      <bufferSize value="100" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="data source=******\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd" />
      <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @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="@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>
    <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
    </root>
  </log4net>


 ...
<connectionStrings>
    <add name="TedasDbContext" connectionString="metadata=res://*/Project.csdl|res://*/Project.ssdl|res://*/Project.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=****\SQLTEST;initial catalog=LISANSSIZ_SANTRAL_PROJE_ONAY;persist security info=True;user id=user;password=pwd;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

和.cs文件:

private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

if (log.IsErrorEnabled)
{
    log.Error("Error ocurred here");
}

if (log.IsDebugEnabled)
{
    log.Debug("Debug message");
}

1 个答案:

答案 0 :(得分:2)

你的buffersize是100,这意味着消息将在100条消息之后写入。将其更改为1,它会将消息直接刷新到sql数据库:

 <bufferSize value="1" />

如果这不起作用,请启用log4net内部调试以查看是否存在连接问题。另请检查您是否拥有最新版本的log4net。