Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Add Azure application insights

supportcbe4e4ed6b67
diff --git a/src/Core/Grand.Infrastructure/Configuration/ApplicationInsightsConfig.cs b/src/Core/Grand.Infrastructure/Configuration/ApplicationInsightsConfig.cs
new file mode 100644
index 000000000..b09683ef4
--- /dev/null
+++ b/src/Core/Grand.Infrastructure/Configuration/ApplicationInsightsConfig.cs
@@ -0,0 +1,7 @@
+namespace Grand.Infrastructure.Configuration
+{
+ public partial class ApplicationInsightsConfig
+ {
+ public bool Enabled { get; set; }
+ }
+}
diff --git a/src/Core/Grand.Infrastructure/StartupBase.cs b/src/Core/Grand.Infrastructure/StartupBase.cs
index 92ca3f250..bffe3fb0c 100644
--- a/src/Core/Grand.Infrastructure/StartupBase.cs
+++ b/src/Core/Grand.Infrastructure/StartupBase.cs
@@ -213,6 +213,7 @@ namespace Grand.Infrastructure
services.StartupConfig<DatabaseConfig>(configuration.GetSection("Database"));
services.StartupConfig<AmazonConfig>(configuration.GetSection("Amazon"));
services.StartupConfig<AzureConfig>(configuration.GetSection("Azure"));
+ services.StartupConfig<ApplicationInsightsConfig>(configuration.GetSection("ApplicationInsights"));
InitDatabase(services, configuration);
diff --git a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
index d3f78fbe0..4b7c93234 100644
--- a/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
+++ b/src/Web/Grand.Web.Common/Grand.Web.Common.csproj
@@ -2,6 +2,7 @@
<Import Project="..\..\Build\Grand.Common.props" />
<ItemGroup>
<PackageReference Include="Flurl.Http" Version="3.2.2" />
+ <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.20.0" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection.StackExchangeRedis" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="6.0.3" />
<PackageReference Include="AspNetCore.HealthChecks.MongoDb" Version="6.0.2" />
diff --git a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
index 4b4414268..f948772dd 100644
--- a/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
+++ b/src/Web/Grand.Web.Common/Infrastructure/ServiceCollectionExtensions.cs
@@ -335,7 +335,16 @@ namespace Grand.Web.Common.Infrastructure
if (performanceConfig.HtmlMinificationErrors)
services.AddSingleton<IWmmLogger, WmmThrowExceptionLogger>();
}
+ public static void AddApplicationInsights(this IServiceCollection services, IConfiguration configuration)
+ {
+ var applicationInsights = new ApplicationInsightsConfig();
+ configuration.GetSection("ApplicationInsights").Bind(applicationInsights);
+ if(applicationInsights.Enabled)
+ {
+ services.AddApplicationInsightsTelemetry();
+ }
+ }
/// <summary>
/// Adds services for WebEncoderOptions
/// </summary>
diff --git a/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs b/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs
index fc49d042d..9d24cbe12 100644
--- a/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs
+++ b/src/Web/Grand.Web.Common/Startup/GrandMvcStartup.cs
@@ -28,6 +28,9 @@ namespace Grand.Web.Common.Startup
//add WebMarkupMin
services.AddHtmlMinification(configuration);
+ //add ApplicationInsights
+ services.AddApplicationInsights(configuration);
+
//add and configure MVC feature
services.AddGrandMvc(configuration);
diff --git a/src/Web/Grand.Web/App_Data/appsettings.json b/src/Web/Grand.Web/App_Data/appsettings.json
index e80cf5276..6c7cb42c5 100644
--- a/src/Web/Grand.Web/App_Data/appsettings.json
+++ b/src/Web/Grand.Web/App_Data/appsettings.json
@@ -248,5 +248,23 @@
// "Args": { "path": "logs/log.txt" }
// }
// ]
- }
+ },
+
+ "ApplicationInsights": {
+ "Enabled": false,
+ "ConnectionString": ""
+ },
+ //https://docs.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core
+ //The following configuration allows ApplicationInsights to capture
+ //all Information logs and more severe logs
+ //"Logging": {
+ // "LogLevel": {
+ // "Default": "Warning"
+ // },
+ // "ApplicationInsights": {
+ // "LogLevel": {
+ // "Default": "Information"
+ // }
+ // }
+ //}
}
\ No newline at end of file