How can I stop logs generated by AspNetCore?

Eric P 5 Reputation points
2023-03-04T03:11:23.3066667+00:00

I have an ASP.NET Core project running on an Azure Web App, logging to blob storage, which is full of hundreds of MB of the following:

[Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request starting HTTP/1.1
[Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executing endpoint
[Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Ro
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core
[Information] Microsoft.EntityFrameworkCore.Infrastructure: Entity Framework Core
[Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Ex
[Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Ex
[Information] Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor: Execu
[Information] Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker: Ex
[Information] Microsoft.AspNetCore.Routing.EndpointMiddleware: Executed endpoint 
[Information] Microsoft.AspNetCore.Hosting.Diagnostics: Request finished HTTP/1.1

My own logging statements (created via ILogger) are lost in the noise, how can I stop logging all this activity?

My HostBuilder looks like this:

static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        })
        .ConfigureAppConfiguration((context, config) =>
        {
            config.AddJsonFile("appsettings.json", optional: false);
            config.AddEnvironmentVariables();
        })
        .ConfigureLogging((context, logging) =>
        {
            logging.ClearProviders();
            logging.AddAzureWebAppDiagnostics();
            if (context.HostingEnvironment.IsDevelopment())
                logging.AddConsole();
        })

My appsettings.json looks like this:

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Warning"
        }
    },
    "ConnectionStrings": {
        "DefaultConnection": "Server=.\\SQLEXPRESS;Database=Blah;Integrated Security=true;MultipleActiveResultSets=True"
    }
}

In development on my local PC, everything is logged to the console is as expected (i.e. Microsoft.* log messages don't appear). The appsettings.json file is apparently doing it's job because if I change "Microsoft": "Warning" to "Microsoft": "Information" all those Microsoft.* log messages (that I don't want) do appear in the console. But running in an app service on Azure, it's as if the appsettings.json file is being ignored. The call to config.AddEnvironmentVariables() is mainly to override the DefaultConnection database connection string in my appsettings.json file with the one on the Configuration page on the web app in Azure, but that page doesn't contain any log-related environment variable settings.

I just can't seem to control what's going into my logs, any advice?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,757 questions
{count} vote

1 answer

Sort by: Most helpful
  1. ajkuma 26,131 Reputation points Microsoft Employee
    2023-03-08T18:09:40.7666667+00:00

    @Eric P , Apologies for the delay from over the weekend.

    From your issue description, I understand you're wanting to stop the logs generated.
    Just to highlight, in App Service, you can set app settings outside of your app code.

    If you configure an app setting with the same name in App Service and in appsettings.json, for example, the App Service value takes precedence over the appsettings.json value. The local appsettings.json value lets you debug the app locally, but the App Service value lets you run the app in production with production settings.
    Also, connection strings work in the same way. This way, you can keep your application secrets outside of your code repository and access the appropriate values without changing your code.

    When deployed to Azure App Service, the app uses the settings in the App Service logs section of the App Service page of the Azure portal.

    The default location for log files is in the D:\\home\\LogFiles\\Application folder, and the default file name is diagnostics-yyyymmdd.txt. The default file size limit is 10 MB, and the default maximum number of files retained is 2.
    Please check this doc section for config info: Azure App Service

    Please let us know, I'll follow-up with you further.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.