Thomas Segato
Thomas Segato

Reputation: 5265

Setting CustomDimensions with the Microsoft Extension Logging framework

I have set up application insights for asp.net core project.

appsettings.json:

  "ApplicationInsights": {
    "InstrumentationKey": "90f786b7-36a5-xxxx-b1fc-xxxxxxxxx"
  },

Startup.cs:

services.AddApplicationInsightsTelemetry();

And in my controller I can now log:

Logger.LogDebug("LogDebug");

I cant find any information on how to set CustomDemensions. I can see on the overloads there is an object array but not sure if that if for customdimensions. Any pointers?

Upvotes: 2

Views: 1879

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30035

Updated:

            var dict = new Dictionary<string, object> { { "user_name","ivan"},{ "mycity","new york"} };

            using (_logger.BeginScope(dict))
            {
                _logger.LogInformation("this is a test message for testing purpose 456");
            }

Original answer:

For example, if you want to add the 2 properties city / user in customDimentions, please use the code below in the controller:

string city = "london";
string user = "jack";
_logger.LogInformation("{user} sends a  message from {city}", user, city);

Then in azure portal -> application insights logs:

enter image description here

Upvotes: 4

Related Questions