Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope Logging in Application Insight

We have a .net core web api and we have used inbuild logger available in Microsoft.Extensions.Logging namespace.

We have integrated this logger with Application Insight.

I can see all the logs getting logged correctly. However, I am not able to see the logging information of scope

Below is my code:

var taskId = Guid.NewGuid();
            using (logger.BeginScope("Assigning Task {taskId}.",taskId))
            {
               logger.LogInformation("{taskId} is assigned",taskId);
            }

I can see this output where scope log information is matained in '{Original Format}'. However, structured logging is not working for that field :

enter image description here

I have below questions:

  1. How does scope logging works in Application Insight?
  2. Is this the only way to see the scope information?
  3. Is there any way to see all the logs under one scope?
  4. Why structured logging is not working for {Original Format}?
like image 702
Mangesh Kulkarni Avatar asked Dec 08 '25 09:12

Mangesh Kulkarni


1 Answers

Scopes logging works nowadays in Application Insights (I'm using version 2.19.0). Instead of passing a format string + params to BeginScope() you should pass a dictionary of name-value pairs, e.g.

using (logger.BeginScope(new Dictionary<string, object> 
         { { "TaskId" = taskId }, { "Action", "AssigningTask" } }))
{
    .. 
    logger.LogInformation("{taskId} is assigned", taskId);
    ...
}

Here putting the taskId in the inner log message is a bit redundant, of course.

like image 160
Rory Avatar answered Dec 10 '25 21:12

Rory