c# - nlog 事件上下文自定义变量日志错误

标签 c# nlog

我对 Nlog 有一个简单的设置,当仅传递消息等普通参数时,它工作正常,但是当我传递如下所示的 LogEventInfo 对象时:

//inside a class
private static Logger _logger = LogManager.GetLogger("MyLogs");

//in a method
LogEventInfo logEvent = new LogEventInfo(LogLevel.Info, "LogName", "some message");
logEvent.Properties.Add("httpMethod", "GET");
_logger.Warn(logEvent);

我收到以下错误:

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) +52
System.Collections.Generic.Enumerator.MoveNext() +10900893
NLog.Targets.Target.MergeEventProperties(LogEventInfo logEvent) +312
NLog.Targets.Target.Write(AsyncLogEventInfo logEvent) +74

[NLogRuntimeException: Exception occurred in NLog]
NLog.<>c__DisplayClass1.<Write>b__0(Exception ex) +150
NLog.Internal.SingleCallContinuation.Function(Exception exception) +153
NLog.Targets.Target.Write(AsyncLogEventInfo logEvent) +189
NLog.Targets.Target.WriteAsyncLogEvent(AsyncLogEventInfo logEvent) +364
NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException) +341
NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory) +351
NLog.Logger.WriteToTargets(LogLevel level, IFormatProvider formatProvider, T value) +136
NLog.Logger.Warn(T value) +106
proj.Infrastructure.Logging.LogResponse(String message, IOwinContext context, String flow, String action) in c:\Sites\proj\Infrastructure\Logging.cs:45
proj.<Validate>d__0.MoveNext() in c:\Sites\proj\myclass.cs:40
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
Microsoft.Owin.Security.OAuth.<InvokeTokenEndpointAsync>d__1e.MoveNext() +1049
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
Microsoft.Owin.Security.OAuth.<InvokeAsync>d__0.MoveNext() +1735
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
Microsoft.Owin.Security.Infrastructure.<Invoke>d__0.MoveNext() +664
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +22
Microsoft.Owin.Host.SystemWeb.Infrastructure.ErrorState.Rethrow() +33
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +150
Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.EndFinalWork(IAsyncResult ar) +42
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +415
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

这是nlog配置文件:

<?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"
       internalLogFile="c:\log.txt" 
       internalLogLevel="Trace"
       throwExceptions="true">
   <targets>
     <target xsi:type="File" name="textFile"
          fileName="${basedir}/logs/${shortdate}/file.txt" layout="${message} ${event-context:item=httpMethod}"/>
   </targets>
   <rules>
     <logger name="*" minlevel="Trace" writeTo="textFile" />
   </rules>
</nlog>

我进行了搜索,但找不到任何相关内容。我使用的nlog版本是:3.1.0.0

大家有什么想法吗?

最佳答案

在这种情况下,您应该使用 void Log(LogEventInfo logEvent) 方法。

_logger.Log(logEvent);

来源:https://github.com/NLog/NLog/wiki/EventContext-Layout-Renderer

但是如果您想知道为什么会发生这种情况,您可以点击链接: https://github.com/NLog/NLog/commit/02e7eff1c5606ea4a4aa4dffc3114adf82791bd6

关于c# - nlog 事件上下文自定义变量日志错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25347846/

相关文章:

c# - 将值传递给 NLog 自定义 LayoutRenderer

c# - 日志级别 : Would you consider DEBUG finer than TRACE?

c# - 查询azure表服务给出不同的结果

c# - 如何在十亿像素位图上实现平移/缩放?

c# - 如何在安装应用程序的文件夹内创建文件夹和文件? C#

c# - WCF - 想知道请求队列

c# - "DATATABLE"的数据表过滤方法

c# - 使用 NLog 登录多个文件

asp.net-mvc - NLog 在所有 aspnet 布局渲染器上抛出配置异常

Nlog:当 Loglevel >= Loglevel.Error 时发送邮件,最后 x 条消息