Allow UWP apps to output logs using Serilog

2 minute read

background

I tried to use NLog to allow the UWP app to output logs, but I couldn’t.
After investigating various things, the following came up as candidates.

MetroLog decided to use Serilog because it decided that the update history had stopped a few years ago and it seemed unusable.
Also, since there was almost no information in Japanese, I will leave it as an article.
(There are few people who are developing UWP apps.)

What is Serilog

According to the official website

Like many other libraries for .NET, Serilog provides diagnostic logging to files, consoles, and other locations. It’s easy to set up, has a clean API, and is portable across modern .NET platforms.

Unlike other logging libraries, Serilog is built with strong structured event data in mind.

… apparently …
The platform supports NET 4.5+, Windows (8 / WinRT / Universal +) and Windows Phone 8+.

Installation method

All you need is

  • Serilog
  • Serilog.Sinks.File

Will be.

Execute the following command in [Tools]> [NuGet Package Manager]> [Package Manager Console], or

PM> Install-Package Serilog
PM> Install-Package Serilog.Sinks.File

In Solution Explorer, right-click Browse, select Manage NuGet Packages, search for “Serilog” and install the latest version.

Setting method

Make the settings using the LoggerConfiguration class as shown below.

string logFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "logs/log.txt");
Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day)
    .CreateLogger();

MinimumLevel indicates the minimum level of log output, and WriteTo.File indicates the destination file.
ʻApplicationData.Current.LocalFolder.Path shows the local store, creates a logs folder in it, and logs are saved under it. Also, rollingInterval indicates the file division unit, and RollingInterval.Day` is the setting to divide the log file on a daily basis.
By the way, the file size is 1GB by default, and the number of files is saved up to the latest 31 files (for one month).
(It is possible to change the settings of file size and number of files, but I will omit it this time.)

With the above settings, the log file will be saved in c: \ users \ <user name> \ AppData \ Local \ Packages \ <app package identifier> \ LocalState \ logs as shown below.

log20180631.txt
log20180701.txt
log20180702.txt

It seems that various other settings can be made, so I would like to check it again if necessary.

how to use

Functions are provided for each log level, and you simply pass the string to the log level function you want to output.
I think this is similar to other logger libraries.

Log.Information("Output at Information level");
Log.Debug("Output at Debug level");
Log.Warning("Output at Warning level");
Log.Error("Output at Error level");

Convenient usage

As an example, it will be much easier if you create the following LogUtil class (wrapper class).
Please try to reference.

LogUtil.cs


public static class LogUtil
{
    private static bool isInitialized = false;
    public static void Information(string message)
    {
        if (!isInitialized)
        {
            Initialize();
        }
        Log.Information(message);
    }
    public static void Debug(string message)
    {
        if (!isInitialized)
        {
            Initialize();
        }
        Log.Debug(message);
    }
    public static void Warning(string message)
    {
        if (!isInitialized)
        {
            Initialize();
        }
        Log.Warning(message);
    }
    public static void Error(string message)
    {
        if (!isInitialized)
        {
            Initialize();
        }
        Log.Error(message);
    }
    private static void Initialize()
    {
        string logFilePath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "logs/log.txt");
        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.File(logFilePath, rollingInterval: RollingInterval.Day)
            .CreateLogger();
        isInitialized = true;
        Log.Debug("Initialized Serilog");
    }
}

Reference URL