Wpf Nlog不';t在应用程序关闭时打印消息

Wpf Nlog不';t在应用程序关闭时打印消息,wpf,.net-4.0,nlog,Wpf,.net 4.0,Nlog,所有ViewModel类都继承自基类: public abstract class ScreenBase : ViewModelBase, IScreen, IDisposable { protected readonly NLog.Logger _logger; protected ScreenBase() : this(Messenger.Default) { } protected ScreenBase(IMessenger messenger)

所有ViewModel类都继承自基类:

public abstract class ScreenBase : ViewModelBase, IScreen, IDisposable
{
  protected readonly NLog.Logger _logger;
  protected ScreenBase()
        : this(Messenger.Default) { }

  protected ScreenBase(IMessenger messenger)
        : base(messenger)
  {
    _logger = NLog.LogManager.GetLogger(this.GetType().Name);
    _logger.Debug("{0} ({1}) constructed. ", this.GetType().Name, this.GetHashCode());
  }

  ~ScreenBase()
  {
    FinalizeProc();
  }

  [Conditional("DEBUG")]
  private void FinalizeProc()
  {
    _logger.Debug("{0} ({1}) Finalized. ", this.GetType().Name, this.GetHashCode());
  }
}
如您所见,任何时候创建/销毁ViewModel实例时,都应该将其记录下来。我正在将其登录到控制台窗口和文件:

<targets>
  <!-- add your targets here -->
  <target name="logDebugInfo" xsi:type="File" deleteOldFileOnStartup="true" fileName="${specialfolder:folder=CommonApplicationData}/MyApp/debug.txt" layout="${longdate} | ${uppercase:${level}} | ${stacktrace} | ${message}${onexception:EXCEPTION OCCURRED\:${exception:format=tostring}}" />
  <target name="console" xsi:type="Console" />
</targets>

<rules>
  <!-- add your logging rules here -->
  <logger name="*" minlevel="Trace" maxlevel="Info" writeTo="logDebugInfo" />
  <logger name="*" minlevel="Trace" writeTo="console" />   
</rules>

在关闭应用程序之前,会正确记录每条消息。当按下主视图上的“X”关闭按钮时,我不会在代码中执行任何特定操作,而是让应用程序自行关闭。但是,此时不会显示“已完成”的消息


有人知道原因吗?

发生这种情况的原因是,在应用程序关闭时,记录器正在被销毁。

可能的副本不是重复的,终结器被调用,_Logger.Debug(…)被执行,但文件/控制台中的输出未被注册。可能与本文中提供的答案有关问题类似,但你对这个话题的评论让我得出结论,为什么会发生这种情况。启用InternalLogger后,我注意到Logger在应用程序关闭时被销毁,因此它不记录日志的原因是。如果您想发布您的答案,只需提及有关启用内部记录器的内容,或者其他内容,我将接受。