logging to database

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

logging to database

RidgeH
Hi All,

Iam new to NLog and just learning the basics. I want to log events to my database and for my application I have to also log the customerid for every log. How do I go about it ? This is what I have in my config file.
<targets>
<target name="database" type="Database">
        <dbProvider>mssql</dbProvider>
        <dbHost>HOSTNAME</dbHost>
        <dbDatabase>DBNAME</dbDatabase>
        <dbUserName>USERID</dbUserName>
        <dbPassword>PASSWORD</dbPassword>
        <commandText>INSERT INTO LOG(LOG.DATE,LOG.LEVEL,LOG.LOGGER,LOG.MESSAGE) VALUES(@time_stamp,@level,@logger,@message);</commandText>
        <parameter name="@time_stamp" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@logger" layout="${logger}" />
        <parameter name="@message" layout="${message}" />
      </target>
</targets>

This works just fine. I want to add another column to the Log table called CustomerID, how do i change the config file and how do i use it in my code. Example would be great !!  Iam using VB.NET and VS 2005

Thanks
RH
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

Jarek Kowalski
Administrator
RidgeH wrote
Hi All,

Iam new to NLog and just learning the basics. I want to log events to my database and for my application I have to also log the customerid for every log. How do I go about it ? This is what I have in my config file.
<targets>
<target name="database" type="Database">
        <dbProvider>mssql</dbProvider>
        <dbHost>HOSTNAME</dbHost>
        <dbDatabase>DBNAME</dbDatabase>
        <dbUserName>USERID</dbUserName>
        <dbPassword>PASSWORD</dbPassword>
        <commandText>INSERT INTO LOG(LOG.DATE,LOG.LEVEL,LOG.LOGGER,LOG.MESSAGE) VALUES(@time_stamp,@level,@logger,@message);</commandText>
        <parameter name="@time_stamp" layout="${date:format=yyyy-MM-dd HH\:mm\:ss}" />
        <parameter name="@level" layout="${level}" />
        <parameter name="@logger" layout="${logger}" />
        <parameter name="@message" layout="${message}" />
      </target>
</targets>

This works just fine. I want to add another column to the Log table called CustomerID, how do i change the config file and how do i use it in my code. Example would be great !!  Iam using VB.NET and VS 2005

Thanks
RH
1. You of course need to add a new column to your DB.
2. Then change your insert statement to actually insert values into this column.
3. You also need an extra <parameter /> tag, which is dependent on the way you want to pass this CustomerID value.

You have the following options:

1. You may set CustomerID to be global (per-application) value with:

// do this once per application or whenever CustomerID changes
GDC.Set("CustomerID", "someID");

In this case use <parameter name="@customerID" value="${gdc:CustomerID}" />

2. You may want to set it per-thread:

// do this in each thread
MDC.Set("CustomerID", "someID");

In this case use <parameter name="@customerID" value="${mdc:CustomerID}" />

3. Depending on where you store your customer ID, you may also use ${aspnet-request}, ${aspnet-session}, ${aspnet-application}, ${event-context}, ${environment}, ${identity}, ${aspnet-user-identity} or write your own layout renderer to return CustomerID to be logged.

I hope it helps.

Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

dshiff
I am using this suggestion to add a column in my log table to collect a taskid.
I put this in my NLog.config but receive an error message:

<parameter name="@taskid" value="${mdc:taskid}"/>

Error: "The 'value' attribute is not declared"
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

Jarek Kowalski
Administrator
That should be "layout" instead of "value".
 
Jarek
NLog Blog
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

dshiff
I tried that before I posted. That throws another error:

<parameter name="@taskid" layout="${mdc:taskid}"/>

Error: "The 'layout' attribute is invalid - The value '${mdc:taskid}' is invalid according to its datatype 'http://www.nlog-project.org/schemas/NLog.xsd:NLogLayout' - The Pattern constraint failed."
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

dshiff
I think I found a post with the corrected syntax

Use:
<parameter name="@taskid" layout="${mdc:item=taskid}"/>

Not:
parameter name="@taskid" layout="${mdc:taskid}"/>



No more questions today. Thanks for all the help!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: logging to database

Harsha
In reply to this post by Jarek Kowalski
Do you a sample code for this like how to call multiple targets in code for multiple tables.
Loading...