Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to resolve service for type 'Serilog.ILogger' : "AspNetCore.Serilog.RequestLoggingMiddleware"

I am new to programming, I am trying to implement a logging solution for .net core project.

I want to collect Default and Microsoft category logs.

APPSETTING.JSON

      {
          "Serilog": {
              "MinimumLevel": {
                  "Default": "Information",
                  "System": "Warning",
                  "Microsoft": "Information"
              },
              "File": {
                  "location": "logs/timeapi.log"
              }
          },
          "EnableSwagger": true
      }

PROGRAM.CS

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
                WebHost.CreateDefaultBuilder(args)
                    .UseStartup<Startup>()
                    .UseSerilogRequestLogging();

STARTUP.CS

app.UseSerilogRequestLogging(options =>
            options.RequestProjection =
            r => new { r.IsHttps, QueryString = r.QueryString.Value });
        app.UseMvc();
https://github.com/mthamil/AspNetCore.Serilog.RequestLoggingMiddleware

The error I am getting is :

Unable to resolve service for type 'Serilog.ILogger' while attempting to activate 'AspNetCore.Serilog.RequestLoggingMiddleware.SerilogRequestMiddleware'.
like image 636
UtpMahesh Avatar asked Aug 13 '19 04:08

UtpMahesh


People also ask

What is Serilog Aspnetcore?

Introduction. Serilog is a structural logging library for Microsoft . Net and has become the preferred logging library for . Net applications.


2 Answers

The middleware you're using appears to need Serilog's ILogger added in the app's Startup.ConfigureServices() method; e.g.:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<ILogger>(Log.Logger);
    }

(This example assumes you're configuring Serilog's Log.Logger static property.)

There's a more complete example of setting up request logging with Serilog in the readme for the Serilog.AspNetCore package, though its implementation of UseSerilogRequestLogging() is quite different.

like image 60
Nicholas Blumhardt Avatar answered Sep 24 '22 18:09

Nicholas Blumhardt


if you are using .net core 3.1 + only use it in program.cs like this and remove every thing about Serilog from startup.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
    .UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
    .WriteTo.Console())
    ......;
like image 29
M.Ali El-Sayed Avatar answered Sep 24 '22 18:09

M.Ali El-Sayed