问题描述
我正在尝试添加一个自定义属性,它是一个guid,但是它给了我这个错误:
I'm trying to add a custom property which is a guid, but it gives me this error:
我在配置中指定了此
<parameter>
<parameterName value="@id" />
<dbType value="Guid" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%X{id}" />
</layout>
</parameter>
我使用的实际代码(摘要)是这样:
The actual code (snippet) i use is this:
Guid guid = Guid.NewGuid();
if (defaultLogger.IsEnabledFor(level))
{
var loggingEvent = new LoggingEvent(ThisDeclaringType,
defaultLogger.Repository, defaultLogger.Name, level, message, exception);
loggingEvent.Properties["Id"] = guid;
有什么需要帮助的吗? :)数据库中的id字段被定义为uniqueidentifier NOT NULL,但它没有主键约束.
Any help please? :) The id field in the database is defined as a uniqueidentifier NOT NULL, but it does not have the primary key contraint.
推荐答案
对于您的示例,以下方法应该起作用:
For your example the following should work:
<parameter>
<parameterName value="@Oid" />
<dbType value="Guid" />
<layout type="log4net.Layout.RawPropertyLayout">
<key value="Id" />
</layout>
</parameter>
重要的是您将@id重命名为其他名称,否则即使您尝试插入字符串,也会在数据库中获得Null值,
Important is you rename @id to something else otherwise you will get Null values in database even if you try to insert strings,
然后使用RawPropertyLayout进行存储,因为您不需要进行转换.
And then use RawPropertyLayout to store, cus you do not need to do a convertion.
这篇关于使用uniqueidentifiers/guid作为log4net中的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!