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

Logging Windows 10 UWP应用程序的记录器,logging,windows-10,win-universal-app,uwp,Logging,Windows 10,Win Universal App,Uwp,我找不到任何适用于windows 10 universal应用程序的记录器,我已经试用了log4net、Microsoft企业库、Nlog,但windows 10 universal平台不支持这些记录器 P>是否有人建议我为Windows 10 UWP提供良好的记录器? < P>我知道的唯一解决方案是使用Windows中的API来创建Etw跟踪。p> 微软也提供了一个样本。你试过计量吗?您可以使用NuGet安装它: Install-Package MetroLog 下面是一个简单的例子: u

我找不到任何适用于windows 10 universal应用程序的记录器,我已经试用了log4net、Microsoft企业库、Nlog,但windows 10 universal平台不支持这些记录器


<> P>是否有人建议我为Windows 10 UWP提供良好的记录器?

< P>我知道的唯一解决方案是使用Windows中的API来创建Etw跟踪。p>
微软也提供了一个样本。

你试过计量吗?您可以使用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.");
使用计量学;
使用计量指标;
LogManagerFactory.DefaultConfiguration.AddTarget(LogLevel.Trace,LogLevel.Fatal,新文件流目标());
ILogger log=LogManagerFactory.DefaultLogManager.GetLogger();
Trace(“这是一条跟踪消息”);
您可以在上找到一个教程,说明如何将其添加到项目中。还有一个关于检索这些日志的解释

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

您可以使用其中一种,但也可以通过实现自定义接收器来选择使用自己的日志记录基础设施

为了使其更加有用,您可以使用和使用方面使日志记录变得简单

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

是ETW和LocalState之上的简单日志记录基础设施。这是快速且易于实现的,但它对ALM没有帮助,因为不支持集中式日志记录

是相对较新的,仅在Windows 10和Windows Server 2016中才可能实现。您可以在ETW和LocalStorage之间进行选择。它为您提供了关于日志外观的最低级控制,但自然也会导致实现最多的工作,并且还缺少ALM功能

有用的链接

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


```

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

在Nuget Package Manager控制台中输入以下内容:

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);
    ...
}

非常感谢@talkitbr同时
FileStreamingTarget
已经过时<建议改为使用代码>StreamingFileTarget。您可以使用应用程序洞察作为选项。我认为uwp应用程序默认带有AI。如何读取日志消息?我尝试了FileLoggingSession示例,并使用wpa.exe查看,我只能在图形浏览器中看到事件,但诊断控制台没有显示LogMessage。虽然事件跟踪是一种日志记录,但与日志记录不同。事件跟踪用于观察应用程序的执行和性能,可以根据需要随时打开和关闭应用程序。它可以快速生成大量数据,因此默认情况下是禁用的。日志记录始终处于活动状态,应用于安全性(即用户登录和注销、访问安全资源等)和稳定性(即应用程序崩溃时的例外情况)。出于安全考虑,它必须始终处于活动状态。以前使用过计量。直到我无法从设备获取日志。切换到Serilog。您使用Serilog和UWP的接收器是什么?我无法从中获得任何输出。不幸的是,控制台日志记录非常无用。我使用Serilog主要是为了写入远程日志存储(Logstash/Elasticsearch;Seq和RayGun也是很好的选择)。将来,Microsoft.Azure.Mobile将是另一种选择。理想情况下,您可以使用Serilog作为主要日志记录层,并在MetroLog或UWP日志记录之上为本地日志记录输出实现自定义接收器,以便您可以配置哪个日志记录输出应以远程日志为目标,哪个日志记录输出应以本地日志为目标。如果你不想写一个水槽。您也可以考虑SeriLog.Sunk.Snabable。以前使用Meta Logo,在使用错误中遇到FieloReXeXCEP文件相关的错误。当MyClass实例被调用时,如何在类中插入记录器?日志注册表是由IOC容器自动处理的。上面OnLaunched方法中的services.AddLogging()告诉IOC容器在实例化MyClass时为您将logger注入MyClass。NLog支持带有NLog ver的UWP。4.5
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);
    ...
}
using Microsoft.Extensions.Logging;

class MyClass
{
    readonly ILogger<MyClass> logger;

    public MyClass(ILogger<MyClass> logger)
    {
        this.logger = logger;
        logger.LogInformation("Hello from MyClass.");
    }
}
2017-12-06T17:06:33.1358005-06:00  [INF] Hello from MyClass. (25278b08)