gpt4 book ai didi

azure - 如何查找特定 ILogger 的应用程序见解日志

转载 作者:行者123 更新时间:2023-12-04 12:03:37 28 4
gpt4 key购买 nike

我像这样使用 asp.net core 日志记录:

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 的日志?

最佳答案

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

它位于属性“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://learn.microsoft.com/en-us/azure/application-insights/app-insights-api-custom-events-metrics#properties https://learn.microsoft.com/en-us/azure/application-insights/app-insights-analytics-tour#custom-properties-and-measurements

关于azure - 如何查找特定 ILogger<T> 的应用程序见解日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49489779/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com