没有注册“Microsoft.Extensions.Logging.ILoggingBuilder"类型的服务 [英] No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered
问题描述
我正在使用 .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.
我的输出窗口显示了这个
and my output window shows this
无法为方法的参数loggingBuilder"解析Microsoft.Extensions.Logging.ILoggingBuilder"类型的服务.
Could not resolve a service of type 'Microsoft.Extensions.Logging.ILoggingBuilder' for the parameter 'loggingBuilder' of method.
我是 .NET Core 的新手,但我在这里遗漏了什么吗?使用 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 https://go.microsoft.com/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
下向您的服务集合注册它:
Or, alternatively, register it with your service collection under ConfigureServices
:
services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
这篇关于没有注册“Microsoft.Extensions.Logging.ILoggingBuilder"类型的服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!