views:

791

answers:

1

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

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

I specify this in the config:

<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;

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.

+6  A: 

For your example the following should work:

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

Important is you rename @id to something else otherwise you will get Null values in database even if you try to insert strings,

And then use RawPropertyLayout to store, cus you do not need to do a convertion.

Claus Thomsen
You are so learned Papa Homer :)
Per Hornshøj-Schierbeck