Sunday, 5 August 2012

NLog config targets -Database, Text File, EventLog

Example of NLog Targets



<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">




  <variable name="applicationName" value="Application" />
 
 
  <!-- do not change -->
  <variable name="eventLogTaget" value="ProjectName" />
 
  <targets>
    <!-- NLogViewer logger -->
    <!-- Text file logger -->
   
    <target name="logFile" xsi:type="File"
            layout="${longdate} ${level} ${message}"
            fileName="${basedir}/${shortdate}.log" />


   
   
    <target name="log" xsi:type="EventLog"
            layout="${logger}: ${message} ${exception:format=ToString}"
            log="${eventLogTaget}" source="${applicationName}" />


    <target name="logdb" type="Database">
      <dbprovider>mssql</dbprovider>
      <dbhost>SERVER2</dbhost>
      <dbdatabase>NLogDatabase</dbdatabase>
      <dbusername>temp</dbusername>
      <dbpassword>temp</dbpassword>


      <commandText>
        INSERT INTO dbo.LogTable([level]
        ,[logger]
        ,[message]
        ,[exception])
        VALUES( @level, @logger, @message, @exception);
      </commandText>
     
      <parameter name="@level" layout="${level}" />
      <parameter name="@logger" layout="${logger}" />
      <parameter name="@message" layout="${message}" />
      <parameter name="@exception" layout=" ${exception:format=ToString}" />
    </target>
   
  </targets>
  <rules>
    <!--<logger name="*" levels="Trace, Info, Debug, Warn, Error, Fatal" writeTo="eventlog"/>-->
    <logger name="*" levels="Trace, Info, Debug, Warn, Error, Fatal" writeTo="log, logdb, logFile"/>
  </rules>
</nlog>