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

编程入门 行业动态 更新时间:2024-10-26 17:23:06
本文介绍了没有注册类型'Microsoft.Extensions.Logging.ILoggingBuilder'的服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用.NET core 2.2构建全新的Web API。在我的 Startup.cs 中,在configure方法中设置了 ILoggerFactory 。当我做这样的事情并添加以下代码时

I am building brand new Web APIs with .NET core 2.2. In my Startup.cs I have set ILoggerFactory in configure method. When I do such a thing and add following code

loggerFactory.AddConsole(); loggerFactory.AddDebug();

我得到的信息是此方法已过时,它将在以后的版本中删除,我应该使用 ILoggingBuilder 。没问题,我已经用这种新的日志记录方法替换了,一旦启动Web API,我就会报错

I get information saying that this method is obsolete and it will be removed in future versions, instead I should use ILoggingBuilder.No problem, I have replaced with this new logging method and as soon as I start Web APIs I get error

InvalidOperationException:没有注册类型为'Microsoft.Extensions.Logging.ILoggingBuilder'的服务。

InvalidOperationException: No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered.

,我的输出窗口显示此

无法解析参数'Microsoft.Extensions.Logging.ILoggingBuilder'类型的服务

Could not resolve a service of type 'Microsoft.Extensions.Logging.ILoggingBuilder' for the parameter 'loggingBuilder' of method.

.NET核心是我的新手,但是我在这里缺少什么吗?使用ILoggerFactury,我不必注册任何服务,日志记录就可以正常工作。 此处是Microsoft的文档并不是很有帮助。

I am new to .NET core, but am I missing something here? With ILoggerFactury, I did not have to register any services and logging would work just fine. Microsoft's documentation here is not really helpful.

Startup.cs看起来像这样:

Startup.cs looks like this:

public class Startup { // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit go.microsoft/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggingBuilder loggingBuilder) { loggingBuilder.AddConsole(); loggingBuilder.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } app.UseMvc(); //app.Run(async (context) => //{ // await context.Response.WriteAsync("Hello World!"); //}); } }

Program.cs看起来像这样:

Program.cs looks like this:

public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>(); }

推荐答案

它不起作用,因为您不要在依赖项注入容器中注册日志记录组件。有两种方法可以执行此操作:

It's not working because you're not registering the logging components with your dependency injection container. There are two ways you can do this:

可以将其配置为 CreateWebHostBuilder 的一部分:

Either configure it as part of the CreateWebHostBuilder:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureLogging((hostingContext, logging) => { logging.AddConsole(); logging.AddDebug(); }) .UseStartup<Startup>();

或者,也可以在 ConfigureServices :

services.AddLogging(logging => { logging.AddConsole(); logging.AddDebug(); });

更多推荐

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

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

发布评论

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

>www.elefans.com

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