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

Lie*_*ero 7 azure azure-web-sites azure-application-insights asp.net-core

我像这样使用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);         
    }
}
Run Code Online (Sandbox Code Playgroud)

我设置了这样的记录器:

services.AddApplicationInsightsTelemetry();

loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information)
Run Code Online (Sandbox Code Playgroud)

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

Ale*_*AIT 6

据我所知,您希望在Application Insights中找到专门链接到您的类的日志条目MyClass.

它位于Property"CategoryName"中.

ASP.NET核心应用程序洞察入门

你的program.cs应该是这样的

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

然后将ASP.NET ILogger链接到Application Insights

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

如果你这样设置,你的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;
...
Run Code Online (Sandbox Code Playgroud)

另请参阅此问题,以获取有关Application Insights中的外观的图像: 将Application Insights与ILoggerFactory配合使用 (图片直接取自这个答案,如果不允许,请告诉我,我会将其删除)

数据作为"自定义属性"添加,可以像门户中的那样进行过滤: 在此输入图像描述

更多信息:https : //docs.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#properties https://docs.microsoft.com/en-us /天蓝色/应用程序的见解/ APP-的见解,分析游#自定义特性和-测量