在Appinsight请求日志中修改userId时遇到很大麻烦。如果我在下面的代码下创建新的Appinsight环境,效果很好!但是,如果我在现有应用程序中使用以下代码,则不能更改UserId或其他REQUEST属性或PageView属性。我之前的开发人员使用向导创建了appinsight。我不知道为什么我不能更改用户ID值。我该如何修改?是否有任何配置文件可用于更改修改appinsight上下文的属性。我的项目是Asp.net core 2.2。我正在使用“ microsoft.ApplicationInsights;”作为nuget包。
StreamReader sr = new StreamReader(@"c:\test.txt",Encoding.UTF8);
int richTextBoxCursor = 0;
while (!sr.EndOfStream){
richTextBoxCursor = richTextBox.TextLength;
string line = sr.ReadLine();
line = line.Replace(Convert.ToChar(0x0).ToString(),"NUL");
richTextBox.AppendText(line);
i = 0;
while(true){
i = line.IndexOf("NUL",i) ;
if(i == -1) break;
// This specific select function select text start from a certain start index to certain specified character range passed as second parameter
// i is the start index of each found "NUL" word in our read line
// 3 is the character range because "NUL" word has three characters
richTextBox.Select(richTextBoxCursor + i,3);
richTextBox.SelectionColor = Color.White;
richTextBox.SelectionBackColor = Color.Black;
i++;
}
}
Startup.cs:
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);
}
}
}
Program.cs:
public void ConfigureServices(IServiceCollection services)
{
......
.......
services.AddSingleton<ITelemetryInitializer,MyTelemetryInitializer>();
services.AddSession();
....
}
如果我查看“ Application Insights搜索”,则可以看到Userid已更改:
但是当我在AZURE中查找Application Insgiht时,用户ID为空。