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?
View logs in Application InsightsGo to Application Insights resource in your resource group. Go to Logs under Monitoring section. Click on traces eye button to get log traces. Select Time Range and click Run.
Diagnose exceptions using Visual StudioOpen the Application Insights Search telemetry window in Visual Studio. While debugging, select the Application Insights dropdown box. Select an exception report to show its stack trace. To open the relevant code file, select a line reference in the stack trace.
Trace telemetry (in Application Insights) represents printf style trace statements that are text-searched. Log4Net , NLog , and other text-based log file entries are translated into instances of this type. The trace does not have measurements as an extensibility.
Application Insights captures and sends ILogger logs by using the same TelemetryConfiguration information that's used for every other telemetry. But there's an exception.
ApplicationInsightsLoggerProvider is an implementation of ILoggerProvider, which is responsible for providing ILogger and ILogger<TCategoryName> implementations. To add Application Insights telemetry to ASP.NET Core applications, use the Microsoft.ApplicationInsights.AspNetCore NuGet package.
The ASP.NET Core ILogger infra has a built-in mechanism to apply log filtering. This lets you control the logs that are sent to each registered provider, including the Application Insights provider. The filtering can be done either in configuration (typically by using an appsettings.json file) or in code.
But data captured using ILogger will only be sent to the subscribing customer’s resource. You will only be able to see data captured for your own environments when you have Application Insights enabled. ILogger is a common interface for capturing log information.
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".
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;
...
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://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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With