简体   繁体   中英

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 :

在此处输入图片说明

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}?

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM