没有注册类型'Microsoft.Extensions.Logging.ILogger'的服务

编程入门 行业动态 更新时间:2024-10-25 17:29:57
本文介绍了没有注册类型'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'的服务

本文发布于:2023-11-15 04:08:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有注册   类型   Microsoft   Logging   Extensions

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!