gpt4 book ai didi

c# - 如何使用 Log4net 将 Guid 插入数据库?

转载 作者:行者123 更新时间:2023-11-30 23:14:11 25 4
gpt4 key购买 nike

这是我的 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 部分之外的所有内容。

我需要做什么来解决这个问题?

最佳答案

使用 RawPropertyLayout而不是 PatternLayout:

<parameter>
<parameterName value="@guid" />
<dbType value="Guid" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="RequestGuid" />
</layout>
</parameter>

关于c# - 如何使用 Log4net 将 Guid 插入数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43258435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com