using System.Reflection; namespace Xorog.UniversalExtensions; public static class UniversalExtensions { /// /// Attaches a logger to UniversalExtensions. Used for Debugging. /// /// public static void AttachLogger(ILogger logger) { _logger = logger; } /// /// Loads all referenced dependencies in an . /// Prevents s that are caused by utilizing not yet loaded assemblies after the system has been updated. /// /// The to load all dependencies from. 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); } /// /// Adds additional data to an exception. /// /// /// /// /// /// public static T AttachData(this T exception, string Key, object? Data) where T : Exception { exception.Data.Add(Key, Data); return exception; } /// /// Get the current CPU Usage on all platforms /// /// public static async Task 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; } }