簡體   English   中英

如何在Windows事件查看器中刪除和創建日志

[英]How to remove and create log in Windows Event Viewer

我有一個應用程序。 我正在嘗試在崩潰時寫入Windows Event Viewer日志。 我發現寫入Windows應用程序事件日志 ,我使用DispatcherUnhandledExceptionEventHandler來捕獲未處理的異常。 我在app的構造函數中設置它,如:

 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.

異常信息:ServerApp.MainWindow..ctor()的System.Exception

異常信息:System.Windows.Markup.WpfXamlLoader.Load(System.Xaml.XamlReader,System.Xaml.IXamlObjectWriterFactory,Boolean,System.Object,System.Xaml.XamlObjectWriterSettings,System.Uri)中的System.Windows.Markup.XamlParseException at at System.Windows.Markup.Markup.XamlReader.LoadBaml(System.IO.Stream)中的System.Windows.Markup.WpfXamlLoader.LoadBaml(System.Xaml.XamlReader,Boolean,System.Object,System.Xaml.Permissions.XamlAccessLevel,System.Uri) System.Windows.Application.LoadComponent(System.Uri)上的System.Windows.Application.LoadBamlStreamWithSyncInfo(System.IO.Stream,System.Windows.Markup.ParserContext)中的,System.Windows.Markup.ParserContext,System.Object,Boolean) ,System.Windows.Application.DoStartup())

如何只寫我的登錄事件查看器?

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);
    }
}

該應用程序將在哪個帳戶下運行的用戶需要具有能夠創建日志的權限。

祝好運。

我會在整個過程中嘗試捕獲,然后將異常寫入文件,請參閱:

如何在txt文件中保存異常?

您不必總是寫入文件,只是為了了解實際情況

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM