0
votes

I have an Angular application running on clients that don't have internet access. :( Obviously no telemetry is sent to Azure.

Does anyone know if ApplicationInsights-JS could be configure to call my .Net Core WebApi which routes the information further on to Azure Application Insights?

1
Have you looked at Application Insights Proxy solutions like this github.com/shaneochotny/ApplicationInsights-JS-Proxy ?Andreas Wendl

1 Answers

0
votes

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();
    }
}