我的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);
答案 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);
有关详细信息,请阅读以下文章:
希望这有帮助。