简体   繁体   English

NLog - 2 个独立的日志文件 - 如何写入其中一个

[英]NLog - 2 separate log files - How to write to one or the other

I want to explicitly write to 2 different log files .我想明确写入 2 个不同的日志文件 Based on the method or type of operations.基于操作的方法或类型。

How to?如何?

I have seen the following StackOverFlow post Having NLog loggers with different configuration我已经看到以下 StackOverFlow 帖子拥有具有不同配置的 NLog 记录器

How do I configure NLog?如何配置 NLog? What code do I need too, so I can write to 1 file or the other?我还需要什么代码,所以我可以写入 1 个文件或另一个? using code like: log.Error("My Big Error") ;使用如下代码: log.Error("My Big Error") ;

The post contains the following该帖子包含以下内容

<targets> 
    <target name="f1" xsi:type="File" fileName="${logger}.txt" />
    <target name="f2" xsi:type="File" fileName="${shortdate}.txt" />
</targets>

So, if it's 'general error' I want write to f1 .所以,如果是“一般错误”,我想写信给f1 If it's file operation error I want to write to f2如果是文件操作错误我想写到f2

thx in advance提前谢谢

In the NLog configuration you need to set up some rules to write to the targets (otherwise nothing get logged).在 NLog 配置中,您需要设置一些规则来写入目标(否则不会记录任何内容)。 In those rules you could add filters and conditions.在这些规则中,您可以添加过滤器和条件。

Simple example简单的例子

For example:例如:

<rules>
  <logger name="Logger1" writeTo="f1" />
  <logger name="Logger2" writeTo="f2" />
</rules>

The name attribute is here a filter, so the first rule means: write to f1 if logger name is equals to "Logger1". name属性在这里是一个过滤器,所以第一条规则意味着:如果 logger name 等于“Logger1”,则写入 f1。 The rules are processed from top to bottom.规则从上到下处理。

So when calling LogManager.GetLogger("Logger1").Info("My message") this will write to target f1 and LogManager.GetLogger("Logger2").Info("My message") will write to target f2 .因此,当调用LogManager.GetLogger("Logger1").Info("My message")这将写入目标f1LogManager.GetLogger("Logger2").Info("My message")将写入目标f2

GetCurrentClassLogger GetCurrentClassLogger

When using LogManager.GetCurrentClassLogger() , the logger name is constructed of the current class and namespace name, eg "MyNameSpace.MyClass".使用LogManager.GetCurrentClassLogger()时,记录器名称由当前 class 和命名空间名称构成,例如“MyNameSpace.MyClass”。 This means you could adjust the name attribute to name="MyNameSpace.MyClass" , or name="*.MyClass" for matching.这意味着您可以将 name 属性调整为name="MyNameSpace.MyClass"name="*.MyClass"进行匹配。

Final最后

You could also write this:你也可以这样写:

<rules>
  <logger name="Logger1" writeTo="f1" final="true" />
  <logger name="*" writeTo="f2" />
</rules>

This will write events of Logger1 to f1 , and others to f2 .这会将 Logger1 的事件写入f1 ,将其他事件写入f2 You probably need the filter attribute, otherwise also the events of Logger1 will be written to f2 - and that isn't always what you need.您可能需要 filter 属性,否则 Logger1 的事件也将写入f2 - 这并不总是您需要的。

Other filter rules其他过滤规则

There are many more filter options, eg min level, max level, but also more advanced filters (not only on logger name).还有更多过滤器选项,例如最小级别、最大级别,还有更高级的过滤器(不仅在记录器名称上)。 You could read more about that here你可以在这里阅读更多相关信息

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

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