0

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:

enter image description here

But When I look up Application insgiht in AZURE, Userid is empty.

enter image description here

loki
  • 2,926
  • 8
  • 62
  • 115
  • 1
    Does it pass `if (userid != null)`? Have you debugged the app? – Peter Bons Nov 04 '19 at 09:05
  • 1
    When did he create the application insights, and which kind of application insights did he choose? There are some changes in application insights creation, and in the previous, when you create application insights, there're type for javascript / asp.net etc. – Ivan Glasenberg Nov 04 '19 at 09:32
  • @PeterBons yes. I passed it. No worry. – loki Nov 04 '19 at 10:25
  • @IvanYang I am using "Microsoft.ApplicationInsights;" as nuget pakage. – loki Nov 04 '19 at 10:25
  • @IvanYang I am using App insight in a really simple way. because if I create simple web app core. Everything working perfect! – loki Nov 04 '19 at 10:28
  • 1
    @loki, when you run the .net core 2.2 project locally, like in visual studio, in the visual studio output window or application insights search window, can you see the userid is changed? – Ivan Glasenberg Nov 05 '19 at 01:24
  • @IvanYang, yes I see userid is changed! exactly you are wright. – loki Nov 05 '19 at 06:46
  • @loki, what do you mean "exactly you are right"? and now it's working well? – Ivan Glasenberg Nov 05 '19 at 06:47
  • @IvanYang, User id is existed when I look at Appinsight Search, But there is no value in azure. Not saving. I updated my question also. please look at above . – loki Nov 05 '19 at 07:09
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/201870/discussion-between-ivan-yang-and-loki). – Ivan Glasenberg Nov 05 '19 at 07:20

0 Answers0