ASP.NET Core 3.1, logging only specific messages to Azure Log Stream Console


Question [ASP.NET Core 3.1, logging only specific messages to Azure Log Stream Console] has 2 solution 2020-08-19 03:04:42 c#

I have an ASP.NET Core 3.1 API running on Microsoft Azure App Service.

I have set up an ILogger into the app and I use it extensively to monitor the behavior of the API. I would like to be able to see those messages real time into the Azure Log Stream of the app portal.

I managed to get logs after registering the AzureWebAppDiagnostics into Program.cs as follows:

 public class Program
 {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddAzureWebAppDiagnostics();
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
       }
    }

The problem is that messages I logged explicitly, using

logger.LogInformation("whatever message") 

are mixed with system messages and becomes flooded: Entity Framework commands, Microsoft.Hosting.Lifetime messages, and multiple other informative messages.

How can I set up the system in a way that I get only error/warning messages from the system, and informative messages from my app code.

I tried to change the appsettings.json as follows, but it seems to have no effect at all :

"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Error",
      "Microsoft.Hosting.Lifetime": "Error",
      "Microsoft.*": "Error",
      "System": "Error",
      "Microsoft.EntityFrameworkCore.*": "Warning" 
    }
  }

I deleted as well the appsettings.development.json file just in case, as suggested here.

What am I doing wrong?


Question [ASP.NET Core 3.1, logging only specific messages to Azure Log Stream Console] solution number 1

Actually, after throrough researches, the solution was provided here

You need to add AzureAppServicesBlob and AzureAppServicesFile configuration into your configuration file, they seem to be the one taken into account by the Azure Log Stream.

So I added the following part :

"AzureAppServicesBlob": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft": "Error",
    "System": "Error"
  }
},
"AzureAppServicesFile": {
  "LogLevel": {
    "Default": "Information",
    "Microsoft": "Error",
    "System": "Error"
  }
}

Question [ASP.NET Core 3.1, logging only specific messages to Azure Log Stream Console] solution number 2

It's not wildcarded in the configuration. It's simply a namespace prefix: use Microsoft.EntityFrameworkCore as the property name.


.htaccess .net .net-core 2d 3d 3d-printing abp abstract-syntax-tree actions-on-google actionscript-3 active-directory activemq activemq-artemis acumatica adobe-xd aframe ag-grid agora.io air airflow ajax akka alert alexa algorithm alignment allure amadeus amazon-cloudformation amazon-cognito