Reputation: 27360
I use asp.net core logging like this:
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);
}
}
An I setup the logger like this:
services.AddApplicationInsightsTelemetry();
loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information)
How do I find the logs for MyClass in Azure portal?
Upvotes: 12
Views: 4950
Reputation: 18556
As far as I understand, you want to find the log entries in Application Insights that are specifically linked to your class MyClass
.
It is in the Property "CategoryName".
Getting Started with Application Insights for ASP.NET Core
Your program.cs should look something like this
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseApplicationInsights()
.Build();
Then 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);
}
If you set it up like this, your ILogger will automatically use the full name of MyClass as a category name, and you will see that in Application Insights under the property "CategoryName".
private void PopulateTelemetry(ITelemetry telemetry,
IReadOnlyList<KeyValuePair<string, object>> stateDictionary, EventId eventId)
{
IDictionary<string, string> dict = telemetry.Context.Properties;
dict["CategoryName"] = this.categoryName;
...
See also this question for an image on how this will look in Application Insights:
Using Application Insights with ILoggerFactory
(Image is taken directly from this answer, please tell me if this is not allowed and I will remove it)
The data is added as a "custom property" and can be filtered like that in the portal:
Some more info: 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
Upvotes: 10