簡體   English   中英

nlog事件上下文自定義變量日志錯誤

[英]nlog event context custom variables log error

我使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM