使用uniqueidentifiers/guid作为log4net中的自定义属性 [英] Using uniqueidentifiers/guids as custom properties in log4net

查看:209
本文介绍了使用uniqueidentifiers/guid作为log4net中的自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加一个自定义属性,它是一个guid,但是它给了我这个错误:

I'm trying to add a custom property which is a guid, but it gives me this error:

System.InvalidCastException:失败 从字符串转换参数值 给一个向导. ---> System.InvalidCastException:无效 从'System.String'强制转换为 "System.Guid".

System.InvalidCastException: Failed to convert parameter value from a String to a Guid. ---> System.InvalidCastException: Invalid cast from 'System.String' to 'System.Guid'.

我在配置中指定了此

<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中的自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆