diff --git a/Directory.Packages.props b/Directory.Packages.props index a3fa5a87a..e18362d52 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,6 +4,10 @@ true + + + + diff --git a/NuGet.config b/NuGet.config index 689748dac..887291f11 100644 --- a/NuGet.config +++ b/NuGet.config @@ -11,6 +11,7 @@ + @@ -25,5 +26,9 @@ + + + + diff --git a/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj b/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj index a986414a9..bf4a33e93 100644 --- a/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj +++ b/src/Microsoft.TryDotNet/Microsoft.TryDotNet.csproj @@ -10,6 +10,8 @@ + + diff --git a/src/Microsoft.TryDotNet/Program.cs b/src/Microsoft.TryDotNet/Program.cs index 00c5746bf..ed4a23cde 100644 --- a/src/Microsoft.TryDotNet/Program.cs +++ b/src/Microsoft.TryDotNet/Program.cs @@ -5,6 +5,8 @@ using System.Reflection; using System.Text; using System.Text.Json; +using Azure.Monitor.OpenTelemetry.AspNetCore; +using Azure.Monitor.OpenTelemetry.Profiler; using Microsoft.DotNet.Interactive; using Microsoft.DotNet.Interactive.Connection; using Microsoft.DotNet.Interactive.CSharpProject; @@ -22,6 +24,8 @@ namespace Microsoft.TryDotNet; public class Program { + private const string AppInsightsConnectionStringEnvVar = "APPLICATIONINSIGHTS_CONNECTION_STRING"; + private static Prebuild? _consolePrebuild; private static bool _loggingEnabled; @@ -29,6 +33,13 @@ public static async Task Main(string[] args) { StartLogging(); + if (string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(AppInsightsConnectionStringEnvVar))) + { + Console.WriteLine( + $"WARNING: {AppInsightsConnectionStringEnvVar} is not set. " + + "Application Insights telemetry and Profiler will be inactive."); + } + await EnsurePrebuildIsReadyAsync(); var app = await CreateWebApplicationAsync(new WebApplicationOptions { Args = args }); @@ -64,6 +75,13 @@ public static async Task CreateWebApplicationAsync(WebApplicatio }); }); + if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(AppInsightsConnectionStringEnvVar))) + { + builder.Services.AddOpenTelemetry() + .UseAzureMonitor() + .AddAzureMonitorProfiler(); + } + builder.Services.AddResponseCompression(compressionOptions => { compressionOptions.EnableForHttps = true; diff --git a/src/Microsoft.TryDotNet/appsettings.json b/src/Microsoft.TryDotNet/appsettings.json index 10f68b8c8..45735af07 100644 --- a/src/Microsoft.TryDotNet/appsettings.json +++ b/src/Microsoft.TryDotNet/appsettings.json @@ -3,6 +3,13 @@ "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" + }, + "OpenTelemetry": { + "LogLevel": { + "Default": "Warning", + "Azure.Monitor.OpenTelemetry.Profiler": "Information", + "Microsoft.ServiceProfiler": "Information" + } } }, "AllowedHosts": "*"