I have a big trouble for modifying userId in Appinsight request logs. If I create new Appinsight environment below code working great! But if I use below code in existed application, UserId or other REQUEST properties or PageView Properties can not changed. A developer before me created appinsight by using Wizard. I have no Idea why I couldn't change userid value. How can I modify it? Is there any config file for changing modification appinsight Context's Properties. My Project is Asp.net core 2.2. I am using "Microsoft.ApplicationInsights;" as nuget package.
public class MyTelemetryInitializer : ITelemetryInitializer
{
public MyTelemetryInitializer()
{
}
public void Initialize(ITelemetry telemetry)
{
try
{
telemetry.Context.User.Id = "yusuf"; //=> NOT WORKING!!
telemetry.Context.Cloud.RoleInstance = "test"; //=> NOT WORKING!!
telemetry.Context.Properties["EventId"] = "deneme";
ISupportProperties propTelemetry = (ISupportProperties)telemetry;
if (propTelemetry.Properties.ContainsKey("EventId"))
{
propTelemetry.Properties["EventId"] = "deneme2"; //=> Working!!!
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
}
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
......
.......
services.AddSingleton<ITelemetryInitializer,MyTelemetryInitializer>();
services.AddSession();
....
}
Program.cs:
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseApplicationInsights()
.UseStartup<Startup>()
.Build();
}
if I look at "Application Insights Search", I can see Userid is changed:
But When I look up Application insgiht in AZURE, Userid is empty.