我使用asp.net core 1.1整理了一个简单的Console应用程序.
我设置了Kestrel托管并使用了我注入的Configure方法
IApplicationBuilder和ILoggerFactory.
我在loggerFactory上调用AddConsole扩展.
然后我运行最简单的中间件,输出一条消息.
应用程序代码如下所示:
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 ..");
});
}
}
}
当我运行应用程序时,我在控制台中获得以下输出:
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.
访问“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
后来我从Configure参数中删除了ILoggerFactory,并在Configure方法中实例化了一个.唯一的区别是Configure方法的签名和LoggerFactory的即时消息.修改后的代码如下所示:
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 ..");
});
}
}
}
现在,运行应用程序,在访问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.
在我重新访问“localhost:5000”后,控制台中不会显示其他请求日志.
对于这两种情况,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"
}
}
}
我错过了什么?
为什么两个“LoggerFactory”实例的行为不同?
最佳答案 在编写这个
ASP.NET Core Logging Tutorial时我的经历完全相同.深入研究代码表明,Kestrel还依赖于Microsoft.Extensions.Logging.启动时,Kestrel将一个记录器添加到ASP.NET Core初始化的ILoggerFactory中.记录器记录有关Kestrel内部的大量数据,有关TCP连接,HTTP请求等的信息.如果降低Microsoft.AspNetCore.Server.Kestrel记录器类别的最低级别,您将看到来自Kestrel的更多日志消息.此外,如果您在调试器中检查ASP.NET Core提供的ILoggerFactory,您将看到Kestrel记录器,这是您自己创建的记录器中没有的.
虽然您可以通过手动创建KestrelTrace来配置来自Kestrel的相同数量的日志消息,但我认为您最好使用ASP.NET Core提供的ILoggerFactory.至少那是我的结论:)