所有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'关闭按钮时,我没有在代码中执行任何特定操作,我让应用程序自行关闭。但是,此时不会显示“已完成”消息。
任何人都知道为什么?
答案 0 :(得分:0)
发生这种情况的原因是Logger在应用程序关闭时被销毁。