Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find application insights logs for specific ILogger<T>

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?

like image 313
Liero Avatar asked Mar 26 '18 10:03

Liero


People also ask

How do I find application insights logs?

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.

How do I log exceptions in application insights?

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.

What is trace in application insight?

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.

How does application insights capture and send ILogger logs?

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.

What is applicationinsightsloggerprovider?

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.

How do I filter logs in ILogger infra?

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.

Can I see data captured using ILogger for my own environment?

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.


Video Answer


1 Answers

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: enter image description here

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

like image 143
Alex AIT Avatar answered Oct 16 '22 20:10

Alex AIT