【问题标题】:Log only to a specific target at runtime在运行时仅记录到特定目标
【发布时间】:2015-05-07 23:35:37
【问题描述】:

我将 NLog 用于两个目标:

<?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">

  <targets async="true">
    <target name="logfile" xsi:type="File" fileName="my.log"/>
    <target name="console" xsi:type="Console"/>
  </targets>

  <rules>
    <logger name="*" minlevel="Trace" writeTo="logfile"/>
    <logger name="*" minlevel="Info" writeTo="console"/>
  </rules>
</nlog>

是否可以只将消息记录到“日志文件”目标,而不将消息写入“控制台”目标?

编辑

澄清一下:我想在运行时将来自同一类的消息定向到不同的记录器(无需更改 XML)。比如:

class Program
{
    static Logger _logger = LogManager.GetCurrentClassLogger();

    static void Main(string[] args)
    {
        // Note - _logger.InfoToTarget() does not really exist
        _logger.InfoToTarget("logfile", "This is my very detailed message, possibly with object dumps");
        _logger.InfoToTarget("console", "Short message");
    }
}

我知道这会将我的代码与 NLlog.config 文件结合在一起。

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    完成您正在寻找的功能的一种方法是命名您的记录器

    _logger = LogManager.GetLogger("MyConsoleLogger")
    _logger.Info("This will log to the console...");
    _logger = LogManager.GetLogger("MyFileLogger")
    _logger.Trace("This will log to a file...");
    

    而不是使用

    LogManager.GetCurrentClassLogger().              
    

    然后你可以在你的配置文件中列出规则

    <rules>
    <logger name="MyFileLogger" minlevel="Trace" writeTo="logfile"/>
    <logger name="MyConsoleLogger" minlevel="Info" writeTo="console"/>
    </rules>
    

    到目前为止,这不是最漂亮的解决方案,但它确实为您提供了您正在寻找的功能。

    【讨论】:

      【解决方案2】:

      有几种方法可以做到这一点,正确的方法取决于你的情况。

      请记住,您通常希望避免让您的应用过多地了解日志记录的内部工作原理。如果可能的话,最好配置 nlog 来决定应该记录的地方。

      是否存在不应记录到控制台的特定命名空间?这很容易配置。此外,您可以使用“何时”过滤器 (https://github.com/nlog/nlog/wiki/When-filter) 或条件 (https://github.com/nlog/nlog/wiki/Conditions)

      最好有多个记录器实例,因此您可以调用适合每种情况的一个(每个类记录器)(Why do loggers recommend using a logger per class?)。

      【讨论】:

        【解决方案3】:

        当然,但是我假设您的意思是在发布时您不再希望登录到控制台。您可以通过删除或注释掉写入控制台目标的侦听器来非常轻松地做到这一点。现在它只会写入日志文件目标。

        <?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">
        
            <targets async="true">
               <target name="logfile" xsi:type="File" fileName="my.log"/>
               <target name="console" xsi:type="Console"/>
            </targets>
        
            <rules>
               <logger name="*" minlevel="Trace" writeTo="logfile"/>
               <!--<logger name="*" minlevel="Info" writeTo="console"/>-->
            </rules>
        </nlog>
        

        写入控制台的规则现在已停用,但日志文件处于活动状态。如果这是在发布期间,您可能希望更改您的规则以不将您的跟踪日志记录为日志文件的最低级别,因为它会因 IO 过多而减慢您的应用程序的速度。我过去曾问过这个问题,看来最好的做法是通过 XML 配置文件来做到这一点。 (Logging in Release Build of Application (C#))

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多