Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OFFI-92: Adding defaults for hosting-related configuration #89

Merged
merged 9 commits into from
Aug 6, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
using Microsoft.ApplicationInsights.DependencyCollector;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.ApplicationInsights.Extensibility.PerfCounterCollector.QuickPulse;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Linq;
using ApplicationInsightsFeatureIds = Lombiq.Hosting.Azure.ApplicationInsights.Constants.FeatureIds;
Expand All @@ -17,6 +20,49 @@ namespace Microsoft.Extensions.DependencyInjection;

public static class ApplicationInsightsInitializerExtensions
{
/// <summary>
/// Lombiq-recommended opinionated default configuration for features of an Orchard Core application hosted in
/// Azure, with Application Insights telemetry. If any of the configuration values exist, they won't be overridden,
/// so e.g. appsettings.json configuration will take precedence.
/// </summary>
/// <param name="webApplicationBuilder">The <see cref="WebApplicationBuilder"/> instance of the app.</param>
/// <param name="hostingConfiguration">Configuration for the hosting defaults.</param>
public static OrchardCoreBuilder ConfigureAzureHostingDefaultsWithApplicationInsightsTelemetry(
this OrchardCoreBuilder builder,
WebApplicationBuilder webApplicationBuilder,
AzureHostingConfiguration hostingConfiguration = null)
{
builder.ConfigureAzureHostingDefaults(webApplicationBuilder, hostingConfiguration);

var logLevelSection = webApplicationBuilder.Configuration.GetSection("Logging:ApplicationInsights:LogLevel");

logLevelSection.AddValueIfKeyNotExists("Default", "Warning");

var ocAppInsightsSection = webApplicationBuilder.Configuration.GetSection("OrchardCore:Lombiq_Hosting_Azure_ApplicationInsights");

ocAppInsightsSection
.AddValueIfKeyNotExists("EnableUserNameCollection", "true")
.AddValueIfKeyNotExists("EnableUserAgentCollection", "true")
.AddValueIfKeyNotExists("EnableIpAddressCollection", "true");

if (webApplicationBuilder.Environment.IsDevelopment())
{
ocAppInsightsSection.AddValueIfKeyNotExists("EnableLoggingTestMiddleware", "true");

var appInsightsSection = webApplicationBuilder.Configuration.GetSection("ApplicationInsights");

appInsightsSection.AddValueIfKeyNotExists("EnableDependencyTrackingTelemetryModule", "false");
}
else
{
ocAppInsightsSection.AddValueIfKeyNotExists("EntraAuthenticationType", "ManagedIdentity");
}

builder.AddOrchardCoreApplicationInsightsTelemetry(webApplicationBuilder.Configuration);

return builder;
}

/// <summary>
/// Initializes Application Insights for Orchard Core. Should be used in the application Program.cs file.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ builder.Services

Note that due to how the Application Insights .NET SDK works, telemetry can only be collected for the whole app at once; collecting telemetry separately for each tenant is not supported.

You can also use `ConfigureAzureHostingDefaultsWithApplicationInsightsTelemetry` instead; this sets up all the recommended hosting configuration with `ConfigureAzureHostingDefaults` from [Lombiq Helpful Libraries - Orchard Core Libraries](https://github.com/Lombiq/Helpful-Libraries/blob/dev/Lombiq.HelpfulLibraries.OrchardCore/Readme.md).

When using the full CMS approach of Orchard Core (i.e. not decoupled or headless) then the client-side tracking script will be automatically injected as a head script. Otherwise, you can create it with `ITrackingScriptFactory`.

### Advanced configuration
Expand Down