如何在Windows Event Viewer中删除和创建日志 [英] How to remove and create log in Windows Event Viewer
问题描述
我有一个应用.我试图在Windows Event Viewer
崩溃时写日志.我发现写入Windows应用程序事件日志,并且我正在使用DispatcherUnhandledExceptionEventHandler
捕获未处理的异常.我在应用的构造函数中设置它,例如:
I have an app. I'm trying to write log in Windows Event Viewer
when its crashing. I found Write to Windows Application Event Log and I'm using DispatcherUnhandledExceptionEventHandler
for catching unhandled exception. I'm setting it in constructor of app like:
DispatcherUnhandledException += MyApplication_DispatcherUnhandledException;
并这样写日志:
using (EventLog eventLog = new EventLog("Application"))
{
eventLog.Source = "Application";
eventLog.WriteEntry(exceptionMessage, EventLogEntryType.Error);
}
创建日志,但在System.Windows.Application
的Run
方法中发生另一个异常,并且Windows在事件查看器中使用另一个ID,源代码添加了此错误.
Log creates, but in Run
method of System.Windows.Application
occurs another exception and windows adds this error in Event Viewer with another Id, source....
Description: The process was terminated due to an unhandled exception.
异常信息:System.Exception 在ServerApp.MainWindow..ctor()
Exception Info: System.Exception at ServerApp.MainWindow..ctor()
异常信息:System.Windows.Markup.XamlParseException 在System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader,System.Xaml.IXamlObjectWriterFactory,布尔值,System.Object,System.Xaml.XamlObjectWriterSettings,System.Uri)中 在System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader,布尔值,System.Object,System.Xaml.Permissions.XamlAccessLevel,System.Uri) 在System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream,System.Windows.Markup.ParserContext,System.Object,布尔值) 在System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream,System.Windows.Markup.ParserContext) 在System.Windows.Application.LoadComponent(System.Uri,布尔值) 在System.Windows.Application.DoStartup()
Exception Info: System.Windows.Markup.XamlParseException at System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader, System.Xaml.IXamlObjectWriterFactory, Boolean, System.Object, System.Xaml.XamlObjectWriterSettings, System.Uri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader, Boolean, System.Object, System.Xaml.Permissions.XamlAccessLevel, System.Uri) at System.Windows.Markup.XamlReader.LoadBaml(System.IO.Stream, System.Windows.Markup.ParserContext, System.Object, Boolean) at System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream, System.Windows.Markup.ParserContext) at System.Windows.Application.LoadComponent(System.Uri, Boolean) at System.Windows.Application.DoStartup()
如何只写我的登录事件查看器?
How can I write only my log in event viewer?
推荐答案
using System;
using System.Diagnostics;
...
...
public void WriteToEventLog(EventLogEntryType eventLogType, string message, string logSourceName)
{
if (!EventLog.SourceExists(logSourceName))
{
EventLog.CreateEventSource(logSourceName, "Application");
}
using (var eventLog = new EventLog { Source = logSourceName })
{
const int maxLength = 31000;
if (message.Length > maxLength)
{
message = message.Substring(0, maxLength);
}
eventLog.WriteEntry(message, eventLogType);
}
}
要使用此应用程序运行帐户的用户必须具有访问权限才能创建日志.
The user, under which account this app is going to run, needs to have access to be able to create logs.
祝你好运.
这篇关于如何在Windows Event Viewer中删除和创建日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!