-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniversalExtensions.cs
118 lines (105 loc) · 3.96 KB
/
UniversalExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Reflection;
namespace Xorog.UniversalExtensions;
public static class UniversalExtensions
{
/// <summary>
/// Attaches a logger to UniversalExtensions. Used for Debugging.
/// </summary>
/// <param name="logger"></param>
public static void AttachLogger(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// Loads all referenced dependencies in an <see cref="AppDomain"/>.
/// <para>Prevents <see cref="FileNotFoundException"/>s that are caused by utilizing not yet loaded assemblies after the system has been updated.</para>
/// </summary>
/// <param name="domain">The <see cref="AppDomain"/> to load all dependencies from.</param>
public static void LoadAllReferencedAssemblies(this AppDomain domain)
{
_logger?.LogDebug("Loading all assemblies..");
var assemblyCount = 0;
foreach (Assembly assembly in domain.GetAssemblies())
{
LoadReferencedAssembly(assembly);
}
void LoadReferencedAssembly(Assembly assembly)
{
try
{
foreach (AssemblyName name in assembly.GetReferencedAssemblies())
{
if (!AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName == name.FullName))
{
assemblyCount++;
_logger?.LogDebug("Loading {Name}..", name.Name);
LoadReferencedAssembly(Assembly.Load(name));
}
}
}
catch (Exception ex)
{
_logger?.LogError("Failed to load an assembly", ex);
}
}
_logger?.LogInformation("Loaded {assemblyCount} assemblies.", assemblyCount);
}
public static void LoadAllReferencedAssemblies(params AssemblyName[] assemblies)
{
_logger?.LogDebug("Loading all assemblies..");
var assemblyCount = 0;
foreach (AssemblyName assembly in assemblies)
{
LoadReferencedAssembly(Assembly.Load(assembly));
}
void LoadReferencedAssembly(Assembly assembly)
{
try
{
foreach (AssemblyName name in assembly.GetReferencedAssemblies())
{
if (!AppDomain.CurrentDomain.GetAssemblies().Any(a => a.FullName == name.FullName))
{
assemblyCount++;
_logger?.LogDebug("Loading {Name}..", name.Name);
LoadReferencedAssembly(Assembly.Load(name));
}
}
}
catch (Exception ex)
{
_logger?.LogError("Failed to load an assembly", ex);
}
}
_logger?.LogInformation("Loaded {assemblyCount} assemblies.", assemblyCount);
}
/// <summary>
/// Adds additional data to an exception.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="exception"></param>
/// <param name="Key"></param>
/// <param name="Data"></param>
/// <returns></returns>
public static T AttachData<T>(this T exception, string Key, object? Data) where T : Exception
{
exception.Data.Add(Key, Data);
return exception;
}
/// <summary>
/// Get the current CPU Usage on all platforms
/// </summary>
/// <returns></returns>
public static async Task<double> GetCpuUsageForProcess()
{
var startTime = DateTime.UtcNow;
var startCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;
await Task.Delay(500);
var endTime = DateTime.UtcNow;
var endCpuUsage = Process.GetCurrentProcess().TotalProcessorTime;
var cpuUsedMs = (endCpuUsage - startCpuUsage).TotalMilliseconds;
var totalMsPassed = (endTime - startTime).TotalMilliseconds;
var cpuUsageTotal = cpuUsedMs / (Environment.ProcessorCount * totalMsPassed);
return cpuUsageTotal * 100;
}
}