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的日志?
据我所知,您希望在Application Insights中找到专门链接到您的类的日志条目MyClass.
它位于Property"CategoryName"中.
你的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"中看到它.
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-的见解,分析游#自定义特性和-测量
| 归档时间: |
|
| 查看次数: |
1250 次 |
| 最近记录: |