我能确定的最佳答案和解决方案是监视Application Error
WindowsApplication Hang
事件查看器应用程序日志中的事件。
从 .NET 3.5 开始,实现了一个方便的类以避免读取和过滤整个事件日志:EventLogWatcher允许监视特定事件。
这是一个非常基本的示例,按 过滤EventID
并Level
使用ApplicationName
XPath 查询:
using System.Globalization;
using System.Diagnostics.Eventing.Reader;
EventLogQuery filter = new EventLogQuery("Application", PathType.LogName, "Event[System[Level=2 and (EventID = 1000 or EventID = 1002)] and EventData[Data[1] = \"example.exe\"]]")
EventLogWatcher watcher = new EventLogWatcher(filter);
watcher.EventRecordWritten += Watcher_ApplicationError; // Register our handler
watcher.Enabled = true; // Start delivering events to the handler
private void Watcher_ApplicationError(object sender, EventRecordWrittenEventArgs e)
{
String rawId = e.EventRecord.Properties[8].Value.ToString(); // Faulting process id
Int32 id = -1;
if (!Int32.TryParse(rawId, out id)) // If not integer, possibly hexadecimal
{
if (!Int32.TryParse(rawId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out id)
return; // Unable to read the process id successfully
}
Process unresponsive = Process.GetProcessById(id); // Get the unresponsive process
unresponsive.Kill(); // Kill it
}
这可以很容易地扩展,以通过完全限定的、错误的应用程序执行路径进行过滤Properties[10]
。