JleruOHeP
JleruOHeP

Reputation: 10386

Application Insights Logs custom properties

I want to have a shared app insights instance that will hold all logs from different microservices running.

For each of them I am adding

        services.AddLogging(
            loggingBuilder =>
            {
                loggingBuilder
                .SetMinimumLevel(settings.LogLevel)
                .AddApplicationInsights();
            }
        );

But then in the azure portal I want to be able to search for logs for individual applications, for example using query like "applicationName = 'MyAppName'".

Is it possible to setup loggingBuilder to add in this custom property applicationName to be MyAppName?

Logging itself is done like

public void MyMethod()
{
    try
    {
      //whatever
    }
    catch (Exception ex)
    {
      logger.LogError(ex, "Meaningful information");
    }
} 

Or is it a bad idea in general to share app insights instance and have all logs and telemetry available in 1 pile?

Upvotes: 2

Views: 2288

Answers (1)

Vova Bilyachat
Vova Bilyachat

Reputation: 19514

You can use ITelemetryInitializer to set role name

    public class MyTelemetryInitializer : ITelemetryInitializer
    {
        public void Initialize(ITelemetry telemetry)
        {
            if (string.IsNullOrEmpty(telemetry.Context.Cloud.RoleName))
            {
                //set custom role name here
                telemetry.Context.Cloud.RoleName = "RoleName";
            }
        }
    }

Then if its .net core register it

  services.AddSingleton<ITelemetryInitializer>(new MyTelemetryInitializer ());

Upvotes: 2

Related Questions