问题
I've created a new .net core 2.1 web app and deployed to Azure and Log Stream and and Application Logging to Blob storage don't seem to be honoring my Logging configuration.
I created a new Solution with a new project in Visual Studio 2019 for a .net core 2.1 web app. In the home controller Index route, we added a line log some information that looks like this:
private readonly ILogger<HomeController> _logger;
public HomeController(ILogger<HomeController> logger)
{
_logger = logger;
}
public IActionResult Index()
{
_logger.LogInformation("=========================================");
_logger.LogError("=========================================");
return View();
}
And in the appsettings.Development.json
we've set the LogLevel
for System and Microsoft to "Error".
What I expect is behavior in Azure to be what it is when ran locally. Locally when accessing the index route and having the appsettings.Development.json LogLevel for System and Microsoft set to "Information" We see this output in the Debug output window:
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http//localhost:44378/
Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:Information: Identity.Application was not authenticated. Failure message: Unprotect ticket failed Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Route matched with {action = "Index", controller = "Home", page = "", area = ""}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Index() on controller LoggingTest.Controllers.HomeController (LoggingTest). Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executing action method LoggingTest.Controllers.HomeController.Index (LoggingTest) - Validation state: Valid LoggingTest.Controllers.HomeController:Information: ========================================= LoggingTest.Controllers.HomeController:Error: ========================================= Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action method LoggingTest.Controllers.HomeController.Index (LoggingTest), returned result Microsoft.AspNetCore.Mvc.ViewResult in 7.9475ms. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:Information: Executing ViewResult, running view Index. Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:Information: Executed ViewResult - view Index executed in 11.4824ms. Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action LoggingTest.Controllers.HomeController.Index (LoggingTest) in 37.629ms Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 54.1369ms 200 text/html; charset=utf-8
And when System and Microsoft are set to "Error":
LoggingTest.Controllers.HomeController:Information: ========================================= LoggingTest.Controllers.HomeController:Error: .=========================================
We want this to be the case to be our output to our LogStream and Logs. We essentially don't want the EfCore and other Microsoft related information in the log unless its of LogLevel "Error". But we want our logs of Level "Information" to be logged.
After publishing to Azure and setting the ASPNETCORE_ENVIRONMENT to development to use the same Appsettings setup. The log stream and blob for the log look like this after calling the index:
2019-05-17 15:57:24.844 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET http//loggingtest20190517104201.azurewebsites.net/ 2019-05-17 15:57:24.844 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Route matched with {action = "Index", controller = "Home", page = "", area = ""}. Executing action LoggingTest.Controllers.HomeController.Index (LoggingTest) 2019-05-17 15:57:24.844 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executing action method LoggingTest.Controllers.HomeController.Index (LoggingTest) - Validation state: Valid 2019-05-17 15:57:24.844 +00:00 [Information] LoggingTest.Controllers.HomeController: ========================================= 2019-05-17 15:57:24.845 +00:00 [Error] LoggingTest.Controllers.HomeController: ========================================= 2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action method LoggingTest.Controllers.HomeController.Index (LoggingTest), returned result Microsoft.AspNetCore.Mvc.ViewResult in 0.0635ms. 2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor: Executing ViewResult, running view Index. 2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor: Executed ViewResult - view Index executed in 0.8902ms. 2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker: Executed action LoggingTest.Controllers.HomeController.Index (LoggingTest) in 1.0913ms 2019-05-17 15:57:24.846 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request finished in 1.4542ms 200 text/html; charset=utf-8 2019-05-17 15:57:24.941 +00:00 [Information] Microsoft.AspNetCore.Hosting.Internal.WebHost: Request starting HTTP/1.1 GET
The rest of log removed for brevity...
Heres my appsettings.development.json file:
{ "Logging": { "LogLevel": { "Default": "Debug", "System": "Error", "Microsoft": "Error" } } }
Heres my appsettings.json file:
{ "ConnectionStrings": { "DefaultConnection": "Removed" }, "AllowedHosts": "*" }
Environement Variable set in Azure Web App:
ASPNETCORE_ENVIRONMENT = Development
The Program.cs and Startup.cs are unmodified from the project template.
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
Why are my Log Levels not being honored?
回答1:
Finally able to make it work by placing these setting in my appsettings.development.json file:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Information",
"System": "Error",
"Microsoft": "Error"
},
"AzureAppServicesBlob": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Error",
"System": "Error"
}
},
"AzureAppServicesFile": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Error",
"System": "Error"
}
}
}
}
回答2:
In the past, I have used the following to configure the Azure log Provider
"AzureAppServicesDiagnosticsSettings": {
"Microsoft": "Warning"
}
But reviewing the documentation that provider is now obsolete and is replaced with AzureFileLoggerOptions and AzureBlobLoggerOptions (see Logging in ASP.NET Core).
Though, I have not tested either of those configuration options to know if they work the same.
来源:https://stackoverflow.com/questions/56190165/iloggingbuilder-logging-loglevel-in-appsettings-json-doesnt-seem-to-be-acknowle