Windows 10 个 UWP 应用程序的记录器

Logger for Windows 10 UWP app

我找不到 windows 10 通用应用程序的任何记录器,我尝试了 log4net、Microsoft 企业库、Nlog,但 none windows 10 支持它们通用平台。

任何人都可以向我推荐 windows 10 UWP 的好记录器吗?

我知道的唯一解决方案是使用 windows.foundation.diagnostics 命名空间中的 API 进行 ETW 跟踪。

Microsoft 提供了示例 here

你试过MetroLog了吗?您可以使用 NuGet 安装它:

Install-Package MetroLog

这是一个简单的例子:

using MetroLog;
using MetroLog.Targets;

LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace, LogLevel.Fatal, new FileStreamingTarget());

ILogger log = LogManagerFactory.DefaultLogManager.GetLogger<MainPage>();

log.Trace("This is a trace message.");

您可以在 http://talkitbr.com/2015/06/11/adicionando-logs-em-universal-apps 找到解释如何将其添加到项目中的教程。还有关于检索这些日志的解释。

Serilog

一种可能性是使用 Serilog 作为中央日志记录接口并将其配置为具有与 UWP 一起使用的接收器。

您可以使用众多 Sinks you can choose from 之一,但您也可以通过实施自定义接收器来选择使用您自己的日志记录基础结构。

为了使其更有用,您可以使用 Anotar.Serilog.Fody to use the Fody Code Weaver 并使用 Aspects 使您的日志记录变得微不足道。

通常,您希望将中央日志记录提供程序用于应用程序生命周期管理 (ALM),并使用本地接收器(文件)作为致命错误的回退。

MetroLog

MetroLog 是一个基于 ETW 和 LocalState 的简单日志记录基础设施。这实现起来既快又容易,但它对 ALM 没有帮助,因为不支持集中式日志记录。

UWP Logging

Universal Windows Plattform Logging 相对较新,仅适用于 Windows 10 和 Windows Server 2016。您可以在 ETW 和 LocalStorage 之间进行选择。它为您提供了对日志外观的最底层控制,但自然也会导致大部分工作需要实施,并且还缺乏 ALM 功能。

有用的链接

您可以在 UWP 中使用标准的 Serilog 滚动文件接收器,您只需要告诉记录器记录到哪里。这是设置它的一些代码(我正在使用 Autofac);

private static void RegisterLogger(ContainerBuilder builder)
{
    const string fileOutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff} [{Level}] {Message}{NewLine}{Exception}";
    var logPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "Logs", "MyAppName-{Date}.log");

    var logConfiguration = new LoggerConfiguration()
                            .MinimumLevel.Verbose()
                            .WriteTo.RollingFile(logPath, outputTemplate: fileOutputTemplate);

    Log.Logger = logConfiguration.CreateLogger();

    builder.RegisterLogger();
}

```

如果您想记录到文件并利用 Microsoft 的轻量级 DI 框架,您可以使用 Serilog。

在 Nuget 包管理器控制台中输入以下内容:

Install-Package Microsoft.Extensions.DependencyInjection
Install-Package Microsoft.Extensions.Logging
Install-Package Serilog.Extensions.Logging.File

在组合根设置依赖注入容器。在我们的例子中,它是 App.xaml.cs 文件中的 OnLaunched 方法。

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Windows.Storage;
...

protected override void OnLaunched(LaunchActivatedEventArgs e)
{
     ...
    // Create IOC container and add logging feature to it.
    IServiceCollection services = new ServiceCollection();
    services.AddLogging();

    // Build provider to access the logging service.
    IServiceProvider provider = services.BuildServiceProvider();

    // UWP is very restrictive of where you can save files on the disk.
    // The preferred place to do that is app's local folder.
    StorageFolder folder = ApplicationData.Current.LocalFolder;
    string fullPath = $"{folder.Path}\Logs\App.log";

    // Tell the logging service to use Serilog.File extension.
    provider.GetService<ILoggerFactory>().AddFile(fullPath);
    ...
}

设置后,日志记录非常简单。只需将记录器注入您的 class.

using Microsoft.Extensions.Logging;

class MyClass
{
    readonly ILogger<MyClass> logger;

    public MyClass(ILogger<MyClass> logger)
    {
        this.logger = logger;
        logger.LogInformation("Hello from MyClass.");
    }
}

您的日志文件应创建于:

C:\Users\yourUserName\AppData\Local\Packages\f15fdadf-faae-4f4a-8445-5feb195ff692_68newbw0j54vy\LocalState\Logs\App-20171206.log,

其中 f15fdadf-faae-4f4a-8445-5feb195ff692_68newbw0j54vy 是我解决方案中 Package.appxmanifest 文件中的包名称。

请注意,AppData 文件夹默认隐藏在文件资源管理器中。

这是它的内容:

2017-12-06T17:06:33.1358005-06:00  [INF] Hello from MyClass. (25278b08)