From 5b77e0f75c5e3ee75c9237b5cb8f0932dd317aeb Mon Sep 17 00:00:00 2001 From: Turnerj Date: Sun, 6 Jun 2021 14:16:42 +0930 Subject: [PATCH 1/3] Treat staging environment like production and development The staging environment is also built into the framework and thus should be treated the same way as both production and development environments. Other custom environments can still be treated as-is. --- src/Sentry.AspNetCore/Constants.cs | 14 -------------- .../SentryAspNetCoreOptionsSetup.cs | 12 +++++++++--- src/Sentry/Internal/Constants.cs | 2 ++ 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/Sentry.AspNetCore/Constants.cs b/src/Sentry.AspNetCore/Constants.cs index 1237d0c3ce..18fb2eb625 100644 --- a/src/Sentry.AspNetCore/Constants.cs +++ b/src/Sentry.AspNetCore/Constants.cs @@ -4,19 +4,5 @@ internal static class Constants { // See: https://github.com/getsentry/sentry-release-registry public const string SdkName = "sentry.dotnet.aspnetcore"; - - public static string ASPNETCoreProductionEnvironmentName => -#if NETSTANDARD2_0 - "Production"; -#else - Microsoft.Extensions.Hosting.Environments.Production; -#endif - - public static string ASPNETCoreDevelopmentEnvironmentName => -#if NETSTANDARD2_0 - "Development"; -#else - Microsoft.Extensions.Hosting.Environments.Development; -#endif } } diff --git a/src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs b/src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs index 768432c03d..c2666bc287 100644 --- a/src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs +++ b/src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs @@ -4,8 +4,10 @@ using Sentry.Extensions.Logging; using Sentry.Internal; #if NETSTANDARD2_0 +using Microsoft.AspNetCore.Hosting; using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment; #else +using Microsoft.Extensions.Hosting; using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IWebHostEnvironment; #endif @@ -36,7 +38,7 @@ public override void Configure(SentryAspNetCoreOptions options) } else { - // NOTE: Sentry prefers to have it's environment setting to be all lower case. + // NOTE: Sentry prefers to have its environment setting to be all lower case. // .NET Core sets the ENV variable to 'Production' (upper case P) or // 'Development' (upper case D) which conflicts with the Sentry recommendation. // As such, we'll be kind and override those values, here ... if applicable. @@ -46,11 +48,15 @@ public override void Configure(SentryAspNetCoreOptions options) // need to respect (especially the case-sensitivity). // REF: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments - if (_hostingEnvironment.EnvironmentName.Equals(Constants.ASPNETCoreProductionEnvironmentName)) + if (_hostingEnvironment.IsProduction()) { options.Environment = Internal.Constants.ProductionEnvironmentSetting; } - else if (_hostingEnvironment.EnvironmentName.Equals(Constants.ASPNETCoreDevelopmentEnvironmentName)) + else if (_hostingEnvironment.IsStaging()) + { + options.Environment = Internal.Constants.StagingEnvironmentSetting; + } + else if (_hostingEnvironment.IsDevelopment()) { options.Environment = Internal.Constants.DevelopmentEnvironmentSetting; } diff --git a/src/Sentry/Internal/Constants.cs b/src/Sentry/Internal/Constants.cs index 5fecf52ac4..5f5c1527bf 100644 --- a/src/Sentry/Internal/Constants.cs +++ b/src/Sentry/Internal/Constants.cs @@ -26,6 +26,8 @@ internal static class Constants /// Best Sentry practice is to always try and have a value for this setting. public const string ProductionEnvironmentSetting = "production"; + public const string StagingEnvironmentSetting = "staging"; + public const string DevelopmentEnvironmentSetting = "development"; public const string DebugEnvironmentSetting = "debug"; From 1ffacec1f21a9f44acd81dcf91960ca7fc562272 Mon Sep 17 00:00:00 2001 From: Turnerj Date: Sun, 6 Jun 2021 14:19:43 +0930 Subject: [PATCH 2/3] Additional tests for staging environment changes --- .../SentryAspNetCoreOptionsSetupTests.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Sentry.AspNetCore.Tests/SentryAspNetCoreOptionsSetupTests.cs b/test/Sentry.AspNetCore.Tests/SentryAspNetCoreOptionsSetupTests.cs index f3f3798b25..71334df667 100644 --- a/test/Sentry.AspNetCore.Tests/SentryAspNetCoreOptionsSetupTests.cs +++ b/test/Sentry.AspNetCore.Tests/SentryAspNetCoreOptionsSetupTests.cs @@ -48,8 +48,11 @@ public void Filters_KestrelEventId1_WithException_NotFiltered() [InlineData("", "Production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is default PROD. [InlineData(null, "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV. [InlineData("", "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV. + [InlineData(null, "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV. + [InlineData("", "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV. [InlineData(null, "production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'p'). [InlineData(null, "development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'd'). + [InlineData(null, "staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 's'). public void Filters_Environment_CustomOrASPNETEnvironment_Set(string environment, string hostingEnvironmentSetting, string expectedEnvironment) { // Arrange. From 93f47b9a7bd590164b742492e8f775d956c0c339 Mon Sep 17 00:00:00 2001 From: Turnerj Date: Tue, 8 Jun 2021 15:39:37 +0930 Subject: [PATCH 3/3] Add changelog entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f710ba30b..22fc8215ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Report lowercase staging environment for ASP.NET Core ([#1046](https://github.com/getsentry/sentry-unity/pull/1046)) + ## 3.5.0 ### Features