我有一个简单的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
有人有什么想法吗?
答案 0 :(得分:2)
在这种情况下,您应该使用void Log(LogEventInfo logEvent)
方法。
_logger.Log(logEvent);
来源:https://github.com/NLog/NLog/wiki/EventContext-Layout-Renderer
但如果你想知道为什么会这样,你可以点击链接: https://github.com/NLog/NLog/commit/02e7eff1c5606ea4a4aa4dffc3114adf82791bd6