Send cloud role name to appinsight using serilog

juan maximiliano aguilar abanto 541 Reputation points
2023-02-23T17:02:04.1333333+00:00

I use serilog to send traces to appinsights

this is my code

 Log.Logger = new LoggerConfiguration()
                    .Enrich.WithProperty("AppName", Assembly.GetExecutingAssembly().GetName().Name)
                    .Enrich.FromLogContext()
                    .Enrich.WithClientIp()
                    .Enrich.WithCorrelationIdHeader()
                    .WriteTo.ApplicationInsights(telemetryConfiguration, new SerilogAppInsightsTraceTelemetryConverter(), Serilog.Events.LogEventLevel.Information)

But I dont see cloud_rolename in appinsights

User's image

I registered this telemetry, but it doesnt send the cloud_rolename to azure

services.AddSingleton<ITelemetryInitializer,GlobalTelemetryInitializer> ();

This is my telemetry class

 public class GlobalTelemetryInitializer : ITelemetryInitializer
    {
        public const string appName = "app-Mkt";
        public const string appNameValue = "Configuration";
        public void Initialize(ITelemetry telemetry)
        {
            telemetry.Context.Cloud.RoleName = $"ctp-mkt-{appNameValue.ToLower()}";
        }
    }
Azure Monitor
Azure Monitor
An Azure service that is used to collect, analyze, and act on telemetry data from Azure and on-premises environments.
3,310 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,604 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ryan Hill 28,386 Reputation points Microsoft Employee
    2023-03-03T16:34:50.1933333+00:00

    Hi @juan maximiliano aguilar abanto ,

    I don't see anything wrong with your GlobalTelemetryInitializer. I also walked over to Serilog Application Insights Sinks and I see that your code snippets came from here. Honestly, I assume the Serilog SDK should pull ITelemetryInitializer from the IoC container and that isn't happening in your case. It could be a bug in Serilog but to work around it, you can definitely include builder.Services.AddApplicationInsightsTelemetry() and that should pick up your RoleName. I didn't add Serilog to my project but in my setup, I can see the role name being updated in the requests table.

    User's image

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

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.