微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何检查NLog是否已在异步模式下从队列中记录消息?

我有一个NLog配置文件如下.由于某些原因,NLog需要进行异步处理.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  autoReload="true">
  <targets async="true">
    <target name="file" xsi:type="File" fileName="c:/log1.log" archiveEvery="Day" archiveNumbering="Rolling"/>
  </targets>
  <rules>
     <logger name="*" minlevel="Info" writeto="file"/>
  </rules>
</nlog>

我想知道是否有任何方法可以用来检查NLog是否完成了日志消息,如下所示:

for (int i = 0; i <= 9999; i++)
{
    LogManager.GetCurrentClassLogger().Info("this is for testing " + i);
}
while(!LogManager.finished())  // check if finished or not
{
    Console.Write(".");
}
Console.Write("done");

谢谢.

解决方法

没有你可以检查的布尔值,但你可以调用flush – 当调用完成后,日志记录就完成了.

例如

for (int i = 0; i <= 9999; i++)
{
    LogManager.GetCurrentClassLogger().Info("this is for testing " + i);
}
LogManager.Flush(); //flushes all async targets and block until done. There is also an optional timeout.
Console.Write("done");

LogManager methods

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐