Database target parameters

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Database target parameters

just3ws
Is it possible to send custom parameters to a database target?

Something along the lines of:

<commandText>insert [dbo].[myCustomLogTable] ( customField, customField2, customField3 ) values ( @customValue, @customValue2, @customValue3 )</commandText>
<parameter name="@customValue" layout="????" />
<parameter name="@customValue2" layout="????" />
<parameter name="@customValue3" layout="????" />

The question marks are where I run into the problems.  I can enter a ${literal:text=X} where X is my custom, albeit hard-coded value.
I'd like to be able to access the params array that I send into the log method.

I see that there are some layout renders available, but none seem to be able to access the values passed into the log methods.

log.Info("TheLogMessageString", "first custom value", "second custom value", "third custom value" );

Is there already some way to access the parameters array values, from a layout renderer perhaps?

Dziękują,
Michael Hall
Reply | Threaded
Open this post in threaded view
|

Re: Database target parameters

just3ws
I noticed in the next snapshot there is the addition of "event-context" to the list of layout renders.  This seems to be promising.  Although when I used the layout like...

<parameter name="@myCustomValue" layout="${event-context:item=Parameters[ 0 ]}" />
or
<parameter name="@myCustomValue" layout="${event-context:item=Parameters[ 0 ].ToString()}" />

I always read a 0, even though a different number was pass into the logging method.

Am I misunderstanding what the intent of the event-context layout was intended for?  Or am I just invoking it incorrectly?

Dziękują,
Michael Hall
Reply | Threaded
Open this post in threaded view
|

Re: Database target parameters

just3ws
In reply to this post by just3ws
Okay, if this is helpful.  I used the NLog v20060904 snapshot, which contains support for the event-context layout render.

Example of the target signature:

<target name="db" type="Database">
        <dbprovider>sqlserver</dbprovider>
        <connectionstring></connectionstring>
        <commandText>
                Insert [dbo].[myCustomLoggingTable]
                (
                        [customField],
                        [customField2],
                        [customField3]
                )
                Values
                (
                        @customValue,
                        @customValue2,
                        @customValue3
                );
        </commandText>
        <parameter name="@customValue" layout="${event-context:item=customValue}" />
        <parameter name="@customValue2" layout="${event-context:item=customValue2}" />
        <parameter name="@customValue3" layout="${event-context:item=customValue3}" />
</target>

Then my logging code looks like:

Int32 customValue = 1;
Int32 customValue2 = 2;
Int32 customValue3 = 3;
LogEventInfo eventInfo = new LogEventInfo( LogLevel.Trace, log.Name, "I *heart* NLog." );
eventInfo.Context[ "customValue" ] = customValue;
eventInfo.Context[ "customValue2" ] = customValue2;
eventInfo.Context[ "customValue3" ] = customValue3;
log.Log( eventInfo );

This works like a champ.  Not *exactly* what I was looking to do, but it gets the job done.

I got the idea after reading the posting under "http://www.nabble.com/Will-Eventlog-ID-be-added--tf2049712s6167.html#a5646305".

Dziękują,
Michael Hall