From 50ebf80277b00c47e19d1be913cd6377bfeae04f Mon Sep 17 00:00:00 2001 From: Blake Niemyjski Date: Thu, 28 Mar 2024 08:07:03 -0500 Subject: [PATCH] Remove Deprecated Microsoft.Extensions.PlatformAbstractions --- src/Exceptionless/Exceptionless.csproj | 1 - .../Plugins/Default/080_VersionPlugin.cs | 76 +------------------ .../DefaultEnvironmentInfoCollector.cs | 26 ++++--- src/Exceptionless/Utility/AssemblyHelper.cs | 57 ++++++++++++++ 4 files changed, 74 insertions(+), 86 deletions(-) diff --git a/src/Exceptionless/Exceptionless.csproj b/src/Exceptionless/Exceptionless.csproj index 177bcb10..6e6ffa24 100644 --- a/src/Exceptionless/Exceptionless.csproj +++ b/src/Exceptionless/Exceptionless.csproj @@ -35,7 +35,6 @@ - diff --git a/src/Exceptionless/Plugins/Default/080_VersionPlugin.cs b/src/Exceptionless/Plugins/Default/080_VersionPlugin.cs index c7769f5e..8d870e3c 100644 --- a/src/Exceptionless/Plugins/Default/080_VersionPlugin.cs +++ b/src/Exceptionless/Plugins/Default/080_VersionPlugin.cs @@ -1,8 +1,7 @@ using System; -using System.Linq; -using System.Reflection; using Exceptionless.Logging; using Exceptionless.Models; +using Exceptionless.Utility; namespace Exceptionless.Plugins.Default { [Priority(80)] @@ -40,10 +39,10 @@ private string GetVersion(IExceptionlessLog log) { if (_appVersionLoaded) return _appVersion; - var entryAssembly = GetEntryAssembly(log); + var entryAssembly = AssemblyHelper.GetEntryAssembly(log); try { - string version = GetVersionFromAssembly(entryAssembly); + string version = AssemblyHelper.GetVersionFromAssembly(entryAssembly); if (!String.IsNullOrEmpty(version)) { _appVersion = version; _appVersionLoaded = true; @@ -54,80 +53,11 @@ private string GetVersion(IExceptionlessLog log) { log.FormattedError(typeof(VersionPlugin), ex, "Unable to get version from loaded assemblies. Error: {0}", ex.Message); } -#if NETSTANDARD2_0 - try { - var platformService = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default; - - _appVersion = platformService.Application.ApplicationVersion; - _appVersionLoaded = true; - - return _appVersion; - } catch (Exception ex) { - log.FormattedError(typeof(VersionPlugin), ex, "Unable to get Platform Services instance. Error: {0}", ex.Message); - } -#endif - _appVersion = null; _appVersionLoaded = true; return null; } - private string GetVersionFromAssembly(Assembly assembly) { - if (assembly == null) - return null; - - string version = assembly.GetInformationalVersion(); - if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) - version = assembly.GetFileVersion(); - - if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) - version = assembly.GetVersion(); - - if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) { - var assemblyName = assembly.GetAssemblyName(); - version = assemblyName != null ? assemblyName.Version.ToString() : null; - } - - return !String.IsNullOrEmpty(version) && !String.Equals(version, "0.0.0.0") ? version : null; - } - - private Assembly GetEntryAssembly(IExceptionlessLog log) { - var entryAssembly = Assembly.GetEntryAssembly(); - if (IsUserAssembly(entryAssembly)) - return entryAssembly; - - try { - var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => - !a.IsDynamic - && a != typeof(ExceptionlessClient).GetTypeInfo().Assembly - && a != GetType().GetTypeInfo().Assembly - && a != typeof(object).GetTypeInfo().Assembly); - - return assemblies.FirstOrDefault(a => IsUserAssembly(a)); - } catch (Exception ex) { - log.FormattedError(typeof(VersionPlugin), ex, "Unable to get entry assembly. Error: {0}", ex.Message); - } - - return null; - } - - private bool IsUserAssembly(Assembly assembly) { - if (assembly == null) - return false; - - if (!String.IsNullOrEmpty(assembly.FullName) && (assembly.FullName.StartsWith("System.") || assembly.FullName.StartsWith("Microsoft."))) - return false; - - string company = assembly.GetCompany() ?? String.Empty; - string[] nonUserCompanies = new[] { "Exceptionless", "Microsoft" }; - if (nonUserCompanies.Any(c => company.IndexOf(c, StringComparison.OrdinalIgnoreCase) >= 0)) - return false; - - if (assembly.FullName == typeof(ExceptionlessClient).GetTypeInfo().Assembly.FullName) - return false; - - return true; - } } } diff --git a/src/Exceptionless/Services/DefaultEnvironmentInfoCollector.cs b/src/Exceptionless/Services/DefaultEnvironmentInfoCollector.cs index b0055cdf..f8b7bd44 100644 --- a/src/Exceptionless/Services/DefaultEnvironmentInfoCollector.cs +++ b/src/Exceptionless/Services/DefaultEnvironmentInfoCollector.cs @@ -4,11 +4,14 @@ using System.Linq; using System.Net; using System.Net.Sockets; +using System.Reflection; using System.Runtime.InteropServices; +using System.Runtime.Versioning; using System.Text; using System.Threading; using Exceptionless.Logging; using Exceptionless.Models.Data; +using Exceptionless.Utility; namespace Exceptionless.Services { public class DefaultEnvironmentInfoCollector : IEnvironmentInfoCollector { @@ -154,31 +157,30 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) { } } -#if NETSTANDARD - Microsoft.Extensions.PlatformAbstractions.PlatformServices computerInfo = null; -#elif NET45 +#if NET45 Microsoft.VisualBasic.Devices.ComputerInfo computerInfo = null; -#endif try { -#if NETSTANDARD - computerInfo = Microsoft.Extensions.PlatformAbstractions.PlatformServices.Default; -#elif NET45 computerInfo = new Microsoft.VisualBasic.Devices.ComputerInfo(); -#endif } catch (Exception ex) { Log.FormattedWarn(typeof(DefaultEnvironmentInfoCollector), "Unable to get computer info. Error message: {0}", ex.Message); } if (computerInfo == null) return; +#endif try { #if NETSTANDARD - info.RuntimeVersion = computerInfo.Application.RuntimeFramework.Version.ToString(); - info.Data["ApplicationBasePath"] = computerInfo.Application.ApplicationBasePath; - info.Data["ApplicationName"] = computerInfo.Application.ApplicationName; - info.Data["RuntimeFramework"] = computerInfo.Application.RuntimeFramework.FullName; + info.RuntimeVersion = Environment.Version.ToString(); + info.Data["ApplicationBasePath"] = AppContext.BaseDirectory ?? AppDomain.CurrentDomain.BaseDirectory; + + + var entryAssembly = AssemblyHelper.GetEntryAssembly(Log); + if (entryAssembly != null) { + info.Data["ApplicationName"] = entryAssembly.GetName().Name; + info.Data["RuntimeFramework"] = entryAssembly.GetCustomAttribute()?.FrameworkName; + } #elif NET45 info.OSName = computerInfo.OSFullName; info.OSVersion = computerInfo.OSVersion; diff --git a/src/Exceptionless/Utility/AssemblyHelper.cs b/src/Exceptionless/Utility/AssemblyHelper.cs index 0e8fe6d5..ccf70e1c 100644 --- a/src/Exceptionless/Utility/AssemblyHelper.cs +++ b/src/Exceptionless/Utility/AssemblyHelper.cs @@ -10,6 +10,63 @@ public static Assembly GetRootAssembly() { return Assembly.GetEntryAssembly(); } + public static Assembly GetEntryAssembly(IExceptionlessLog log) { + var entryAssembly = Assembly.GetEntryAssembly(); + if (IsUserAssembly(entryAssembly)) + return entryAssembly; + + try { + var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => + !a.IsDynamic + && a != typeof(ExceptionlessClient).GetTypeInfo().Assembly + && a != typeof(object).GetTypeInfo().Assembly); + + return assemblies.FirstOrDefault(a => IsUserAssembly(a)); + } + catch (Exception ex) { + log.FormattedError(typeof(AssemblyHelper), ex, "Unable to get entry assembly. Error: {0}", ex.Message); + } + + return null; + } + + private static bool IsUserAssembly(Assembly assembly) { + if (assembly == null) + return false; + + if (!String.IsNullOrEmpty(assembly.FullName) && (assembly.FullName.StartsWith("System.") || assembly.FullName.StartsWith("Microsoft."))) + return false; + + string company = assembly.GetCompany() ?? String.Empty; + string[] nonUserCompanies = new[] { "Exceptionless", "Microsoft" }; + if (nonUserCompanies.Any(c => company.IndexOf(c, StringComparison.OrdinalIgnoreCase) >= 0)) + return false; + + if (assembly.FullName == typeof(ExceptionlessClient).GetTypeInfo().Assembly.FullName) + return false; + + return true; + } + + public static string GetVersionFromAssembly(Assembly assembly) { + if (assembly == null) + return null; + + string version = assembly.GetInformationalVersion(); + if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) + version = assembly.GetFileVersion(); + + if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) + version = assembly.GetVersion(); + + if (String.IsNullOrEmpty(version) || String.Equals(version, "0.0.0.0")) { + var assemblyName = assembly.GetAssemblyName(); + version = assemblyName != null ? assemblyName.Version.ToString() : null; + } + + return !String.IsNullOrEmpty(version) && !String.Equals(version, "0.0.0.0") ? version : null; + } + public static string GetAssemblyTitle() { // Get all attributes on this assembly var assembly = GetRootAssembly();