UtpMahesh
UtpMahesh

Reputation: 410

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'.

Upvotes: 8

Views: 6343

Answers (3)

M.Ali El-Sayed
M.Ali El-Sayed

Reputation: 1779

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())
    ......;

Upvotes: 3

UtpMahesh
UtpMahesh

Reputation: 410

Seems like it is a bug on the middleware itself : https://github.com/mthamil/AspNetCore.Serilog.RequestLoggingMiddleware/issues/3

Upvotes: 0

Nicholas Blumhardt
Nicholas Blumhardt

Reputation: 31797

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.

Upvotes: 12

Related Questions