This wasn't as tricky as I first expected.
Creating a controller in my Web Api which routes the message further on to Application Insights seems to work very well.
Angular App
I'm using the package @microsoft/applicationinsights-web.
Then in the constructor of the service handling the communication to Application Insights I place this code where the endpointUrl is pointing to my controller method.
this.appInsights = new ApplicationInsights({
config: {
instrumentationKey: environment.appInsights.instrumentationKey,
endpointUrl: environment.apiUrl + '/api/ai-tracker',
enableCorsCorrelation: true
}
});
Web Api
Here's the code in the Web Api that routes trace-message further on to 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();
}
}