簡體   English   中英

Windows 10 Universal Apps中的事件記錄器

[英]Event logger in Windows 10 Universal Apps

我正在嘗試為Windows Universal Application創建事件日志。 之前我們有System.Diagnostics EventLog記錄事件,但我在Windows 10 Universal Apps平台上找不到類似的東西。 是否可以為Windows 10創建日志,是否可以將這些日志寫入文件以便以后訪問?

我搜索了很多,但找不到任何東西。

FileLoggingSession

Windows 8.1 FileLoggingSessionWindows.Foundation.Diagnostics命名空間中有FileLoggingSessionLoggingChannel類,它們可以在配置為執行此操作時執行文件記錄。 您可以在官方文檔中閱讀更多內容

初始化,使用和檢索日志文件可以像在下面的代碼片段中那樣完成,當然您需要創建接口,單例等以使其可用:

// Initialization
FileLoggingSession fileLoggingSession = new FileLoggingSession("session");
var loggingChannel = new LoggingChannel("channel");
fileLoggingSession.AddLoggingChannel(loggingChannel);

// Log messages
loggingChannel.LogMessage("error message", LoggingLevel.Error);

// When file is needed
var file = await fileLoggingSession.CloseAndSaveToFileAsync();

// Do anything with file

LoggingSession

就像FileLoggingSession將日志寫入文件一樣,但主要區別在於FileLoggingSession立即將日志寫入文件,而LoggingSession則不會,並且您需要手動請求使用SaveToFileAsync方法將日志寫入文件。 從文檔:

FileLoggingSession類在記錄時將記錄的消息發送到磁盤文件。 FileLoggingSession類使用順序日志記錄,這意味着所有消息都將發送到磁盤文件,並保留消息的連續歷史記錄。 這與LoggingSession類不同,LoggingSession類根據需要將記錄的消息發送到磁盤,當出現問題並且需要分析內存消息的直接歷史記錄時會發生這種情況。

METROLOG

如果您不想使用FileLoggingSessionLoggingSession類,則還有其他選擇。 一個很好的解決方案是MetroLog ,它具有FileStreamingTarget目標,可以非常簡單地登錄Windows / Phone應用程序。

您可以在需要時創建記錄器,例如在頁面中:

public sealed partial class LogSamplePage : Win8Sample.Common.LayoutAwarePage
{
    private ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<LogSamplePage>();
}

然后你可以在頁面中使用它,如下所示:

// flat strings...
if (this.Log.IsInfoEnabled)
    this.Log.Info("I've been navigated to.");

// formatting...
if (this.Log.IsDebugEnabled)
    this.Log.Debug("I can also format {0}.", "strings");

// errors...
try
{
    this.DoMagic();
}
catch(Exception ex)
{
    if (this.Log.IsWarnEnabled)
        this.Log.Warn("You can also pass in exceptions.", ex);
}

MetroEventSource

第二個解決方案是Can Bilgin在MSDN示例庫中的日志記錄示例,其中包含MetroEventSource類。 您可以記錄消息,例如這樣的錯誤:

 MetroEventSource.Log.Error("Here is the error message");

如果使用此記錄器,請不要忘記在應用程序運行時對其進行初始化,如示例項目中所述。

暫無
暫無

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

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