![](/img/trans.png)
[英]Iterate a JSON data, put comma after each run and SerializeObject
[英]NLogger giving inconsistent data after each run
我已經使用 NLog 來記錄我的代碼中函數的進入和退出。 但是對於同一個應用程序的不同運行,我得到了不同的日志。 它是一個多線程應用程序。 我正在使用 async 來記錄信息。
以下是我的配置:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="asynclogger" xsi:type="AsyncWrapper" overflowAction="Grow" queueLimit="100000" batchSize="5000" timeToSleepBetweenBatches="1">
<target name="logfile" xsi:type="File" fileName="D:\IALogs\${processname}_${processid}_${threadid}.ialog" layout ="${longdate} ${processname} ${processid} ${threadid} ${mdlc:item=threadid} ${level} ${message}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="asynclogger" />
</rules>
</nlog>
以下是記錄器代碼。
class Program
{
private static readonly NLog.Logger nLogger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
Thread th1 = new Thread(() => print(5000000));
th1.Start();
Thread th2 = new Thread(() => print(5000000));
th2.Start();
th1.Join();
th2.Join();
print(10000);
Console.WriteLine("Done!!!!");
Console.ReadLine();
}
private static void print(int noOfItems)
{
for (int i = 1; i <= noOfItems; i++)
{
nLogger.Info("Printing i =" + i);
}
}
}
使用 Console.Readline() 日志被完全寫入,如果沒有 Console.Readline() 日志每次都不同,並且如果沒有 Console.Readline,來自主線程的第三個 Print 方法調用不會記錄任何內容()。 如果存在 Console.Readline() 則第三個打印語句會記錄所有信息
我想你的句子“我得到不同的日志”應該翻譯成“我缺少一些日志”。
為 NLog 目標啟用異步操作時,刷新很重要,因為寫入發生在后台線程上。 在退出應用程序之前必須等待這些:
NLog.LogManager.Flush()
另請參閱: NLog 教程 - 記得刷新
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.