简体   繁体   中英

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

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

    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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM