Skip to content

Commit

Permalink
Remove Deprecated Microsoft.Extensions.PlatformAbstractions
Browse files Browse the repository at this point in the history
  • Loading branch information
niemyjski committed Mar 28, 2024
1 parent 829bdbc commit 50ebf80
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 86 deletions.
1 change: 0 additions & 1 deletion src/Exceptionless/Exceptionless.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'" Label="Package References">
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.PlatformAbstractions" Version="1.1.0" />
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>

Expand Down
76 changes: 3 additions & 73 deletions src/Exceptionless/Plugins/Default/080_VersionPlugin.cs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -40,10 +39,10 @@ public class VersionPlugin : IEventPlugin {
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;
Expand All @@ -54,80 +53,11 @@ public class VersionPlugin : IEventPlugin {
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;
}
}
}
26 changes: 14 additions & 12 deletions src/Exceptionless/Services/DefaultEnvironmentInfoCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -154,31 +157,30 @@ public class DefaultEnvironmentInfoCollector : IEnvironmentInfoCollector {
}
}

#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<TargetFrameworkAttribute>()?.FrameworkName;
}
#elif NET45
info.OSName = computerInfo.OSFullName;
info.OSVersion = computerInfo.OSVersion;
Expand Down
57 changes: 57 additions & 0 deletions src/Exceptionless/Utility/AssemblyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,63 @@ public class AssemblyHelper {
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();
Expand Down

0 comments on commit 50ebf80

Please sign in to comment.