Skip to content

Commit 699c205

Browse files
authored
fix: IsOsPlatform() can fail on older .NET Framework Versions (#1552)
1 parent 3508b13 commit 699c205

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/Agent/NewRelic/Agent/Core/NewRelic.Agent.Core/Environment.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
using NewRelic.SystemInterfaces;
1818
using Newtonsoft.Json;
1919
using NewRelic.Agent.Configuration;
20-
using System.Runtime.InteropServices;
2120

2221
namespace NewRelic.Agent.Core
2322
{
@@ -46,14 +45,16 @@ public Environment(ISystemInfo systemInfo, IProcessStatic processStatic, IConfig
4645

4746
AddVariable("OS", () => System.Environment.OSVersion?.VersionString);
4847

48+
#if NETSTANDARD2_0
4949
// report linux distro name and version when appropriate
50-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
50+
// This API is only supported on .net FX 4.7 + so limiting it
51+
// to .net core since that is the one affected.
52+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
5153
{
5254
AddVariable("Linux Distro Name", () => RuntimeEnvironmentInfo.OperatingSystem);
5355
AddVariable("Linux Distro Version", () => RuntimeEnvironmentInfo.OperatingSystemVersion);
5456
}
5557

56-
#if NETSTANDARD2_0
5758
// This API is only supported on .net FX 4.7 + so limiting it
5859
// to .net core since that is the one affected.
5960
AddVariable(".NET Version", () => System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription.ToString());

src/Agent/NewRelic/Agent/Core/NewRelic.Agent.Core/RuntimeEnvironmentInfo.cs

+12-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
using System;
55
using System.IO;
6-
using System.Runtime.InteropServices;
76

87
namespace NewRelic.Agent.Core
98
{
@@ -75,20 +74,22 @@ private static string GetOSVersion()
7574

7675
private static string GetFreeBSDVersion()
7776
{
77+
#if NETSTANDARD2_0
7878
// This is same as sysctl kern.version
7979
// FreeBSD 11.0-RELEASE-p1 FreeBSD 11.0-RELEASE-p1 #0 r306420: Thu Sep 29 01:43:23 UTC 2016 [email protected]:/usr/obj/usr/src/sys/GENERIC
8080
// What we want is major release as minor releases should be compatible.
81-
String version = RuntimeInformation.OSDescription;
81+
String version = System.Runtime.InteropServices.RuntimeInformation.OSDescription;
8282
try
8383
{
8484
// second token up to first dot
85-
return RuntimeInformation.OSDescription.Split()[1].Split('.')[0];
85+
return System.Runtime.InteropServices.RuntimeInformation.OSDescription.Split()[1].Split('.')[0];
8686
}
8787
catch (Exception ex)
8888
{
8989
log4net.ILog logger = log4net.LogManager.GetLogger(typeof(RuntimeEnvironmentInfo));
9090
logger.Debug($"Unable to report Operating System: Unexpected exception in GetFreeBSDVersion: {ex}");
9191
}
92+
#endif
9293
return string.Empty;
9394
}
9495

@@ -185,24 +186,28 @@ private static DistroInfo NormalizeDistroInfo(DistroInfo distroInfo)
185186

186187
private static Platform DetermineOSPlatform()
187188
{
188-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
189+
#if NETSTANDARD2_0
190+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows))
189191
{
190192
return Platform.Windows;
191193
}
192-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
194+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
193195
{
194196
return Platform.Linux;
195197
}
196-
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
198+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX))
197199
{
198200
return Platform.Darwin;
199201
}
200-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Create("FREEBSD")))
202+
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Create("FREEBSD")))
201203
{
202204
return Platform.FreeBSD;
203205
}
204206

205207
return Platform.Unknown;
208+
#else
209+
return Platform.Windows;
210+
#endif
206211
}
207212
}
208213
}

0 commit comments

Comments
 (0)