我在我的应用程序中使用 NLog 日志记录机制。我面临的问题是,当并发请求命中应用程序时,日志记录是异步完成的,我无法确定哪一行属于哪个请求。 NLog 中有没有办法将配置设置为 NLog 本身,为每个请求记录一个唯一的请求 ID?
<?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>
<!-- add your targets here -->
<target xsi:type="File" name="file" fileName="E:\IBELogs\AirAvailability\AirAvailability.log" layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Trace" writeTo="file" />
</rules>
</nlog>
最佳答案
在 NLog 4.1 中引入了 ${activityid},参见 here .我不能说 MSDN documentation对它的值(value)已经很清楚了,但到目前为止,这对我们来说是有用的,因为我们的 webAPI 应用程序中的每个请求都被分配了一个唯一的 GUID,并且它在整个请求中被保留,因为我们在几个 Nlog 操作中看到相同的值同样的请求。
关于在文本文件 NLog 中记录唯一请求 ID - 区分请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20088847/