我有一个应用程序。当它崩溃时,我想要将日志写入 Windows事件查看器
。我找到了写入Windows应用程序事件日志,并使用DispatcherUnhandledExceptionEventHandler
来捕获未处理的异常。我在应用程序的构造函数中设置它,如下所示:
DispatcherUnhandledException += MyApplication_DispatcherUnhandledException;
而且要像这样记录日志:
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry(exceptionMessage, EventLogEntryType.Error);
}
日志已经生成,但在System.Windows.Application
的Run
方法中发生了另一个异常,Windows会将此错误与其他ID、源一起记录在事件查看器中...
Description: The process was terminated due to an unhandled exception.
异常信息: System.Exception 在 ServerApp.MainWindow..ctor() 中发生
异常信息: System.Windows.Markup.XamlParseException 在 System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) 中发生 在 System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) 中发生 在 System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean) 中发生 在 System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) 中发生 在 System.Windows.Application.LoadComponent(System.Uri, Boolean) 中发生 在 System.Windows.Application.DoStartup() 中发生
如何在事件查看器中只记录我的日志?
using
代码块注释掉。在我看来,你有一个未处理的异常。 - spodger