我正在使用 NLog 进行日志记录。我正在尝试设置一种将其输出到 System.Diagnostics.Trace() 的方法,因此我将 NLog.config 文件设置如下:
<targets>
<target name="debugOutput" xsi:type="Trace" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debugOutput" />
</rules>
这适用于除 Error() 和 Fatal() 之外的所有日志记录级别。
例如,下面的代码:
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
...
_logger.Trace("Sample trace message\n");
_logger.Debug("Sample debug message\n");
_logger.Info("Sample informational message\n");
_logger.Warn("Sample warning message\n");
_logger.Error("Sample error message\n");
_logger.Fatal("Sample fatal error message\n");
当 _logger.Error() 和 _logger.Fatal() 被调用时抛出异常:
2012-09-28 12:08:04.9296|DEBUG|ClassLibraryUsingNLog.Test|Sample debug message
2012-09-28 12:08:04.9636|INFO|ClassLibraryUsingNLog.Test|Sample informational message
2012-09-28 12:08:04.9906|WARN|ClassLibraryUsingNLog.Test|Sample warning message
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
2012-09-28 12:08:05.0176|ERROR|ClassLibraryUsingNLog.Test|Sample error message
---- Assert Long Message ----
at NLog.Targets.TraceTarget.Write(LogEventInfo logEvent)
at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
at NLog.Targets.Target.WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
at NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException)
at NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
at NLog.Logger.WriteToTargets[T](LogLevel level, IFormatProvider formatProvider, T value)
at NLog.Logger.Error(String message)
at ClassLibraryUsingNLog.Test.Func() in c:\Test\CS4.x\ClassLibraryUsingNLog\Test.cs:line 15
at ConsoleAppIndirectlyUsesNLog.Program.Main(String[] args) in C:\Test\CS4.x\ConsoleAppIndirectlyUsesNLog\Program.cs:line 11
First-chance exception at 0x7686b9bc in ConsoleAppIndirectlyUsesNLog.exe: Microsoft C++ exception: EEMessageException at memory location 0x0053e8d4..
---- DEBUG ASSERTION FAILED ----
---- Assert Short Message ----
2012-09-28 12:08:06.7936|FATAL|ClassLibraryUsingNLog.Test|Sample fatal error message
---- Assert Long Message ----
at NLog.Targets.TraceTarget.Write(LogEventInfo logEvent)
at NLog.Targets.Target.Write(AsyncLogEventInfo logEvent)
at NLog.Targets.Target.WriteAsyncLogEvent(AsyncLogEventInfo logEvent)
at NLog.LoggerImpl.WriteToTargetWithFilterChain(TargetWithFilterChain targetListHead, LogEventInfo logEvent, AsyncContinuation onException)
at NLog.LoggerImpl.Write(Type loggerType, TargetWithFilterChain targets, LogEventInfo logEvent, LogFactory factory)
at NLog.Logger.WriteToTargets[T](LogLevel level, IFormatProvider formatProvider, T value)
at NLog.Logger.Fatal(String message)
at ClassLibraryUsingNLog.Test.Func() in c:\Test\CS4.x\ClassLibraryUsingNLog\Test.cs:line 16
at ConsoleAppIndirectlyUsesNLog.Program.Main(String[] args) in C:\Test\CS4.x\ConsoleAppIndirectlyUsesNLog\Program.cs:line 11
但是,如果我将配置文件更改为输出到 OutputDebugString,则通过将 xsi:type 更改为“OutputDebugString”,如下所示:
<targets>
<target name="debugOutput" xsi:type="OutputDebugString" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="debugOutput" />
</rules>
然后一切都按预期工作,没有任何例外。我在调试输出窗口中得到这个输出(在更改项目设置以支持非托管调试之后):
2012-09-28 12:16:13.4166|DEBUG|ClassLibraryUsingNLog.Test|Sample debug message
2012-09-28 12:16:13.4166|INFO|ClassLibraryUsingNLog.Test|Sample informational message
2012-09-28 12:16:13.4166|WARN|ClassLibraryUsingNLog.Test|Sample warning message
2012-09-28 12:16:13.4266|ERROR|ClassLibraryUsingNLog.Test|Sample error message
2012-09-28 12:16:13.4266|FATAL|ClassLibraryUsingNLog.Test|Sample fatal error message
有没有办法通过输出到 System.Diagnostics.Trace() 来完成这项工作,而不会引发 Logger.Error() 和 Logger.Fatal() 的异常?为什么它会这样?