0

我的 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);
4

1 回答 1

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/

希望这可以帮助。

于 2013-11-06T20:03:08.020 回答