创建了一个空白的ASP.NET Core 2.0应用程序。 在Startup.cs中,想要记录传入的请求。所以在configure方法中,我使用的是Microsoft.Extensions.Logging.ILogger
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger)
{
app.Use(next =>
{
return async context =>
{
logger.LogInformation("Incoming request");
await next(context);
logger.LogInformation("Outgoing response");
};
});
然而,当我构建项目时,它抱怨
An error occurred while starting the application.
InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.
为什么以及如何注册此服务?如果它是我的界面,它仍然有意义
services.AddScope
在ConfigureServices中
答案 0 :(得分:5)
ILogger总是一种类型,尝试改变它:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger<Startup> logger)
{
app.Use(next =>
{
return async context =>
{
logger.LogInformation("Incoming request");
await next(context);
logger.LogInformation("Outgoing response");
};
});