在不使用 WebHost 的情况下使用 ASP.NET Core 日志记录 [英] Use ASP.NET Core logging without using WebHost

查看:25
本文介绍了在不使用 WebHost 的情况下使用 ASP.NET Core 日志记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 .NET Core 应用程序可以从 ASP.NET Core 日志记录(和服务提供程序)中受益,但它们没有使用任何 WebHostWebHostBuilder.

我想深入了解诸如 ConfigureLoggingUseStartup 之类的扩展/方法,以获取有关如何设置我的根配置/服务提供程序的一些灵感.

我认为 ASP.NET Core 是开源的,但不知何故我缺乏找到这些源代码的才能.我在哪里可以详细了解所有这些是如何设置的,以及如何在任何控制台应用程序中使用它们,而不仅仅是使用 WebHostBuilder 的应用程序?

解决方案

来自 ASP.NET Core 的各种组件都可以单独使用.所以你绝对可以使用 Microsoft.Extensions.Logging 之类的东西,甚至其他东西像 DependencyInjectionConfiguration选项单独使用,而不必实际使用 ASP.NET Core 或其 WebHost.

我现在可以将您指向源中的各个位置(例如 WebHostBuilder, WebHost默认构建器),但在您自己的应用程序中登录和运行实际上非常简单,所以你不需要浏览所有这些.

相反,只需添加对 Microsoft.Extensions.Logging 和您最喜欢的日志记录提供程序,例如Microsoft.Extensions.Logging.Console,然后设置一个记录器工厂并创建一个记录器:

var loggerFactory = new LoggerFactory();loggerFactory.AddConsole();var logger = loggerFactory.CreateLogger("categoryName");logger.LogInformation("哇!");

这就是让日志正常工作所需的全部内容.当然,您现在需要创建自己的记录器并将其传递到您需要的地方,但您可以完全控制它.

如果您愿意,还可以通过添加对 的引用来添加依赖项注入.Microsoft.Extensions.DependencyInjection 并创建服务集合:

var services = new ServiceCollection();//添加日志services.AddLogging(loggerBuilder =>{loggerBuilder.AddConsole();});//添加你的服务services.AddTransient();//创建服务提供者var serviceProvider = services.BuildServiceProvider();//请求你的服务并做一些事情var service = serviceProvider.GetService();服务.DoSomething();

公共类MyService{私有只读 ILogger_记录器;公共我的服务(ILogger logger){_logger = 记录器;}公共无效 DoSomething(){_logger.LogInformation("做了些什么!");}}

突然之间,您只需登录几行就可以进行依赖注入.因此,您可以为控制台应用程序设置自己的环境,然后基于这些技术为您的应用程序构建环境,而无需依赖实际的 ASP.NET Core 内容.

We have .NET Core applications that could benefit from ASP.NET Core logging (and service provider) but they're not using any WebHost or WebHostBuilder.

I'd like to peek inside extensions/methods like ConfigureLogging or UseStartup to get some inspiration on how to set-up my root configuration/service provider.

I thought ASP.NET Core was open-source, but somehow I lack the talent to find those sources. Where can I learn more about how all this is set up, and how I could use them in any console app, not just apps using WebHostBuilder?

解决方案

The various components from ASP.NET Core are all usable separately. So you can absolutely use things like Microsoft.Extensions.Logging, or even other things like DependencyInjection, Configuration or Options separately without having to actually use ASP.NET Core or its WebHost.

I could point you to various locations in the sources now (e.g. the WebHostBuilder, the WebHost, or the default builder), but getting logging up and running in your own application is actually pretty simple, so you don’t need to browse through all that.

Instead, just add a reference to Microsoft.Extensions.Logging and your favorite logging providers, e.g. Microsoft.Extensions.Logging.Console, and then set up a logger factory and create a logger:

var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole();

var logger = loggerFactory.CreateLogger("categoryName");
logger.LogInformation("Whee!");

That is all you need to get logging working. Of course, you now need to create your own loggers and pass it to where you need it, but you have full control over it.

If you like, you can also add dependency injection to the mix by adding a reference to Microsoft.Extensions.DependencyInjection and creating a service collection:

var services = new ServiceCollection();

// add logging
services.AddLogging(loggerBuilder =>
{
    loggerBuilder.AddConsole();
});

// add your services
services.AddTransient<MyService>();


// create the service provider
var serviceProvider = services.BuildServiceProvider();

// request your service and do something
var service = serviceProvider.GetService<MyService>();
service.DoSomething();

public class MyService
{
    private readonly ILogger<MyService> _logger;
    public MyService(ILogger<MyService> logger)
    {
        _logger = logger;
    }

    public void DoSomething()
    {
        _logger.LogInformation("Did something!");
    }
}

And suddenly you have working dependency injection with logging in just a few lines. So you can set up your own environment for console applications, and then built upon those techniques for your application—without relying on actual ASP.NET Core stuff.

这篇关于在不使用 WebHost 的情况下使用 ASP.NET Core 日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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