9

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?

Liero
  • 25,216
  • 29
  • 151
  • 297
  • 1
    Unclear what you're asking: What type is ILogger? – Thibault D. Apr 06 '18 at 13:59
  • Oh, sorry, I've included to logger registration in the question: `loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Information)` – Liero Apr 08 '18 at 07:58

1 Answers1

8

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://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

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • Hi, this explains how the log looks like, but could you elaborate on how to find it (filter by CategoryName) in Azure Portal? – Liero Apr 08 '18 at 07:42