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

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

问题描述

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

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.

我想浏览一下 ConfigureLogging 之类的扩展/方法或 UseStartup 来获得有关如何设置我的根配置/服务提供商的灵感。

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.

我认为ASP .NET Core是开源的,但是以某种方式我缺乏寻找这些资源的才能。

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?

推荐答案

ASP.NET Core中的各种组件都可以单独使用。因此,您可以绝对使用 Microsoft.Extensions.Logging 之类的东西,甚至是其他东西像 DependencyInjection 配置选项,而不必实际使用ASP.NET Core或其WebHost。

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.

我现在可以将您指向源代码中的各个位置(例如 WebHostBuilder 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.

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

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.

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

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!");
    }
}

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

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天全站免登陆