Home > database >  Nlog - Web.config rules - log file only for Global.asax file?
Nlog - Web.config rules - log file only for Global.asax file?

Time:10-28

I am trying to add an extra log file to my existing NLog implementation. I already have an Nlog file logger, but I want to add another for certain logs.

I am having trouble keeping these logs separate, as the existing calls use LogManager.GetCurrentClassLogger() and it seems to just append onto the log I want to keep separate.

The new log file should only be populated by logs in the Global.asax file, but adding that ruleset to the Web.config does not work. Is there a special way to define a rule for the Global.asax file?

Global.asax file

namespace TestOne.Browser
{
using System.Net;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using DataAnnotationsExtensions.ClientValidation;

using WebMatrix.WebData;

public class MvcApplication : HttpApplication
{
    protected void Application_EndRequest()
    {
        ...

        var logger = LogManager.GetLogger("diagnosticsfile");
        logger.Info("{0}, {1}, {2}, {3}", user, pageName, totalClientTimings, duration);

        ...
    }
}
}

Web.config file

<nlog>
    <variable name="root" value="C:/TestOne/Logs/Browser" />
    <targets>
      <target name="file" type="File" header="Starting logging..." footer="Stopping logging..." layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${uppercase:${level}} ${callsite} ${aspnet-user-identity}${newline}${message} ${exception:format=ToString:maxInnerExceptionLevel=5}" fileName="${root}/Browser.log" archiveFileName="${root}/Archive/Browser.{##}.log" createDirs="true" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="31" concurrentWrites="false" keepFileOpen="false" />
      <target name="diagnosticsfile" type="File" header="Time, LogType, User, PageName, TotalClientTimings, Duration (ms)" layout="${longdate},${level:uppercase=true},${message}" fileName="${root}/DiagnosticsLog.log" archiveFileName="${root}/Archive/Diagnostics.{##}.log" createDirs="true" archiveEvery="Day" archiveNumbering="Rolling" maxArchiveFiles="31" concurrentWrites="false" keepFileOpen="false" />
    </targets>
    <rules>
      <logger name="*" minlevel="Trace" writeTo="file" />
      <logger name="TestOne.Browser.MvcApplication" minlevel="Info" writeTo="diagnosticsfile" />
    </rules>
  </nlog>

With this current configuration, nothing gets written to my diagnostics file (the new log file). If I replace the TestOne.Browser.MvcApplication with * in the Nlog rules, the file gets written to, but then the existing logs in other classes that use LogManager.GetCurrentClassLogger() sometimes append to the diagnostics log (which they should not do). Is there a way to configure this so that doesn't happen?

CodePudding user response:

The logging rules decides where logger-output should be written. Your logging-rules says that ONLY loggers with name TestOne.Browser.MvcApplication should write to the diagnosticsfile-target:

    <rules>
      <logger name="*" minlevel="Trace" writeTo="file" />
      <logger name="TestOne.Browser.MvcApplication" minlevel="Info" writeTo="diagnosticsfile" />
    </rules>

But in your code you are using LogManager.GetLogger("diagnosticsfile"):

To specify that logger-name diagnosticsfile should write to the diagnosticsfile-target, you can do like this:

    <rules>
      <logger name="diagnosticsfile" minlevel="Info" writeTo="diagnosticsfile" final="true" />
      <logger name="*" minlevel="Trace" writeTo="file" />
    </rules>

Notice the final="true" to ensure logger-output will not flow to the catch-all-rule that writes to file-target.

See also: https://github.com/NLog/NLog/wiki/Tutorial

See also: https://github.com/nlog/NLog/wiki/Configuration-file#rules

  • Related