I cannot see my custom properties being logged to application insights only the message. Does it need additional setup?

Valeriy Sudakov 20 Reputation points
2023-07-11T12:34:44.8166667+00:00

Doing something like that

Dictionary<string, string> properties = new Dictionary<string, string>()
{
     { "Prop1", "abc" },
     { "Prop2", "def" },
};
 _logger.LogInformation("CustomProperties", properties); //Logs only the string, dictionary content missing completely

//In Program.cs

builder.Services.AddApplicationInsightsTelemetry();

Appsettings.json

{

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,741 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,841 Reputation points Microsoft Vendor
    2023-07-12T01:33:37.3733333+00:00

    Hi @Valeriy Sudakov

    _logger.LogInformation("CustomProperties", properties);

    The issue relates the use of LoggerExtensions.LogInformation Method, to formats and writes an informational log message. You can change your code as below:

                Dictionary<string, string> properties = new Dictionary<string, string>()
                {
                     { "Prop1", "abc" },
                     { "Prop2", "def" },
                };
                _logger.LogInformation("CustomProperties {properties}", properties);
    

    Then the result as below:

    User's image

    Or you can serialize the dictionary to a Json string, then log the result:

                var logmessage = System.Text.Json.JsonSerializer.Serialize(properties);
    
                _logger.LogInformation($"CustomProperties: {logmessage}"); 
    

    The output as below:

    User's image


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 69,501 Reputation points
    2023-07-11T15:14:14.44+00:00

    see docs:

    https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerextensions.loginformation?view=dotnet-plat-ext-7.0

    _logger.LogInformation("CustomProperties {Prop1}, {Prop2}",  
         properties["Prop1", 
         properties["Prop2"]);
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.