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

索托

我创建了一个新的.net core 2.1 Web应用程序,并将其部署到Azure和Log Stream,并且将应用程序日志记录到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();
}

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

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

Microsoft.AspNetCore.Hosting.Internal.WebHost:信息:请求启动HTTP / 1.1 GET http // localhost:44378 /
Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:信息:Identity.Application未通过身份验证。错误消息:Unprotect票证失败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 :Information:执行的操作方法LoggingTest.Controllers.HomeController.Index(LoggingTest),在7.9475毫秒内返回结果Microsoft.AspNetCore.Mvc.ViewResult。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:执行ViewResult,运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行ViewResult-在11.4824毫秒内执行的视图索引。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information:在37.629毫秒内执行了LoggingTest.Controllers.HomeController.Index(LoggingTest)操作Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms内完成200 text / html; 字符集= utf-8 信息:已执行操作方法LoggingTest.Controllers.HomeController.Index(LoggingTest),返回结果Microsoft.AspNetCore.Mvc.ViewResult在7.9475ms内。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:执行ViewResult,运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行ViewResult-在11.4824毫秒内执行的视图索引。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information:在37.629毫秒内执行了LoggingTest.Controllers.HomeController.Index(LoggingTest)操作Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms内完成200 text / html; 字符集= utf-8 信息:已执行操作方法LoggingTest.Controllers.HomeController.Index(LoggingTest),返回结果Microsoft.AspNetCore.Mvc.ViewResult在7.9475ms内。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:执行ViewResult,运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行ViewResult-在11.4824毫秒内执行的视图索引。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information:在37.629毫秒内执行了LoggingTest.Controllers.HomeController.Index(LoggingTest)操作Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms内完成200 text / html; 字符集= utf-8 ViewFeatures.ViewResultExecutor:信息:执行ViewResult,运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行ViewResult-在11.4824毫秒内执行的视图索引。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information:在37.629毫秒内执行了LoggingTest.Controllers.HomeController.Index(LoggingTest)操作Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms内完成200 text / html; 字符集= utf-8 ViewFeatures.ViewResultExecutor:信息:执行ViewResult,运行视图Index。Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:信息:已执行ViewResult-在11.4824毫秒内执行的视图索引。Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information:在37.629毫秒内执行了LoggingTest.Controllers.HomeController.Index(LoggingTest)操作Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms内完成200 text / html; 字符集= utf-8 629ms Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms中完成200 text / html; 字符集= utf-8 629ms Microsoft.AspNetCore.Hosting.Internal.WebHost:Information:请求在54.1369ms中完成200 text / html; 字符集= utf-8

并且当System和Microsoft设置为“错误”时:

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

我们希望这种情况成为我们向LogStream和Logs的输出。除非LogLevel为“错误”,否则我们基本上不希望EfCore和其他Microsoft相关信息出现在日志中。但是我们希望记录“信息”级别的日志。

发布到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 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionActionInvoker:执行动作方法LoggingTest.Controllers.HomeController.Index( LoggingTest)-验证状态:有效2019-05-17 15:57:24.844 +00:00 [Information] LoggingTest.Controllers.HomeController:==================== ===================== 2019-05-17 15:57:24.845 +00:00 [错误] LoggingTest.Controllers.HomeController:======================================== = 2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:执行的操作方法LoggingTest.Controllers.HomeController.Index(LoggingTest),返回结果Microsoft.AspNetCore.Mvc。 ViewResult以0.0635ms为单位。2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:执行ViewResult,运行视图索引。2019-05-17 15:57:24.845 +00:00 [Information] Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor:已执行ViewResult-视图索引在0.8902毫秒内执行。2019-05-17 15:57:24.845 +00:00 [信息] Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:执行的操作LoggingTest.Controllers.HomeController.Index(LoggingTest)在1.0913毫秒中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"
    }
  }
}

这是我的appsettings.json文件:

{
  "ConnectionStrings": {
    "DefaultConnection": "Removed"
  }, 
  "AllowedHosts": "*"
}

在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>();

为什么不遵守我的日志级别?

索托

通过将这些设置放在我的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"
      }
    }
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章