部署在Azure上时,application insights中没有数据
我有一个.net core 2 MVC web应用程序,它使用Azure上的应用程序洞察。 我还将nlog配置为使用应用程序洞察进行跟踪。 一切都在我的电脑上运行,因为我在azure上发现了异常和跟踪,但当我在azure上部署应用程序并使用它时,它不会在application insights上生成任何事件(我只在日志文件中找到了事件) 因此,我尝试在控制器中创建TelemetryClient实例,它甚至在部署的实例中也能工作:部署在Azure上时,application insights中没有数据,azure,azure-application-insights,Azure,Azure Application Insights,我有一个.net core 2 MVC web应用程序,它使用Azure上的应用程序洞察。 我还将nlog配置为使用应用程序洞察进行跟踪。 一切都在我的电脑上运行,因为我在azure上发现了异常和跟踪,但当我在azure上部署应用程序并使用它时,它不会在application insights上生成任何事件(我只在日志文件中找到了事件) 因此,我尝试在控制器中创建TelemetryClient实例,它甚至在部署的实例中也能工作: TelemetryClient tc = new Telemetr
TelemetryClient tc = new TelemetryClient()
{
InstrumentationKey = "11111111-2222-3333-4444-555555555555"
};
tc.Context.User.Id = Environment.MachineName;
tc.Context.Session.Id = "session_id";
tc.Context.Device.OperatingSystem = Environment.OSVersion.ToString();
tc.TrackTrace("Privacy page says hello with TelemetryClient");
以下是我的项目片段:
appsettings.json
{
"ApplicationInsights": {
"InstrumentationKey": "11111111-2222-3333-4444-555555555555"
}
}
appsettings.Staging.json
{
"ConnectionStrings": {
"DefaultConnection": "Server=tcp:dom.database.windows.net,1433;Initial Catalog=dom;Persist Security Info=False;User ID=user;Password=pass;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
},
"AllowedHosts": "*",
"Logging": {
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
}
}
我在我的VS和Azure(登台)上定义了相同的ASPNETCORE_环境值,以确保加载相同的appsettings并部署所有文件
我以这种方式加载配置
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.AddEnvironmentVariables()
.Build();
CreateWebHostBuilder是这样的吗
public static IWebHostBuilder CreateWebHostBuilder(string[] args, IConfiguration config) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext) =>
{
//config.AddJsonFile("appsettings.json");
})
.UseStartup<Startup>()
.ConfigureLogging(
logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseApplicationInsights() // Enable Application Insights
.UseNLog();
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]参数,IConfiguration配置)=>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext)=>
{
//config.AddJsonFile(“appsettings.json”);
})
.UseStartup()
.配置日志记录(
日志=>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.logging.LogLevel.Trace);
})
.UseApplicationInsights()//启用应用程序洞察
.UseNLog();
nlog.config包含
<extensions>
<add assembly="Microsoft.ApplicationInsights.NLogTarget" />
</extensions>
<targets>
<target type="ApplicationInsightsTarget" name="aiTarget" />
<target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="*" minlevel="Warn" writeTo="aiTarget" />
<logger name="*" minlevel="Warn" writeTo="f" />
</rules>
在我看来,配置或InstrumentationKey中似乎有问题,但我不知道如何检查它
有什么想法吗,或者。。。有没有办法知道application insights库是如何配置的,以便找到一些有用的信息来解决问题?我尝试了远程调试,但我不知道检查什么。根据您的描述,我认为您已经在azure portal->web应用程序->配置->应用程序设置中设置了另一个application insights密钥 请检查您是否这样做: 如果钥匙在那里,你需要取下它。或者将这行代码
AddEnvironmentVariables()
放在AddJsonFile()
之前,如下所示:
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables() //put this before AddJsonFile()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.Build();
如果您有更多问题,请告诉我。根据您的描述,我认为您已经在azure portal->web应用程序->配置->应用程序设置中设置了另一个application insights密钥 请检查您是否这样做: 如果钥匙在那里,你需要取下它。或者将这行代码
AddEnvironmentVariables()
放在AddJsonFile()
之前,如下所示:
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables() //put this before AddJsonFile()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{environmentName}.json", optional: true)
.Build();
如果您有更多问题,请告诉我。您提到过吗。您提到过吗。是的@IvanYang,这就是问题所在。我删除了密钥APPINSIGHTS\u INSTRUMENTATIONKEY,现在它可以工作了!:-)是的@IvanYang,这就是问题所在。我删除了密钥APPINSIGHTS\u INSTRUMENTATIONKEY,现在它可以工作了!:-)