My Technical Notes

Friday, 7 November 2014

NLog sample configuration in Web.config

Following is a `web.config` file with NLog configuration, with two targets, `debugger` which logs to the Output Window, and `logfile` which logs to the log file path specified:


<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
   </configSections>
   <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">
        <targets>
            <target name="debugger" xsi:type="Debugger" layout="${longdate} ${callsite} ${level} ${message}" />
            <target name="logfile" xsi:type="File" fileName="c:\temp\scu.log.txt" layout="${longdate} ${callsite} ${level} ${message}"/>
        </targets>
        <rules>
            <logger name="*" minLevel="Trace" writeTo="debugger" />
            <logger name="*" minLevel="Debug" writeTo="logfile"/>
        </rules>
    </nlog>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

Here is a sample test web page's code behind:


// using statements
using NLog;

public partial class WebForm1 : System.Web.UI.Page
{
    private static Logger logger = LogManager.GetCurrentClassLogger();

    protected void Page_Load(object sender, EventArgs e)
    {
        logger.Trace("trace statement");
        logger.Debug("debug statement");
    }
}

No comments: