如何查找特定ILogger <t>的应用程序洞察日志

时间:2018-03-26 10:55:10

标签: azure asp.net-core azure-web-sites azure-application-insights

我使用asp.net核心日志记录:

public class MyClass
{
    private readonly ILogger<MyClass> _logger;
    public readonly EventId NoEntryFoundEventId = new EventId(1, "No Entry Found");

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

    public void Foo(decimal entryId)
    {
        _logger.LogError(NoEntryFoundEventId, "MyCustomMessage\t: Entry ID: {EntryId}", entryId);         
    }
}

我设置了这样的记录器:

services.AddApplicationInsightsTelemetry();

loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information)

如何在Azure门户中找到MyClass的日志?

1 个答案:

答案 0 :(得分:5)

据我了解,您希望在Application Insights中找到专门链接到您的班级MyClass的日志条目。

它位于Property“CategoryName”中。

Getting Started with Application Insights for ASP.NET Core

你的program.cs看起来应该是这样的

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

然后link the ASP.NET ILogger to Application Insights

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
    ILoggerFactory loggerFactory)
{
/*...existing code..*/
        loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Warning);
}

如果你这样设置,你的ILogger会自动使用MyClass的全名作为类别名称,你会在Application Insights的“CategoryName”属性下看到它。

https://github.com/Microsoft/ApplicationInsights-aspnetcore/tree/develop/src/Microsoft.ApplicationInsights.AspNetCore/Logging/Implementation

private void PopulateTelemetry(ITelemetry telemetry, 
   IReadOnlyList<KeyValuePair<string, object>> stateDictionary, EventId eventId)
    {
        IDictionary<string, string> dict = telemetry.Context.Properties;
        dict["CategoryName"] = this.categoryName;
...

另请参阅此问题,了解有关Application Insights中的外观的图片: Using Application Insights with ILoggerFactory (图片直接取自这个答案,如果不允许,请告诉我,我会将其删除)

数据作为“自定义属性”添加,可以像门户中的那样进行过滤: enter image description here

更多信息: https://docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#properties https://docs.microsoft.com/en-us/azure/application-insights/app-insights-analytics-tour#custom-properties-and-measurements