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

C# nlog事件上下文自定义变量日志错误,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.P

我使用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
[InvalidOperationException:集合已修改;枚举操作可能无法执行。]
System.ThrowHelper.ThrowInvalidOperationException(异常资源)+52
System.Collections.Generic.Enumerator.MoveNext()+10900893
NLog.Targets.Target.MergeEventProperties(LogEventInfo logEvent)+312
NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)+74
[NLogRuntimeException:NLog中发生异常]
NLog.c_uuu显示器Class1.b_uuu0(例外情况除外)+150
NLog.Internal.SingleCallContinuation.Function(异常)+153
NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)+189
NLog.Targets.Target.WriteAsynClogeEvent(AsyncLogEventInfo logEvent)+364
NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead,LogEventInfo logEvent,AsyncContinuation OneException)+341
NLog.LoggerImpl.Write(类型loggerType,TargetWithFilterChain目标,LogEventInfo logEvent,LogFactory工厂)+351
NLog.Logger.WriteToTargets(日志级别,IFormatProvider formatProvider,T值)+136
NLog.Logger.Warn(T值)+106
c:\Sites\proj\Infrastructure\Logging.cs:45中的proj.Infrastructure.Logging.LogResponse(字符串消息、IOwinContext上下文、字符串流、字符串操作)
c:\Sites\proj\myclass.cs中的proj.d\uu 0.MoveNext():40
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.Runtime.CompilerServices.TaskWaiter.GetResult()+21
Microsoft.Owin.Security.OAuth.d_u1E.MoveNext()+1049
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.Runtime.CompilerServices.TaskWaiter.GetResult()+21
Microsoft.Owin.Security.OAuth.d_u0.MoveNext()+1735
System.Runtime.CompilerServices.TaskWaiter.ThrowForNonSuccess(任务任务)+93
System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务)+52
System.Runtime.CompilerServices.TaskWaiter`1.GetResult()+24
Microsoft.Owin.Security.Infrastructure.d_u0.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步骤,布尔值&同步完成)+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);
资料来源:

但如果您想知道为什么会发生这种情况,可以通过链接:

是否每次尝试运行代码时都会发生此错误,并且
\u logger.Warn(logEvent)名为?@Pure.Krome Yeh是的。可能有拼写错误吗?您的布局有以下内容:
${event context:item=httpmethod}
。看起来你有一个额外的
m
@BrendanGreen,这只是在stackoverflow上格式化代码时的一个输入错误。这为我指明了正确的方向,奇怪的是你不能调用_logger.Warn/Info等,并且必须显式使用void Log,我认为以前的方法在日志级别方面会优先考虑