Log4net AdoNetAppender未保存到Db

时间:2013-11-06 17:14:25

标签: asp.net asp.net-mvc log4net log4net-configuration

我的AdoNetAppender出现问题 - 它只是没有记录。我已经阅读了log4net手册和各种其他链接,但看不出我做错了什么。以下是我的配置文件以及我如何调用它,所以任何帮助都将非常感谢!

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  <connectionString value="***" />
  <commandText value="dbo.spx_CreatePageLog" />
  <commandType value="StoredProcedure" />
  <!--<parameter>
    <parameterName value="@log_date" />
    <dbType value="DateTime" />
    <layout type="log4net.Layout.RawTimeStampLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>-->
  <parameter>
    <parameterName value="@HTTPMethod" />
    <dbType value="String" />
    <size value="10" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@URI" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%URI{URI}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@RequestData" />
    <dbType value="String" />
    <size value="1024" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%RequestData{RequestData}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@UserID" />
    <dbType value="int" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%UserID{UserID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@UserToDealerID" />
    <dbType value="int" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%UserToDealerID{UserToDealerID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@AdminID" />
    <dbType value="String" />
    <size value="256" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%AdminID{AdminID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@SessionID" />
    <dbType value="String" />
    <size value="256" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%SessionID{SessionID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@BrowserInfo" />
    <dbType value="String" />
    <size value="1024" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%BrowserInfo{BrowserInfo}" />
    </layout>
  </parameter>
  <!--<parameter>
    <parameterName value="@exception" />
    <dbType value="String" />
    <size value="2000" />
    <layout type="log4net.Layout.ExceptionLayout" />
  </parameter>-->
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="AdoNetAppender" />
</root>

string RequestData = auditModel.ParameterValues.GetDictionaryIntoString<string>();
        string BrowserInfo = auditModel.BrowserInfo.GetDictionaryIntoString<string>();

        //log4net.LogicalThreadContext.Properties["HTTPMethod"] = auditModel.Method;
        log4net.GlobalContext.Properties["URI"] = auditModel.RequestUrl;
        log4net.GlobalContext.Properties["RequestData"] = RequestData;
        log4net.GlobalContext.Properties["UserID"] = auditModel.UserID;
        log4net.GlobalContext.Properties["UserToDealerID"] = auditModel.UserToDealerID;
        log4net.GlobalContext.Properties["AdminID"] = auditModel.AdminID;
        log4net.GlobalContext.Properties["SessionID"] = "123456"; //auditModel.SessionID;
        log4net.GlobalContext.Properties["BrowserInfo"] = BrowserInfo;

        Logger.Debug(auditModel.Method);

1 个答案:

答案 0 :(得分:0)

您正在尝试使用存储过程将Log4Net日志存储在数据库中。 从你拥有的,我只能想到可能导致问题的这两件事。

1)conversionPattern的值不正确。见下文:

   <parameter>
    <parameterName value="@HTTPMethod" />
    <dbType value="String" />
    <size value="10" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%message" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@URI" />
    <dbType value="String" />
    <size value="512" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{URI}" /> <- here use property instead of "UserID"
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@RequestData" />
    <dbType value="String" />
    <size value="1024" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{RequestData}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@UserID" />
    <dbType value="int" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{UserID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@UserToDealerID" />
    <dbType value="int" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{UserToDealerID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@AdminID" />
    <dbType value="String" />
    <size value="256" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{AdminID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@SessionID" />
    <dbType value="String" />
    <size value="256" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{SessionID}" />
    </layout>
  </parameter>
  <parameter>
    <parameterName value="@BrowserInfo" />
    <dbType value="String" />
    <size value="1024" />
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="%property{BrowserInfo}" />
    </layout>
  </parameter>

2)如果您更改为上面的代码,它仍然无效。考虑log4Net参数是否  匹配存储过程中的参数。您可以尝试使用虚假数据并执行  存储过程在Logger.Debug()之前的任何地方。如果它有效,问题是,

 Logger.Debug(auditModel.Method);

有关详细信息,请阅读以下文章:

http://www.l4ndash.com/News/tabid/69/ctl/ArticleView/mid/413/articleId/5/Using-Log4Net-to-log-from-Database-Stored-procedures.aspx

http://blogs.claritycon.com/blog/2006/02/log4net-versus-enterprise-library-2-0-logging-application-block-part-ii-database-logging/

希望这有帮助。