0

I would like to add custom property to application insight which is coming in applicaion header. Is there a way to do without making much changes in existing code?

I have configured the loggin in my .net core application as below.

`services.AddLogging(options => { options.AddFilter("", _logLevel);

            // pass the InstrumentationKey provided under the appsettings
            options.AddApplicationInsights(Configuration["AppInsightInstrumentKey"]);
        });`

And to write the logs I have called the function as private readonly ILogger<MyController> _logger; _logger.LogError(ex.ToString());

I would like to add custom property to application insight which is coming in applicaion header. Is there a way to do without making much changes in existing code?

i would like to use same method _logger.LogError and it should add customer properties as well.

1 Answers1

0
  • Using ITelemetryInitializer, it is possible to add additional properties to an existing request and git reference ApplicationInsightsRole with package Microsoft.Extensions.Logging.ApplicationInsights.

enter image description here

   _logger = new TelemetryClient(configuration);

       
        Person user = new Person() { FirstName = "Cat", LastName = "Lady" };

    
        Dictionary<string, string> customProperties = new Dictionary<string, string>()
        {
            { "FirstName", user.FirstName },
            { "LastName", user.LastName }
        };

        TrackTrace("User details", SeverityLevel.Information, customProperties);

        telemetryClient.TrackTrace("User details", SeverityLevel.Information, customProperties);

For Exception:

 _logger.TrackException(telemetry);

enter image description here

enter image description here

  • More details with Custom properties for each request in Application Insights metrics SO and logging
Sampath
  • 810
  • 2
  • 2
  • 13
  • Where does TrackException come from? – Enrico Jul 14 '23 at 10:14
  • I used TrackException we can use also ```_logger.LogError(ex );``` – Sampath Jul 14 '23 at 10:18
  • Yes but ex should be an exception object? How would that work with a dictionary? – Enrico Jul 14 '23 at 10:25
  • I wrote down the working methods I found in my answer here:https://stackoverflow.com/a/76686706/7064454 If you have any other ideas, let me know. – Enrico Jul 14 '23 at 10:28
  • TrackException is a method of the TelemetryClient class in the Application Insights SDK[1](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-monitor/app/api-custom-events-metrics.md). It is used to track exceptions in the application. When we call the TrackException method, it sends telemetry data to Application Insights that includes information about the exception, such as the exception message, stack trace, and custom properties[2](https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/azure-monitor/app/asp-net-exceptions.md). – Sampath Jul 14 '23 at 10:37
  • _logger.The ILogger interface in .NET Core has a function called LogError(ex). An exception object and an error message are both logged using this method. The error message and exception object are recorded to the logging system set up in your application when you call the _logger.LogError method. – Sampath Jul 14 '23 at 10:37
  • TrackException or _logger are both options.Use LogError to record exceptions in your program. Use the TelemetryClient class and the TrackException function to add custom properties to the telemetry data by passing a Dictionarystring, string> parameter that contains the custom properties. You can use the ILogger to add unique attributes to the log messages.Pass a Dictionarystring, object> parameter with the custom properties[1] to the BeginScope function. – Sampath Jul 14 '23 at 10:37
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254496/discussion-between-sampath-and-enrico). – Sampath Jul 14 '23 at 10:37