【问题标题】:Where in Azure Portal are the logger.Log statements?Azure 门户中的 logger.Log 语句在哪里?
【发布时间】:2020-02-18 16:42:36
【问题描述】:

我有一个 .NET Core WebAPI 应用程序。该应用作为应用服务部署在 Azure 上。

在代码中,我像这样启用了 Application Insights

public static IWebHost BuildWebHost(string[] args) =>
    WebHost
    .CreateDefaultBuilder(args)
    .UseApplicationInsights()
    .UseStartup<Startup>()
    .ConfigureLogging((hostingContext, logging) =>
    {                      
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging")).SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Error);  
        logging.AddApplicationInsights(" xxxxx-xxxx-xxxx-xxxx--xxxxxxxxxxx").SetMinimumLevel(LogLevel.Trace);
    })
    .Build();

在控制器的构造函数和控制器的方法中,我有这些日志记录语句。

_logger.LogInformation("ApiController, information");
_logger.LogWarning("ApiController, warning");
_logger.LogCritical("ApiController, critical");
_logger.LogWarning("ApiController, error");
_logger.LogDebug("ApiController, debug");

在 Azure 门户中,我为我的应用服务启用了 Application Insights。这是来自门户的图片。

App Insights in Azure Portal

但是我在 Azure 门户中的哪里可以看到日志记录语句?

当我转到Application Insights -&gt; Logs 并查询时

search *

我可以看到对 API 的请求,但看不到日志语句。

Application Insights Log

日志语句在哪里?

【问题讨论】:

标签: azure logging .net-core azure-application-insights webapi


【解决方案1】:

首先,在代码中配置日志级别并不是一个好习惯。您可以在appsettings.json 文件中轻松配置日志级别。所以在Program.cs -> public static IWebHost BuildWebHost 方法中,将代码改成如下:

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseApplicationInsights()
                .UseStartup<Startup>()
                .Build();

然后在appsettings.json(也右键文件->属性->设置“复制到输出目录”为“如果较新则复制”):

{
  "Logging": {
    "IncludeScopes": false,
    "ApplicationInsights": {
      "LogLevel": {
        "Default": "Trace"
      }
    },
    "Console": {
      "LogLevel": {
        "Default": "Warning"
      }
    }
  },
  "ApplicationInsights": {
    "InstrumentationKey": "the key"
  }
}

在控制器中,如ValuesController:

    public class ValuesController : Controller
    {
        private readonly ILogger _logger;
        public ValuesController(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<ValuesController>();
        }

        // GET api/values
        [HttpGet]
        public IEnumerable<string> Get()
        {
            _logger.LogInformation("ApiController, information");
            _logger.LogWarning("ApiController, warning");
            _logger.LogCritical("ApiController, critical");
            _logger.LogWarning("ApiController, error");
            _logger.LogDebug("ApiController, debug");

            return new string[] { "value1", "value2" };
        }
     }

运行项目,然后等待几分钟(应用洞察总是需要 3 到 5 分钟或更长时间才能显示数据)。然后进入 azure 门户 -> 应用程序洞察日志,记住 ILogger 写入的所有日志都存储在“traces”表中。只需编写“traces”之类的查询并指定适当的时间范围,您应该会看到如下所有日志:

【讨论】:

    猜你喜欢
    • 2012-12-26
    • 2018-08-25
    • 2016-03-28
    • 2021-12-17
    • 2018-05-14
    • 2017-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    相关资源
    最近更新 更多