ASP.NET Core: How to disable logging for Static Files?

1.4k views Asked by At

I use ASP.NET Core 2.2 with Angular 8 for my project,

I am serving angular Static Files from my .net core project using SPA Extensions Like this:

public void ConfigureServices(IServiceCollection services)
{
    services.AddSpaStaticFiles(options =>
    {
        options.RootPath = "ClientApp/dist";
    });
}

public void Configure(IApplicationBuilder app){
   app.UseSpa(spa =>
   {
        spa.Options.SourcePath = "ClientApp";
   });
}

My problem is that my logs contains every request regarding back-end or front-end server, That is, I find a lot of logs to routes like:

https://example.com/index.html

https://example.com/assets/images/logo.svg

Which is really annoying, I don't want front-end server logs (static files)!

I have one general logger in my custom middle-ware defined like this:

public CustomRequestMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
{
    _next = next;
    _logger = loggerFactory.CreateLogger("My Logger");
}

I can disable its logs from appsettings.json file like this:

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "None",
      "Microsoft": "None",
      "Hangfire": "None",
      "My Logger": "None"
    }
  }

But the problem is that this logger is used for all requests to system, which includes both front-end (static files) & back-end (API) logs, and I need only to disable logging for front-end logs.

Is there a way to filter logs?

1

There are 1 answers

0
Kerem Yıldız On

You can solve the problem by adding the code "Microsoft.AspNetCore.Hosting.Diagnostics": "None" to the logging section of the appsetting.json file