/
vshmidt
/
masstransit
Обзор
Документация
Войти
/
vshmidt
/
masstransit
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/MassTransit.Abstractions/Metadata/BusHostInfo.cs
80 строк
3 KB
Chris Patterson
Change PlatformNotSupportedException to NotSupportedException to catch other platform-related errors
24 сен 2024, 20:25
24 сен 2024, 20:25
46995e0
Код
Авторство
О чём код?
namespace MassTransit.Metadata { using System; using System.Diagnostics; using System.IO; using System.Reflection; [Serializable] public class BusHostInfo : HostInfo { public BusHostInfo() { } public BusHostInfo(bool initialize) { FrameworkVersion = Environment.Version.ToString(); OperatingSystemVersion = Environment.OSVersion.ToString(); var entryAssembly = System.Reflection.Assembly.GetEntryAssembly() ?? System.Reflection.Assembly.GetCallingAssembly(); MachineName = Environment.MachineName; MassTransitVersion = typeof(HostInfo).Assembly.GetName().Version?.ToString(); try { using var currentProcess = Process.GetCurrentProcess(); ProcessId = currentProcess.Id; ProcessName = currentProcess.ProcessName; if ("dotnet".Equals(ProcessName, StringComparison.OrdinalIgnoreCase)) ProcessName = GetUsefulProcessName(ProcessName); } catch (NotSupportedException) { ProcessId = 0; ProcessName = GetUsefulProcessName("UWP"); } var assemblyName = entryAssembly.GetName(); Assembly = assemblyName.Name; AssemblyVersion = assemblyName.Version?.ToString() ?? "Unknown"; } public string? MachineName { get; set; } public string? ProcessName { get; set; } public int ProcessId { get; set; } public string? Assembly { get; set; } public string? AssemblyVersion { get; set; } public string? FrameworkVersion { get; set; } public string? MassTransitVersion { get; set; } public string? OperatingSystemVersion { get; set; } static string GetAssemblyFileVersion(Assembly assembly) { var attribute = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>(); if (attribute != null) return attribute.Version; return FileVersionInfo.GetVersionInfo(assembly.Location).FileVersion ?? "Unknown"; } static string GetAssemblyInformationalVersion(Assembly assembly) { var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>(); if (attribute != null) return attribute.InformationalVersion; return GetAssemblyFileVersion(assembly); } static string GetUsefulProcessName(string defaultProcessName) { var entryAssemblyLocation = System.Reflection.Assembly.GetEntryAssembly()?.Location; return string.IsNullOrWhiteSpace(entryAssemblyLocation) ? defaultProcessName : Path.GetFileNameWithoutExtension(entryAssemblyLocation); } } }