NLog例外:收集被修改;枚举操作可能无法执行

时间:2012-11-22 18:41:18

标签: c# nlog

我正在使用NLog。当我尝试初始化我的记录器对象时:

private static Logger _logger = LogManager.GetCurrentClassLogger();

我明白了:

    2012-11-22 10:15:33.8479|Error|MyClass(6468:13)|MyNamespace.BaseThreadLoop|Exception processing event in RequestLoop: System.TypeInitializationException: The type initializer for 'MyClass' threw an exception. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at NLog.Internal.EnumerableHelpers.<OfType>d__0`1.MoveNext() in c:\NLogBuild\src\NLog\Internal\EnumerableHelpers.cs:line 60
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at NLog.Internal.EnumerableHelpers.ToList[T](IEnumerable`1 enumerable) in c:\NLogBuild\src\NLog\Internal\EnumerableHelpers.cs:line 118
   at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, Dictionary`2 visitedObjects) in c:\NLogBuild\src\NLog\Internal\ObjectGraphScanner.cs:line 118
   at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, Dictionary`2 visitedObjects) in c:\NLogBuild\src\NLog\Internal\ObjectGraphScanner.cs:line 125
   at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, Dictionary`2 visitedObjects) in c:\NLogBuild\src\NLog\Internal\ObjectGraphScanner.cs:line 125
   at NLog.Internal.ObjectGraphScanner.ScanProperties[T](List`1 result, Object o, Int32 level, Dictionary`2 visitedObjects) in c:\NLogBuild\src\NLog\Internal\ObjectGraphScanner.cs:line 125
   at NLog.Internal.ObjectGraphScanner.FindReachableObjects[T](Object[] rootObjects) in c:\NLogBuild\src\NLog\Internal\ObjectGraphScanner.cs:line 65
   at NLog.Internal.TargetWithFilterChain.PrecalculateStackTraceUsage() in c:\NLogBuild\src\NLog\Internal\TargetWithFilterChain.cs:line 95
   at NLog.LogFactory.GetTargetsByLevelForLogger(String name, IList`1 rules, TargetWithFilterChain[] targetsByLevel, TargetWithFilterChain[] lastTargetsByLevel) in c:\NLogBuild\src\NLog\LogFactory.cs:
line 587
   at NLog.LogFactory.GetTargetsByLevelForLogger(String name, IList`1 rules, TargetWithFilterChain[] targetsByLevel, TargetWithFilterChain[] lastTargetsByLevel) in c:\NLogBuild\src\NLog\LogFactory.cs:
line 574
   at NLog.LogFactory.GetConfigurationForLogger(String name, LoggingConfiguration configuration) in c:\NLogBuild\src\NLog\LogFactory.cs:line 599
   at NLog.LogFactory.GetLogger(LoggerCacheKey cacheKey) in c:\NLogBuild\src\NLog\LogFactory.cs:line 717
   at NLog.LogFactory.GetLogger(String name) in c:\NLogBuild\src\NLog\LogFactory.cs:line 330
   at NLog.LogManager.GetCurrentClassLogger() in c:\NLogBuild\src\NLog\LogManager.cs:line 143
   at MyNamespace.MyOtherClass..cctor() in C:\....cs:line 25
   --- End of inner exception stack trace ---
   at MyNamespace.MyClass.get_Instance()
   ...

我按照http://nlog-forum.1685105.n2.nabble.com/InvalidOperationException-when-accessing-the-configuration-from-multiple-threads-td6723806.html的建议将我的记忆目标移到了一个单独的班级但仍有问题。任何的想法?

1 个答案:

答案 0 :(得分:1)

我想您可能正在使用旧版本的NLog。你能检查一下你所依赖的NLog文件src/NLog/Internal/ObjectGraphScanner.cs(或者用.NET反射器等工具查看程序集)吗?

它应该像在这个错误修复中以绿色显示的内容:

BUG 6015: InvalidOperationException (Collection was modified) in call…