From a620241c52df68f2cd74054506d67dde90551895 Mon Sep 17 00:00:00 2001 From: Martin Taillefer Date: Thu, 4 Jan 2024 14:38:25 -0800 Subject: [PATCH] Simplify platform check. Fixes #4642 --- ...ceMonitoringServiceCollectionExtensions.cs | 24 +++++++------------ 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceMonitoringServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceMonitoringServiceCollectionExtensions.cs index 0981bd5a612..06d183536bc 100644 --- a/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceMonitoringServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Diagnostics.ResourceMonitoring/ResourceMonitoringServiceCollectionExtensions.cs @@ -51,6 +51,8 @@ public static IServiceCollection AddResourceMonitoring( return services.AddResourceMonitoringInternal(configure); } + // can't easily test the exception throwing case + [ExcludeFromCodeCoverage] private static IServiceCollection AddResourceMonitoringInternal( this IServiceCollection services, Action configure) @@ -62,14 +64,18 @@ private static IServiceCollection AddResourceMonitoringInternal( #if NETFRAMEWORK _ = builder.AddWindowsProvider(); #else - if (GetPlatform() == PlatformID.Win32NT) + if (OperatingSystem.IsWindows()) { _ = builder.AddWindowsProvider(); } - else + else if (OperatingSystem.IsLinux()) { _ = builder.AddLinuxProvider(); } + else + { + throw new PlatformNotSupportedException(); + } #endif configure.Invoke(builder); @@ -77,20 +83,6 @@ private static IServiceCollection AddResourceMonitoringInternal( return services; } -#if !NETFRAMEWORK - [ExcludeFromCodeCoverage] - private static PlatformID GetPlatform() - { - var os = Environment.OSVersion; - if (os.Platform != PlatformID.Win32NT && os.Platform != PlatformID.Unix) - { - throw new NotSupportedException("Resource monitoring is not supported on this operating system."); - } - - return os.Platform; - } -#endif - private static ResourceMonitorBuilder AddWindowsProvider(this ResourceMonitorBuilder builder) { builder.PickWindowsSnapshotProvider();