[英]NLog multiple file target issue
I am trying to implement NLog in WPF application. 我正在尝试在WPF应用程序中实现NLog。
We need to have two file log target, 我们需要有两个文件日志目标,
1) for debug or developer purpose which is going some "Infologs" folder 1)用于调试或开发人员目的,其中包含一些“ Infologs”文件夹
2) for error or exception purpose which is going to some "Errorlogs" folder in 2)出于错误或异常的目的,这将转到某些“ Errorlogs”文件夹中
base directory. 基本目录。 Below is my code and configuration in nlog.config for same.
下面是我的代码和在nlog.config中的配置。
<variable name="ErrorLayout" value="${longdate} | ${logger} | ${level} |
${message} ${exception:format=message,stacktrace:separator=/}" />
<variable name="InfoLayout" value="${longdate} | ${logger} | ${level} |
${message}"/>
<variable name="ErrorDir" value="${basedir}/Errorlogs/${longdate}">
</variable>
<variable name="InfoDir" value="${basedir}/Infologs/${longdate}"></variable>
<target xsi:type="File" name="fileLogException" archiveEvery="Month"
createDirs="true" lineEnding="Default" layout="${ErrorLayout}"
fileName="${ErrorDir}.log"/>
<target xsi:type="File" name="fileLogInfo" archiveEvery="Month"
createDirs="true" lineEnding="Default" layout="${InfoLayout}"
fileName="${InfoDir}.log"/>
<logger name="*" minlevel="Error" writeTo="fileLogException"/>
<logger name="*" minlevel="Info" writeTo="fileLogInfo"/>
Expected result: 预期结果:
_logger.Info("Sample informational message");//This should write to only fileLogInfo
_logger.Error("Sample error message"); //this should write to only fileLogException
_logger.Fatal("Sample fatal error message"); //this should write to only fileLogException
Current result: 当前结果:
_logger.Info() write to --> fileLogInfo
_logger.Error() & _logger.Fatal() both write to --> fileLogInfo and fileLogException
Any help please , Thanks 请任何帮助,谢谢
There are multiple approaches here. 这里有多种方法。 Option 3. is the best solution for your current case, in my opinion.
我认为,方案3是当前案例的最佳解决方案。
final
attribute final
属性 (this is the same answer as Julian's) (这与朱利安的答案相同)
<logger name="*" minlevel="Error" writeTo="fileLogException" final="true"/>
<logger name="*" minlevel="Info" writeTo="fileLogInfo"/>
Adding the attribute to fileLogException
means that when a message is written to fileLogException
, NLog will not look for any other logs afterwards. 将属性添加到
fileLogException
意味着,当将消息写入fileLogException
,NLog之后将不再查找任何其他日志。
Note that NLog evaluates loggers from top to bottom. 请注意,NLog从上到下评估记录器。
final
will only prevent lower loggers from receiving the same log message. final
只会阻止较低级别的记录器接收相同的日志消息。
This is a solution, but it can cause issues in the future. 这是一个解决方案,但将来可能会引起问题。 If you remove the
fileLogException
(or change the order of loggers), then the content of the fileLogInfo
will change (because no messages are being halted by fileLogException
anymore). 如果删除
fileLogException
(或更改记录器的顺序),则fileLogInfo
的内容将更改(因为fileLogException
不再暂停任何消息)。
When should you use this option?
您何时应使用此选项?
When you want to intercept messages and prevent them from being added to other logs.当您想拦截消息并阻止将它们添加到其他日志时。
For your current example, this seems usable at first sight.对于您当前的示例,乍一看似乎可以使用。 This is mainly because you are not separating loggers based on their name.
这主要是因为您没有根据记录器的名称来分开记录器。 But when you start considering loggers with
name
requirements and not just level requirements, eg但是,当您开始考虑具有
name
要求而不是级别要求的记录器时,例如
<logger name="MyApplication.Repositories.*" final="true" writeTo="fileLogRepository" />
<logger name="MyApplication.Importers.*" final="true" writeTo="fileLogImport" />
<logger name="*" writeTo="fileLogAllUnloggedMessages" />
it becomes more obvious when you should and shouldn't use this.
当您应该和不应该使用它时,它会变得更加明显。 In the above example, the bottom log will catch all messages except messages which have already been logged by any previous logger (which was marked as
final
, of course).在上面的示例中,底部的日志将捕获所有消息, 除了以前的任何记录器已经记录过的消息(当然,标记为
final
)。
maxLevel
attribute maxLevel
属性 Just like you've used minLevel
, there is also a maxLevel
attribute. 就像您使用
minLevel
,还有一个maxLevel
属性。
<logger name="*" minlevel="Error" writeTo="fileLogException"/>
<logger name="*" minlevel="Trace" maxLevel="Info" writeTo="fileLogInfo"/>
fileLogException
logs all messages with a level of Error
and above, whereas fileLogInfo
logs all messages with a level between Trace
and Info
(= Trace
, Debug
and Info
). fileLogException
记录所有Error
级别为Error
或更高的消息,而fileLogInfo
记录所有消息级别为Trace
和Info
(= Trace
, Debug
和Info
)。
When should you use this option?
您何时应使用此选项?
When you want to strictly define your logger to only ever include messages between a range of accepted log levels .当您想严格定义记录器时,只将接受的日志级别范围内的消息包括在内。
I changed the level values to prove a point, that it selects all messages with a level that's between the upper limit ( maxLevel
) and lower limit ( minLevel
). 我更改了级别值以证明一个观点,即它选择级别介于上限(
maxLevel
)和下限( minLevel
)之间的所有消息。
level
attribute level
属性 Your fileLogException
is only interested in one level of messages, Info
. 您的
fileLogException
只对一种消息Info
感兴趣。 Therefore, you can shorten this to 因此,您可以将其缩短为
<logger name="*" level="Info" writeTo="fileLogInfo"/>
When should you use this option?
您何时应使用此选项?
When you want to strictly define your logger to only ever include messages from a single log level .如果要严格定义记录器,使其仅包含来自单个日志级别的消息。
levels
attribute levels
属性 The levels
attribute allows you to explicitly specify multiple levels (comma-separated). levels
属性允许您显式指定多个级别(以逗号分隔)。
<logger name="*" levels="Trace,Info,Fatal" writeTo="fileLogException"/>
When should you use this option?
您何时应使用此选项?
When you want to strictly define your logger to only ever include messages from multiple log levels that cannot be described with a range .如果要严格定义记录器,使其仅包括来自多个日志级别的消息,而这些消息无法用范围来描述 。
Eg If you want to log
Info
andError
messages, and you would usemaxLevel
andminLevel
, then Nlog would also include allWarning
messages (as that falls betweenInfo
andError
in the ordered list of log levels).例如,如果您要记录
Info
和Error
消息,并且将使用maxLevel
和minLevel
,则Nlog还将包括所有Warning
消息(因为它们位于日志级别的有序列表中的Info
和Error
之间)。 However, you can excludeWarning
messages by usinglevels="Info,Error"
.但是,您可以使用
levels="Info,Error"
排除Warning
消息。
All information is based on the NLog documentation 所有信息均基于NLog文档
You need the final
attribute on the first <logger>
rule 您需要第一个
<logger>
规则的final
属性
<logger name="*" minlevel="Error" writeTo="fileLogException" final="true"/>
<logger name="*" minlevel="Info" writeTo="fileLogInfo"/>
Rules are non-final by default and so the events will be written to all loggers (from top to down), which meet the name and (min)level requirements. 默认情况下,规则是非最终规则,因此事件将被写入所有符合名称和最低级别要求的记录器(从上到下)。
For more info, see the docs 有关更多信息, 请参阅文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.