简体   繁体   English

在控制台应用程序中注入ILoggerFactory与在asp.net核心1.1中实例化一个

[英]Injecting ILoggerFactory in Console app vs instantiating one in asp.net core 1.1

I have put together a trivial Console application using asp.net core 1.1. 我使用asp.net core 1.1整理了一个简单的Console应用程序。 I setup Kestrel hosting and used a Configure method where I inject IApplicationBuilder and ILoggerFactory . 我设置了Kestrel托管并使用了一个Configure方法,我注入了IApplicationBuilderILoggerFactory I call the AddConsole extension on loggerFactory . 我呼吁的LoggerFactoryAddConsole扩展。 Then I run the simplest middleware, outputing a message. 然后我运行最简单的中间件,输出一条消息。 The application code is shown below: 应用程序代码如下所示:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace DemoILoggerFactory
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Program>()
                .Build();
            host.Run();
        }

        public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();
            app.Run( async context => {
                await context.Response.WriteAsync("Message ..");
            });
        }
    }
}

When I run the application, I get the following output in the console: 当我运行应用程序时,我在控制台中获得以下输出:

Hosting environment: QQ
Content root path: F:\REPOS CORE     1.0.0\LOGGING\DemoILoggerFactory\src\DemoILoggerFactory\bin\Debug\netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

After I visit "locahost:5000" the following log info is shown in the console: 访问“locahost:5000”后,控制台中会显示以下日志信息:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 125.5854ms 200
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/favicon.ico
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 0.9471ms 200

Later I removed the ILoggerFactory from the Configure arguments and instantiated one within the Configure method. 后来我从Configure参数中删除了ILoggerFactory,并在Configure方法中实例化了一个。 The only difference is the signature of the Configure method and the instantation of a LoggerFactory. 唯一的区别是Configure方法的签名和LoggerFactory的即时消息。 The modified code is shown below: 修改后的代码如下所示:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;

namespace DemoILoggerFactory
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseStartup<Program>()
                .Build();
            host.Run();
        }

        public void Configure(IApplicationBuilder app)
        {
            ILoggerFactory loggerFactory = new LoggerFactory();
            loggerFactory.AddConsole();
            app.Run( async context => {
                await context.Response.WriteAsync("Message ..");
            });
        }
    }
}

Now, Running the application and before I visit localhost:5000 the following information shows up in the console window: 现在,运行应用程序,在访问localhost:5000之前,控制台窗口中显示以下信息:

Hosting environment: QQ
Content root path: F:\REPOS CORE 1.0.0\LOGGING\DemoILoggerFactory\src\DemoILoggerFactory\bin\Debug\netcoreapp1.0
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

After I revisit "localhost:5000", no additional request logs show up in the console. 在我重新访问“localhost:5000”后,控制台中不会显示其他请求日志。

The Project.json file, for both cases, is as follows: 对于这两种情况,Project.json文件如下:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.1.0"
    },
    "Microsoft.AspNetCore.Hosting": "1.1.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.Extensions.Logging": "1.1.0",
    "Microsoft.Extensions.Logging.Console": "1.1.0",
    "Microsoft.AspNetCore.Http": "1.1.0"
  },

  "frameworks": {
    "netcoreapp1.1": {
      "imports": "dnxcore50"
    }
  }
}

What am I missing? 我错过了什么? Why do the two "LoggerFactory" instances behave differently? 为什么两个“LoggerFactory”实例的行为不同?

I experienced exactly the same while writing this ASP.NET Core Logging Tutorial . 编写ASP.NET核心日志教程时,我的体验完全相同。 Digging into the code showed, that Kestrel also depend on Microsoft.Extensions.Logging. 深入研究代码表明,Kestrel还依赖于Microsoft.Extensions.Logging。 When started, Kestrel adds a logger to the ILoggerFactory initialized by ASP.NET Core. 启动时,Kestrel将一个记录器添加到ASP.NET Core初始化的ILoggerFactory The logger logs a lot of data about the internals in Kestrel, information about TCP connections, HTTP requests etc. If you lower the minimum level for the Microsoft.AspNetCore.Server.Kestrel logger category, you will see more log messages from Kestrel. 记录器记录有关Kestrel内部的大量数据,有关TCP连接,HTTP请求等的信息。如果降低Microsoft.AspNetCore.Server.Kestrel记录器类别的最低级别,您将看到来自Kestrel的更多日志消息。 Also, if you inspect the ILoggerFactory provided by ASP.NET Core in the debugger, you will see the Kestrel logger which is not available in the one you create yourself. 此外,如果您在调试器中检查ASP.NET Core提供的ILoggerFactory ,您将看到Kestrel记录器,这是您自己创建的记录器中没有的。

While you may be able to configure the same amount of log messages from Kestrel, by manually creating a KestrelTrace , I think that you will be better of using the ILoggerFactory provided by ASP.NET Core. 虽然您可以通过手动创建KestrelTrace来配置来自Kestrel的相同数量的日志消息,但我认为您最好使用ASP.NET Core提供的ILoggerFactory At least that was my conclusion :) 至少那是我的结论:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM