Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using uniqueidentifiers/guids as custom properties in log4net

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.

like image 850
Per Hornshøj-Schierbeck Avatar asked Jan 08 '09 11:01

Per Hornshøj-Schierbeck


1 Answers

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.

like image 83
Claus Thomsen Avatar answered Jan 02 '23 02:01

Claus Thomsen