具有不同配置的NLog日志记录器 [英] Having NLog loggers with different configuration

查看:834
本文介绍了具有不同配置的NLog日志记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在NLog中可以创建多个不同配置的记录器吗?

In NLog is possible to create multiple loggers with different configuration?

我有一个组件,每次实例化都必须将所有事件记录到与新实例相关的不同文件中。

I have a component that every time that is instanced must log all events to a different file that is related to the new instance.

这可能与NLog吗?如果没有,有没有这样的日志框架?

Is this possible with NLog? If not, there are logging frameworks that do that?

推荐答案

是的,你可以这样做。您可以配置该类型的日志记录器以记录到特定目标。或者,您可以配置该类型的记录器以记录到目标(例如文件),根据记录器名称自动命名文件。

Yes, you can do that. You can either configure the logger for that type to log to a specific target. Or you can configure the logger for that type to log to a target (such as a file), naming the file (automatically) based on the logger name.

请参阅 NLog

See the NLog config file documentation here for some examples.

此外,请参阅我的在这里一些配置文件提示。

Also, see my post here for some config file tips.

这是一个非常简单的示例如何配置两个记录器:一个用于将特定类型记录到为该类型命名的输出文件,一个用于基于日期记录到文件的所有其他记录器。

Here is a very brief example of how you might configure two loggers: one for a specific type to be logged to an output file named for that type and one for all other loggers to log to a file based on the date.

<nlog>
  <targets> 
    <target name="f1" xsi:type="File" fileName="${logger}.txt" />
    <target name="f2" xsi:type="File" fileName="${shortdate}.txt" />
  </targets>
  <rules>
    <logger name="Name.Space.Class1" minlevel="Trace" writeTo="f1" />  
    <logger name="*" levels="Debug" writeTo="f2" />
  </rules>
</nlog>

如果您希望类型Name.Space.Class1的日志转到special文件即,其名称由记录器确定的那个),那么可以向记录器规范添加final,如下所示:

If you want the logs for type Name.Space.Class1 to go to the "special" file (i.e. the one whose name is determined by the logger), then you can add "final" to the logger specfication like this:

<logger name="Name.Space.Class1" minlevel="Trace"final="true" />

这篇关于具有不同配置的NLog日志记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆