【问题标题】:How to prevent nlog from using config file?如何防止 nlog 使用配置文件?
【发布时间】:2013-09-12 04:54:27
【问题描述】:

NLog的行为可以配置both via configuration files and programmatically

似乎当我设置编程规则时,仍然处理存在的任何配置文件,因此这两种机制同时发挥作用。这准确吗?

是否可以将 NLog 设置为仅使用编程规则并阻止它读取任何配置文件?

【问题讨论】:

    标签: c# nlog


    【解决方案1】:

    为了扩展 Sergey 的回答,假设您有一个 LogFactory:

    var factory = new LogFactory()
    

    当它被创建时,它会尝试从配置文件中读取配置数据。您可以调整或补充此配置

    factory.Configuration.LoggingRules.Add( ... )
    

    不过你也可以替换你自己的配置:

    factory.Configuration = new LoggingConfiguration();
    

    在这种情况下,任何已加载的配置都将被丢弃,现在您将继续使用您在代码中创建的规则。

    【讨论】:

      【解决方案2】:
      LoggingConfiguration config = new LoggingConfiguration(); //Create configuration object in code
      Logger Log = LogManager.GetCurrentClassLogger(); //Load configuration from xml file
      

      如果你分别使用其中一种方法,它就会起作用

      更多信息:https://github.com/nlog/nlog/wiki

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-24
        • 1970-01-01
        • 1970-01-01
        • 2021-12-08
        • 1970-01-01
        • 2011-10-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多