Problem with Adding my own sqlserver columns to logging

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

Problem with Adding my own sqlserver columns to logging

pkellner
I've followed a post in this forum on how to do it, but can't quite get it to work.  When I debug through NLog, I can see that the sqlcomand is formed correctly, but I don't get my own values back. I've tried lots of combinations (with parameters, context, properties, etc) but none have had success.  Below is the relevant code I'm using.

Also, I anticipate my next problem will be that one of my columns is a long type.  How can I deal with that?

thanks, -Peter


 var logEventInfo = new LogEventInfo(LogLevel.Warn, "MyLoggerName", "NylogMessage");
            logEventInfo.Properties.Add("username","MyUsername");
            logEventInfo.Properties.Add("application","MyAppName");
            logEventInfo.Properties.Add("executiontimems","999");


    <target xsi:type="Database" name="db">
     
      <commandText>INSERT INTO NLogEntries (TimeStamp, Message, Level, Logger,Username,ApplicationName,ExecutionTimeMs) VALUES(getutcdate(), @msg, @level, @logger,@username,@applicationname,@executiontimems)</commandText>

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

      <parameter name="@username" layout="${event-context:username}"/>
      <parameter name="@applicationname" layout="${event-context:applicationname}"/>
      <parameter name="@executiontimems" layout="${event-context:executiontimems}"/>
Peter Kellner
http://peterkellner.net
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Adding my own sqlserver columns to logging

Jason LaFlair
Administrator
Just tried it with NLog v2.0 in a quick test app.

The only thing I saw was: logEventInfo.Properties.Add("application","MyAppName");
and your mapped your value as: <parameter name="@applicationname" layout="${event-context:applicationname}"/>
instead of: <parameter name="@applicationname" layout="${event-context:application}"/>

Otherwise I setup my database table to be and all worked fine.

CREATE TABLE [dbo].[NLogEntries](
        [TimeStamp] [datetime] NULL,
        [Message] [varchar](2000) NULL,
        [Level] [varchar](2000) NULL,
        [Logger] [varchar](2000) NULL,
        [Username] [varchar](2000) NULL,
        [ApplicationName] [varchar](2000) NULL,
        [ExecutionTimeMs] [bigint] NULL
)
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Adding my own sqlserver columns to logging

pkellner
Hmm, I'm still not getting the extra parameters logged.  Somehow, I don't think I'm getting the extra parameters added to the collection.  I took  a screen shot of what I have in my debugger.  It's not obvious to me how to turn on the internal tracing (though I'm not surprised it is there and  so complete).

Do you have the sample vs project you put together? if so, it might be easiest for me to see a simple example (I'll even blog it so next person like me has a simple path to follow).

http://peterkellner.net/misc/nlog1.png
Peter Kellner
http://peterkellner.net
Reply | Threaded
Open this post in threaded view
|

Re: Problem with Adding my own sqlserver columns to logging

pkellner
In reply to this post by Jason LaFlair
Never mind my last question.  It is working.  I was not using the correct call.  Here is what works:

  var logEventInfo = new LogEventInfo(LogLevel.Warn, "MyLoggerName", message);
            logEventInfo.Properties.Add("username", "MyUsername");
            logEventInfo.Properties.Add("applicationname", "MyAppName");
            logEventInfo.Properties.Add("executiontimems", "99999");
            _logger.Log(logEventInfo);

Thanks for your help. -Peter
Peter Kellner
http://peterkellner.net