Event logger in Windows 10 Universal Apps
Asked Answered
A

1

10

I am trying to create an event log for a Windows Universal Application. Earlier we had System.Diagnostics EventLog to log events, but I could not find anything similar on the Windows 10 Universal Apps platform. Is it possible to create logs for Windows 10 and can these logs be written to a file for accessing it later?

I searched a lot, but could not find anything.

Aquamanile answered 1/9, 2015 at 12:21 Comment(0)
C
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.

Chopin answered 1/9, 2015 at 21:23 Comment(2)
I have gone through the documentation of FileLoggingSession and it says that FileLoggingSession logs immediately to the HardDisk. However I don't see any sample for the same. It seems that we have to create a logging channel, add it to FileLoggingSession, log using this log channel. Finally when we call fileLoggingSession.closeAndSavetoFileAsync(), It writes the logs to File. The Question is How to write logs immediately to hardisk using FileLoggingSession ? Because I may not be able to call closeAndSaveToFileAsync() if my App crashes before that. Kindly Help. Thanks!Psoriasis
@Kristian: can we use any one of the solutions you mention for logging into the Windows Event Viewer, as with System.Diagnostics EventLog?Nymphet

© 2022 - 2024 — McMap. All rights reserved.