如何获取Microsoft.Extensions.Logging< T>在使用Serilog和AutoFac的控制台应用程序中? [英] How to get Microsoft.Extensions.Logging<T> in console application using Serilog and AutoFac?

查看:333
本文介绍了如何获取Microsoft.Extensions.Logging< T>在使用Serilog和AutoFac的控制台应用程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET Core应用程序中,我们有常见的BL类,它们都包含在ctor中:

We have common BL classes in a ASP.NET Core application that get in the ctor:

Microsoft.Extensions.Logging.ILogger< Foo>

在ASP.NET Core中,ASP.NET的内部基础结构通过 LoggerFactory处理获取ILogger。

In ASP.NET Core, the internal infrastructure of ASP.NET handles getting the ILogger via the LoggerFactory.

我们现在想在控制台应用程序中重用这些BL类(用于异步作业),我们如何设置 AutoFac 和Serilog注入 LoggerFactory 不会的环境中的 Microsoft.Extensions.Logging.ILogger< T> 是否存在?

We now want to reuse these BL classes in a console application (for async jobs), how do we setup AutoFac and Serilog to inject Microsoft.Extensions.Logging.ILogger<T> in environment that LoggerFactory doesn't exists?

推荐答案

Microsoft.Extensions.Logging (请参阅)不属于ASP.NET Core可以独立运行。您需要做的就是注册 ILoggerFactory ILogger<> 接口。

Microsoft.Extensions.Logging (see source) is not part of ASP.NET Core and can run independently of it. All you need to do is to register the ILoggerFactory and ILogger<> interface.

ILoggerFactory Logger< T> >实例化实际的记录器。

The ILoggerFactory is used by Logger<T> to instantiate the actual logger.

在控制台应用程序中使用Logging扩展时,建议仍使用 IServiceCollection ,因为这样做可以使您使用 IServiceCollection 扩展方法以注册所有支持此模式的软件包。

When using the Logging extension in console applications, its recommended still to use the IServiceCollection, as this allows you to use the IServiceCollection extension methods to register all packages which support this pattern.

var services = new ServiceCollection();
services.AddLogging();

// Initialize Autofac
var builder = new ContainerBuilder();
// Use the Populate method to register services which were registered
// to IServiceCollection
builder.Populate(services);

// Build the final container
IContainer container = builder.Build();

这是推荐的方法,因为您不必详细考虑需要注册哪些课程适用于具有 Microsoft.Extensions.DependencyInjection 集成支持的库。

This is the recommended approach, as you won't have to think in detail which classes need to registered for libraries which have Microsoft.Extensions.DependencyInjection integration support.

但是您当然也可以手动注册,但是当 Microsoft.Extensions.Logging 发生更改时库(添加了新的依赖项),您将无法获得它,并且首先必须弄清楚或挖掘插入源代码以查找错误。

But of course you can also register it manually, but when a change happens to the Microsoft.Extensions.Logging library (new dependency added), you won't get it and first have to figure out or dig into the source code to find the error.

builder.RegisterType<LoggerFactory>()
    .As<ILoggerFactory>()
    .SingleInstance();
builder.RegisterGeneric(typeof(Logger<>))
    .As(typeof(ILogger<>))
    .SingleInstance();

剩下的就是在构建容器之后或在应用程序启动之前注册记录器类型:

All that remains is to register the logger types after the container has been built or before your application starts:

var loggerFactory = container.Resolve<ILoggerFactory>();
loggerFactory.AddConsole()
    .AddSerilog();

,然后在您的服务中注入 ILogger< MyService> 照常。

and in your services inject ILogger<MyService> as usual.

这篇关于如何获取Microsoft.Extensions.Logging&lt; T&gt;在使用Serilog和AutoFac的控制台应用程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆