简体   繁体   中英

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. I setup Kestrel hosting and used a Configure method where I inject IApplicationBuilder and ILoggerFactory . I call the AddConsole extension on loggerFactory . 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:

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. The only difference is the signature of the Configure method and the instantation of a 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:

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.

The Project.json file, for both cases, is as follows:

{
  "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?

I experienced exactly the same while writing this ASP.NET Core Logging Tutorial . Digging into the code showed, that Kestrel also depend on Microsoft.Extensions.Logging. When started, Kestrel adds a logger to the ILoggerFactory initialized by ASP.NET Core. 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. 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.

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. At least that was my conclusion :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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