Appsettings Json 中的 ILoggingBuilder 日志记录 LogLevel 似乎未在 Azure 日志流或 Blob 日志中得到确认

AJ *_*oto 5 c# logging azure asp.net-core-2.1

我创建了一个新的 .net core 2.1 Web 应用程序并部署到 Azure,日志流和应用程序日志记录到 Blob 存储似乎不支持我的日志记录配置。

我在 Visual Studio 2019 中为 .net core 2.1 Web 应用程序创建了一个新的解决方案。在家庭控制器索引路由中,我们添加了一行记录一些信息,如下所示:

private readonly ILogger<HomeController> _logger;

public HomeController(ILogger<HomeController> logger)
{
    _logger = logger;
}

public IActionResult Index()
{
    _logger.LogInformation("=========================================");
    _logger.LogError("=========================================");
    return View();
}
Run Code Online (Sandbox Code Playgroud)

在 中,appsettings.Development.json我们将LogLevel系统和 Microsoft 设置为“错误”。

我期望 Azure 中的行为与本地运行时的行为相同。在本地访问索引路由并将系统和 Microsoft 的 appsettings.Development.json LogLevel 设置为“信息”时,我们在调试输出窗口中看到以下输出:

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求启动 HTTP/1.1 GET http//localhost:44378/
Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:Identity.Application 未经过身份验证。失败消息:取消保护票证失败 Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息:路由与 {action = "Index"、controller = "Home"、page = ""、area = ""} 匹配。在控制器 LoggingTest.Controllers.HomeController (LoggingTest) 上使用签名 Microsoft.AspNetCore.Mvc.IActionResult Index() 执行控制器操作。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息:正在执行操作方法 LoggingTest.Controllers.HomeController.Index (LoggingTest) - 验证状态:有效 LoggingTest.Controllers.HomeController:信息:============ ============================= LoggingTest.Controllers.HomeController:错误:============= ============================ Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息:执行的操作方法 LoggingTest.Controllers.HomeController.Index (LoggingTest),在 7.9475ms 内返回结果 Microsoft.AspNetCore.Mvc.ViewResult。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:正在执行ViewResult,正在运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行 ViewResult - 视图索引在 11.4824 毫秒内执行。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:信息:在 37.629 毫秒内执行操作 LoggingTest.Controllers.HomeController.Index (LoggingTest) Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求在 54.1369 毫秒内完成 200 text/html;字符集=utf-8

当系统和微软设置为“错误”时:

LoggingTest.Controllers.HomeController:信息: ============================================= LoggingTest .Controllers.HomeController:错误:.=============================================

我们希望 LogStream 和日志的输出就是这种情况。我们本质上不希望日志中包含 EfCore 和其他 Microsoft 相关信息,除非其 LogLevel“错误”。但我们希望记录“信息”级别的日志。

发布到 Azure 并将 ASPNETCORE_ENVIRONMENT 设置为开发后,以使用相同的 Appsettings 设置。调用索引后,日志流和日志的 blob 如下所示:

2019-05-17 15:57:24.844 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求启动 HTTP/1.1 GET http://loggingtest20190517104201.azurewebsites.net/ 2019-05-17 15:57 :24.844 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:路由与 {action = "Index", controller = "Home", page = "", area = ""} 匹配。执行操作 LoggingTest.Controllers.HomeController.Index (LoggingTest) 2019-05-17 15:57:24.844 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:执行操作方法 LoggingTest.Controllers.HomeController.Index ( LoggingTest) - 验证状态:有效 2019-05-17 15:57:24.844 +00:00 [信息] LoggingTest.Controllers.HomeController: ====================== ===================== 2019-05-17 15:57:24.845 +00:00 [错误] LoggingTest.Controllers.HomeController: ====== ===================================== 2019-05-17 15:57:24.845 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:执行操作方法LoggingTest.Controllers.HomeController.Index(LoggingTest),在0.0635ms内返回结果Microsoft.AspNetCore.Mvc.ViewResult。2019-05-17 15:57:24.845 +00:00 [信息] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:执行ViewResult,运行视图Index。2019-05-17 15:57:24.845 +00:00 [信息] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:已执行 ViewResult - 视图索引在 0.8902 毫秒内执行。2019-05-17 15:57:24.845 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:在 1.0913ms 内执行操作 LoggingTest.Controllers.HomeController.Index (LoggingTest) 2019-05-17 15:57 :24.846 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求在 1.4542ms 200 text/html 内完成;charset=utf-8 2019-05-17 15:57:24.941 +00:00 [信息] Microsoft.AspNetCore.Hosting.Internal.WebHost:请求开始 HTTP/1.1 GET

为了简洁起见,删除了日志的其余部分...

这是我的 appsettings.development.json 文件:

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Error",
      "Microsoft": "Error"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 appsettings.json 文件:

{
  "ConnectionStrings": {
    "DefaultConnection": "Removed"
  }, 
  "AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)

Azure Web App中设置的环境变量:

ASPNETCORE_ENVIRONMENT = 开发

Program.cs 和 Startup.cs 未对项目模板进行修改。

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
Run Code Online (Sandbox Code Playgroud)

为什么我的日志级别没有得到遵守?

AJ *_*oto 4

最后能够通过将这些设置放入我的 appsettings.development.json 文件中来使其工作:

{
  "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"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)