使用NLog打印多行消息 [英] Print a multi-line message with NLog
问题描述
使用NLog是否可以发出多行消息,从而使每一行都根据当前布局进行格式化?例如
Is it possible with NLog to emit a multi-line message such that each line is formatted according to the current layout? E.g.
2015-12-17 11:37:38.0845 | 64 | INFO | -----------------------------------
2015-12-17 11:37:38.0845 | 64 | INFO | Statistics:
2015-12-17 11:37:38.0845 | 64 | INFO | Crawling Requests 46887 /min
2015-12-17 11:37:38.0845 | 64 | INFO | Stored Documents 9910 /min
2015-12-17 11:37:38.0845 | 64 | INFO | -----------------------------------
或者,在多线程环境中,NLog是否有可能将多个消息作为单个不中断的块发出?
Alternatively, is it possible with NLog to emit multiple messages as a single, non-interrupted block in a multithreaded environment?
推荐答案
您可以从配置中完成所有这些操作.
You can do all this from your config.
<variable name="statData" value="${longdate} | 64 | ${level} | "/>
<variable name="line" value="-----------------------------------"/>
<targets>
<target xsi:type="Console"
name="Console"
layout="
${statData}${line}${newline}
${statData}Statistics:${newline}
${statData} Crawling Requests 46887 /min ${newline}
${statData} Stored Documents 9910 /min ${newline}
${statData}${line}${newline}"/>
无法确切确定您的64岁或每分钟获取数据的位置.可能是变量或您要插入的内容.如果您要登录的文件不是控制台,这也应该起作用.
Wasn't exactly sure what your 64 was or where you were getting your per minute data. Probably a variable or something your inserting.This should also work if you are logging to a file not the console.
关于第二个问题,如果您想要来自多个线程的一条日志消息,我认为您必须在代码端执行此操作.您将必须收集线程,获取所需的日志数据,然后将其发送1次到nLog.不过我可能会误会
As for your second question, if you are wanting a single log message from multiple threads I think you would have to do that on the code side. You would have to collect your threads, get your log data you want and send it 1 time to nLog. I might be misunderstanding though
这篇关于使用NLog打印多行消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!