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();
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: No service for type 'Microsoft.Extensions.Logging.ILoggingBuilder' has been registered.
and my output window shows this
Could not resolve a service of type 'Microsoft.Extensions.Logging.ILoggingBuilder' for the parameter 'loggingBuilder' of method.
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 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 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>();
}
The ILoggingBuilder extension is useful when using a WebHostBuilder or a HostBuilder , also known as Generic host. This extension method is used by the ASP.NET Core integration under the hood to automatically.
In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).
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:
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>();
Or, alternatively, register it with your service collection under ConfigureServices
:
services.AddLogging(logging =>
{
logging.AddConsole();
logging.AddDebug();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With