【问题标题】:Using ApplicationInsights-JS from client without internet access. Is it possible to send telemetry through Web API?在没有 Internet 访问权限的情况下从客户端使用 ApplicationInsights-JS。是否可以通过 Web API 发送遥测数据?
【发布时间】:2020-11-27 08:40:34
【问题描述】:
我有一个 Angular 应用程序在无法访问互联网的客户端上运行。 :(
显然没有遥测数据发送到 Azure。
有谁知道 ApplicationInsights-JS 是否可以配置为调用我的 .Net Core WebApi,它将信息进一步路由到 Azure Application Insights?
【问题讨论】:
标签:
javascript
angular
azure-application-insights
【解决方案1】:
这并不像我最初预期的那样棘手。
在我的 Web Api 中创建一个控制器,将消息进一步路由到 Application Insights 似乎效果很好。
Angular 应用
我正在使用包@microsoft/applicationinsights-web。
然后在处理与 Application Insights 通信的服务的构造函数中,我将此代码放置在 endpointUrl 指向我的控制器方法的位置。
this.appInsights = new ApplicationInsights({
config: {
instrumentationKey: environment.appInsights.instrumentationKey,
endpointUrl: environment.apiUrl + '/api/ai-tracker',
enableCorsCorrelation: true
}
});
网络接口
这是 Web Api 中将跟踪消息进一步路由到 Application Insights 的代码。
[Route("api/ai-tracker")]
[ApiController]
public class ApplicationInsightsTrackController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> PostToApplicationInsights(dynamic message)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://dc.services.visualstudio.com/v2/track/");
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
string content = message.ToString();
var request = new HttpRequestMessage(HttpMethod.Post, client.BaseAddress)
{
Content = new StringContent(content, Encoding.UTF8, "application/json")
};
await client.SendAsync(request);
}
return Ok();
}
}