Example #1
0
        internal async Task ConfigureTelemetry(PnPContext context)
        {
            // We only configure telemetry and send the init event once per created PnPContextFactory
            if (telemetryInitialized)
            {
                return;
            }

#if NET5_0_OR_GREATER
            if (RuntimeInformation.RuntimeIdentifier == "browser-wasm")
            {
                // Blazor WASM cannot handle the AppInsights NuGet package way of working,
                // hence we're sending just one event to track WASM usage via a manual post
                await SendBlazorInitEventAsync(context).ConfigureAwait(false);

                telemetryInitialized = true;
            }
#endif

            // Populate the Azure AD tenant id
            if (TelemetryManager != null && GlobalOptions != null && !GlobalOptions.DisableTelemetry)
            {
                await context.SetAADTenantId().ConfigureAwait(false);

                // Send a one time "Init" event
                TelemetryManager.LogInitRequest();
            }

            telemetryInitialized = true;
        }
Example #2
0
        private async Task SendBlazorInitEventAsync(PnPContext context)
        {
            try
            {
                // Ensure the tenant id was fetched
                await context.SetAADTenantId().ConfigureAwait(false);

                using (var request = new HttpRequestMessage(HttpMethod.Post, $"https://dc.services.visualstudio.com/v2/track"))
                {
                    // Build payload
                    var body = new
                    {
                        name = "AppEvents",
                        time = DateTime.Now.ToUniversalTime().ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffZ"),
                        iKey = TelemetryManager.InstrumentationKey,
                        data = new
                        {
                            baseType = "EventData",
                            baseData = new
                            {
                                ver        = 2,
                                name       = "PnPCoreInit",
                                properties = new
                                {
                                    PnPCoreSDKVersion = "1.0.0.0",
                                    AADTenantId       = GlobalOptions.AADTenantId.ToString(),
                                    OS = "WASM"
                                }
                            }
                        }
                    };

                    var jsonBody = JsonSerializer.Serialize(body, PnPConstants.JsonSerializer_IgnoreNullValues);

                    using (StringContent content = new StringContent(jsonBody))
                    {
                        // Remove the default Content-Type content header
                        if (content.Headers.Contains("Content-Type"))
                        {
                            content.Headers.Remove("Content-Type");
                        }
                        // Add the batch Content-Type header
                        content.Headers.Add($"Content-Type", "application/x-json-stream");

                        // Connect content to request
                        request.Content = content;

                        HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);

                        if (!response.IsSuccessStatusCode)
                        {
                            Log.LogDebug($"Blazor telemetry failure: {await response.Content.ReadAsStringAsync().ConfigureAwait(false)}");
                        }
                    }
                }
            }
            // Never fail if telemetry is failing
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                Log.LogDebug($"Blazor telemetry failure: {ex}");
            }
        }