本文介绍了没有注册类型'Microsoft.Extensions.Logging.ILogger'的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
创建了一个空白的ASP.NET Core 2.0应用程序.在Startup.cs中,要记录传入的请求.所以在配置方法中,我使用的是Microsoft.Extensions.Logging.ILogger
Created a blank ASP.NET Core 2.0 application. In Startup.cs, would like to log incoming requests. So in configure method, I am using 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"); }; });但是,当我构建项目时,它会抱怨
However, when I build the project, its complaining
An error occurred while starting the application. InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILogger' has been registered.为什么以及如何注册此服务?如果这是我的界面,那么这样做仍然很有意义
Why and how should I register this service? Had it been my interface, it would have still made sense to do
services.AddScope在ConfigureServices中
in ConfigureServices
推荐答案ILogger始终是一种类型,请尝试像这样更改它:
ILogger is always of a type, try change it like this:
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"); }; });更多推荐
没有注册类型'Microsoft.Extensions.Logging.ILogger'的服务
发布评论