How to check if NLog has finished logging messages from its queue in asynchronous mode?

I have an NLog configuration file as shown below. For some reason, NLog should work in asynchronous processing.

<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>

I would like to know if there is any method that I can use to check if the NLog logging protocols are complete, as shown below:

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");

Thank.

Look

+4
source share
1 answer

There is no logical value that you could check, but you can call flush- when this call is completed, logging is completed.

eg.

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");

See LogManager Ways

0
source

Source: https://habr.com/ru/post/1660786/


All Articles