the_tr00per
the_tr00per

Reputation: 439

NLog issue writing object to database using WCF C#.NET

I'm currently having a problem writing a XML value to a database table using NLog. I'm using WCF and C#

I have a request XML object that i'm writing to the database, and if i use the built in "message" object as follows, it works:

NLog.Config

<parameter name="@message" layout="${message}" />

Code behind:

logger.Debug("{@value1}", new { request1 = request});

But if i try and write it to another field, for example:

<parameter name="@Request" layout="${event-properties:value1}" />

It doesn't write the value of the object to the table, but:

"MyServices.Types.Request"

Request type is:

namespace MyServices.Types
{
    [DataContract]
    public class Request
    {
        [DataMember]
        public string MessageHeader { get; set; }
        [DataMember]
        public int EventType { get; set; }
        [DataMember]
        public string Identification { get; set; }
        [DataMember]
        public DateTime VisitDate { get; set; }

    }
}

Is there a reason for this? The "Message" property seems to work correctly

Upvotes: 3

Views: 235

Answers (2)

Julian
Julian

Reputation: 36790

(this is an answer for an older NLog version. For the latest, see Rolf's answer)

Even with structured logging, the event properties aren't converted to a structured format - it's using a ToString

Currently JSON serialize is only supported, not XML

<parameter name="@Request" layout="${event-properties:value1:format=@}" />

See ${event-properties} docs for more options and examples

Upvotes: 4

Rolf Kristensen
Rolf Kristensen

Reputation: 19867

NLog 4.6 allows you to render XML into the DatabaseTarget:

<parameter name="@Request" DbType="SqlDbType.Xml">
   <layout type="xmllayout" includeAllProperties="true" />
</parameter>

NLog 4.6 also allows you set the DbType for the parameter. See aslo https://github.com/NLog/NLog/wiki/XmlLayout

Upvotes: 2

Related Questions