NLog 邮件目标不适用于 BufferingWrapper\PostFilteringWrapper
NLog Mail Target not working with BufferingWrapper\PostFilteringWrapper
我想在失败的情况下通过电子邮件发送日志。但是,如果我在没有 BufferingWrapper 和 PostFilteringWrapper(目标:mailLog3)的情况下直接使用邮件目标(目标:mailLog3),这也会起作用,然后我会为每个日志条目获取一封单独的电子邮件。如果我使用 BufferingWrapper 和 PostFilteringWrapper(目标:mailLog1),这有时是第一次有效,但不是第二次。
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true" internalLogToConsole="true" internalLogLevel="Warn" internalLogFile="nlog.log">
<variable name="Error" value="Logs${shortdate}_ErrorLog.xml"/>
<targets async="true">
<!--The following will keep the default number of log messages in a buffer and write out certain levels if there is an error and other levels if there is not. Messages that appeared before the error (in code) will be included, since they are buffered.-->
<wrapper-target xsi:type="BufferingWrapper" name="smartLog">
<wrapper-target xsi:type="PostFilteringWrapper">
<!--<target-ref name="fileAsCsv"/>-->
<target xsi:type="File" fileName="${Error}"
archiveAboveSize="4194304" concurrentWrites="false" maxArchiveFiles="1" archiveNumbering="Sequence">
<layout xsi:type="Log4JXmlEventLayout">
</layout>
</target>
<!--during normal execution only log certain messages-->
<defaultFilter>level >= LogLevel.Warn</defaultFilter>
<!--if there is at least one error, log everything from trace level-->
<when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
</wrapper-target>
</wrapper-target>
<wrapper-target xsi:type="BufferingWrapper" name="mailLog1" slidingTimeout="true" bufferSize="1000" flushTimeout="-1">
<wrapper-target xsi:type="PostFilteringWrapper" name="mailLog2" defaultFilter="level = LogLevel.Fatal">
<target xsi:type="Mail" name="mailLog3"
smtpServer="xxx"
smtpPort="25"
smtpAuthentication="None"
subject="Error"
from="xxx"
to="xx"
layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}${newline}" />
<when exists="level = LogLevel.Fatal" filter="level >= LogLevel.Trace"/>
</wrapper-target>
</wrapper-target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="smartLog"/>
<logger name="*" minlevel="Trace" writeTo="mailLog1"/>
</rules>
</nlog>
</configuration>
感谢您的帮助。
我发现了问题。如果我设置:
<targets async="false">
有效 fine.The 有趣的是在其他项目中,这也适用于:
<targets async="true">
如果 BufferingWrapper 上的 slidingTimeout
设置为 true
(默认值,在本例中也是如此),则消息已被异步写入。
所以没有必要将BufferingWrapper 与async 属性或AsyncWrapper 结合起来。正如您所注意到的,将 BufferingWrapper 与异步写入相结合可能会导致消息丢失。这是因为在异步写入中,消息被写入另一个异步写入器。
我想在失败的情况下通过电子邮件发送日志。但是,如果我在没有 BufferingWrapper 和 PostFilteringWrapper(目标:mailLog3)的情况下直接使用邮件目标(目标:mailLog3),这也会起作用,然后我会为每个日志条目获取一封单独的电子邮件。如果我使用 BufferingWrapper 和 PostFilteringWrapper(目标:mailLog1),这有时是第一次有效,但不是第二次。
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
throwExceptions="true" internalLogToConsole="true" internalLogLevel="Warn" internalLogFile="nlog.log">
<variable name="Error" value="Logs${shortdate}_ErrorLog.xml"/>
<targets async="true">
<!--The following will keep the default number of log messages in a buffer and write out certain levels if there is an error and other levels if there is not. Messages that appeared before the error (in code) will be included, since they are buffered.-->
<wrapper-target xsi:type="BufferingWrapper" name="smartLog">
<wrapper-target xsi:type="PostFilteringWrapper">
<!--<target-ref name="fileAsCsv"/>-->
<target xsi:type="File" fileName="${Error}"
archiveAboveSize="4194304" concurrentWrites="false" maxArchiveFiles="1" archiveNumbering="Sequence">
<layout xsi:type="Log4JXmlEventLayout">
</layout>
</target>
<!--during normal execution only log certain messages-->
<defaultFilter>level >= LogLevel.Warn</defaultFilter>
<!--if there is at least one error, log everything from trace level-->
<when exists="level >= LogLevel.Error" filter="level >= LogLevel.Trace" />
</wrapper-target>
</wrapper-target>
<wrapper-target xsi:type="BufferingWrapper" name="mailLog1" slidingTimeout="true" bufferSize="1000" flushTimeout="-1">
<wrapper-target xsi:type="PostFilteringWrapper" name="mailLog2" defaultFilter="level = LogLevel.Fatal">
<target xsi:type="Mail" name="mailLog3"
smtpServer="xxx"
smtpPort="25"
smtpAuthentication="None"
subject="Error"
from="xxx"
to="xx"
layout="${longdate} ${uppercase:${level}} ${callsite:className=true:includeSourcePath=true:methodName=true} ${message}${newline}" />
<when exists="level = LogLevel.Fatal" filter="level >= LogLevel.Trace"/>
</wrapper-target>
</wrapper-target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="smartLog"/>
<logger name="*" minlevel="Trace" writeTo="mailLog1"/>
</rules>
</nlog>
</configuration>
感谢您的帮助。
我发现了问题。如果我设置:
<targets async="false">
有效 fine.The 有趣的是在其他项目中,这也适用于:
<targets async="true">
如果 BufferingWrapper 上的 slidingTimeout
设置为 true
(默认值,在本例中也是如此),则消息已被异步写入。
所以没有必要将BufferingWrapper 与async 属性或AsyncWrapper 结合起来。正如您所注意到的,将 BufferingWrapper 与异步写入相结合可能会导致消息丢失。这是因为在异步写入中,消息被写入另一个异步写入器。