这是我的log4net.config
<?xml version="1.0" encoding="utf-8"?>
<log4net>
<root>
<level value="ALL" />
</root>
<logger name="RequestLogger">
<level value="INFO" />
<appender-ref ref="AdoNetAppenderForRequestLogs" />
</logger>
<appender name="AdoNetAppenderForRequestLogs" 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="INSERT INTO Log4net_Requests ([Guid], [Date], [Url], [Headers], Controller, Action, RouteData) VALUES (@guid, @log_date, @url, @headers, @controller, @action, @routeData)" />
<parameter>
<parameterName value="@guid" />
<dbType value="Guid" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RequestGuid}" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value="@url" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Url}" />
</layout>
</parameter>
<parameter>
<parameterName value="@headers" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Headers}" />
</layout>
</parameter>
<parameter>
<parameterName value="@controller" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Controller}" />
</layout>
</parameter>
<parameter>
<parameterName value="@action" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{Action}" />
</layout>
</parameter>
<parameter>
<parameterName value="@routeData" />
<dbType value="String" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%property{RouteData}" />
</layout>
</parameter>
</appender>
</log4net>
在我的代码中,我设置了这样的属性:
Request.Id
已经是有效的Guid。
GlobalContext.Properties["Url"] = RequestLog.Url;
GlobalContext.Properties["RequestGuid"] = RequestLog.Id; // .ToString() doesn't work either
GlobalContext.Properties["Headers"] = RequestLog.GetHeaders();
GlobalContext.Properties["Controller"] = RequestLog.Controller;
GlobalContext.Properties["Action"] = RequestLog.Action;
GlobalContext.Properties["RouteData"] = RequestLog.RouteData;
当我尝试记录时,我在log4net.txt日志文件中得到此异常
System.InvalidCastException: Failed to convert parameter value from a String to a Guid. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.
at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType, Boolean& coercedToDataFeed, Boolean& typeChanged, Boolean allowStreaming)
at System.Data.SqlClient.SqlParameter.GetCoercedValue()
at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.BuildParamList(TdsParser parser, SqlParameterCollection parameters, Boolean includeReturnValue)
at System.Data.SqlClient.SqlCommand.BuildExecuteSql(CommandBehavior behavior, String commandText, SqlParameterCollection parameters, _SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at log4net.Appender.AdoNetAppender.SendBuffer(IDbTransaction dbTran, LoggingEvent[] events)
at log4net.Appender.AdoNetAppender.SendBuffer(LoggingEvent[] events)
如果我删除guid,日志会存储在数据库中,所以除了guid部分外我知道一切。
我需要做些什么来解决这个问题?
答案 0 :(得分:1)
尝试创建一个新的Guid
对象并将其分配给RequestGuid
属性:
GlobalContext.Properties["RequestGuid"] = new Guid(RequestLog.Id);
如提供的MSDN链接中所述,上面使用的构造函数:
使用该值初始化Guid结构的新实例 由指定的字符串表示。
答案 1 :(得分:0)
使用RawPropertyLayout
代替PatternLayout
:
<parameter>
<parameterName value="@guid" />
<dbType value="Guid" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="RequestGuid" />
</layout>
</parameter>