1

我有一个使用 Prism 的 xamarin.forms 应用程序。但是我需要向它添加几个 microsoft.extensions.logging ILoggers。在用于日志记录的标准 microsoft.extensions.dependencyinjection 设置中,我通常会这样做

serviceCollection.AddLogging(builder =>
                        {
                                builder.AddDebug();
                                builder.AddConsole();

                                builder.AddApplicationInsights(someKey);
                        });

我将如何使用 Prism 做到这一点?我还能获得打字的 ILogger 吗?例如

public ThingieService(ILogger<ThingieService> logger)

4

1 回答 1

1

有一个很酷的小插件Prism.Microsoft.DependencyInjection.Extensions,我觉得它对此很有用。

在您的方法中,RegisterTypes您可以使用Microsoft.Extensions.DependencyInjection. IServiceCollection您可以在Microsoft.Extensions.Logging ILogger此处添加对的支持。例如:

public partial class App : PrismApplication
{
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterServices(s =>
        {
            s.AddLogging(logging =>
            {
                logging.AddSimpleConsole();
            });
        });
    }
}

这也适用于其他IServiceCollection扩展,例如AddHttpClient()or ConfigureOptions()

于 2021-03-24T04:57:25.923 回答