简体   繁体   English

Nlog - Web.config 规则 - 仅用于 Global.asax 文件的日志文件?

[英]Nlog - Web.config rules - log file only for Global.asax file?

I am trying to add an extra log file to my existing NLog implementation.我正在尝试向我现有的 NLog 实现添加一个额外的日志文件。 I already have an Nlog file logger, but I want to add another for certain logs.我已经有一个 Nlog 文件记录器,但我想为某些日志添加另一个。

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.我无法将这些日志分开,因为现有调用使用LogManager.GetCurrentClassLogger()并且它似乎只是附加到我想分开的日志上。

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.新日志文件应仅由 Global.asax 文件中的日志填充,但将该规则集添加到 Web.config 中不起作用。 Is there a special way to define a rule for the Global.asax file?是否有一种特殊的方法可以为 Global.asax 文件定义规则?

Global.asax file Global.asax 文件

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 Web.config 文件

<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).如果我在 Nlog 规则中用*替换TestOne.Browser.MvcApplication ,该文件将被写入,但是使用LogManager.GetCurrentClassLogger()其他类中的现有日志有时会附加到诊断日志(他们不应该这样做) . Is there a way to configure this so that doesn't happen?有没有办法对此进行配置,以免发生这种情况?

The logging rules decides where logger-output should be written.日志规则决定了 logger-output 应该写在哪里。 Your logging-rules says that ONLY loggers with name TestOne.Browser.MvcApplication should write to the diagnosticsfile -target:您的日志记录规则说只有名称为TestOne.Browser.MvcApplication记录器应该写入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") :但是在您的代码中,您使用的是LogManager.GetLogger("diagnosticsfile")

To specify that logger-name diagnosticsfile should write to the diagnosticsfile-target, you can do like this:要指定记录器名称diagnosticsfile应写入诊断文件目标,您可以这样做:

    <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.请注意final="true"以确保记录器输出不会流向写入file -target 的 catch-all-rule。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM