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"?>
        <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
   <nlog xmlns="" 
         xsi:schemaLocation=" NLog.xsd">
            <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}"/>
            <logger name="*" minLevel="Trace" writeTo="debugger" />
            <logger name="*" minLevel="Debug" writeTo="logfile"/>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />

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: