Windows 10 Universal Apps中的事件记录器

时间:2015-09-01 12:21:26

标签: c# windows-runtime winrt-xaml win-universal-app windows-10

我正在尝试为Windows Universal Application创建事件日志。 之前我们有System.Diagnostics EventLog来记录事件,但我在 Windows 10 Universal Apps 平台上找不到类似内容。 是否可以为 Windows 10 创建日志,是否可以将这些日志写入文件以便以后访问?

我经常搜索,但找不到任何东西。

1 个答案:

答案 0 :(得分:9)

FileLoggingSession

Since Windows 8.1 there are FileLoggingSession and LoggingChannel classes in the Windows.Foundation.Diagnostics namespace, which can perform logging to files when configured to do so. You can read more in the official documentation.

Initialization, usage and retrieving the log file can be done like in the following snippet, of course you need to create interfaces, singletons etc. to make it usable:

// 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

Just as FileLoggingSession writes logs to a file but the main difference is that FileLoggingSession writes logs immediately to the file, and LoggingSession does not, and you need to manually request writing the logs to a file with the SaveToFileAsync method. From the documentation:

The FileLoggingSession class sends logged messages to disk files as they are logged. The FileLoggingSession class uses sequential logging, which means that all messages are sent to a disk file, and a sequential history of messages is retained. This is distinct from the LoggingSession class, which sends logged messages to disk on-demand, and this happens when there's a problem and the immediate history of in-memory messages is needed for analysis.

MetroLog

You have another alternatives if you do not wan't to use FileLoggingSession or LoggingSession classes. One good solution is MetroLog which has a FileStreamingTarget target that makes it very simple to log in a Windows/Phone app.

You create the logger when you need it, for example in a page:

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

Then you can use it in the page like this:

// 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

The second solution is this logging sample on MSDN sample gallery by Can Bilgin where you have the MetroEventSource class. You can log messages for example an error like this:

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

If you use this logger don't forget to initialize it on application run, as described in the sample project.