private void PopulateProcessInfo(EnvironmentInfo info) {
            try {
                info.ProcessorCount = Environment.ProcessorCount;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get processor count. Error message: {0}", ex.Message);
            }

#if !PORTABLE && !NETSTANDARD1_2
            try {
                Process process = Process.GetCurrentProcess();
                info.ProcessName = process.ProcessName;
                info.ProcessId = process.Id.ToString(NumberFormatInfo.InvariantInfo);
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get process name or id. Error message: {0}", ex.Message);
            }

            try {
#if NETSTANDARD1_5
                info.CommandLine = String.Join(" ", Environment.GetCommandLineArgs());
#elif NET45
                info.CommandLine = Environment.CommandLine;
#endif
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get command line. Error message: {0}", ex.Message);
            }
#endif
        }
        public EnvironmentInfo GetEnvironmentInfo() {
            if (_environmentInfo == null)
                _environmentInfo = new EnvironmentInfo {
                    MachineName = Guid.NewGuid().ToString("N")
                };

            return _environmentInfo;
        }
        public EnvironmentInfo GetEnvironmentInfo() {
            if (_environmentInfo != null) {
                PopulateThreadInfo(_environmentInfo);
                PopulateMemoryInfo(_environmentInfo);
                return _environmentInfo;
            }

            var info = new EnvironmentInfo();
            PopulateRuntimeInfo(info);
            PopulateProcessInfo(info);
            PopulateThreadInfo(info);
            PopulateMemoryInfo(info);

            _environmentInfo = info;
            return _environmentInfo;
        }
        private void PopulateApplicationInfo(EnvironmentInfo info) {
#if !PORTABLE && !NETSTANDARD1_2
            try {
                info.Data.Add("AppDomainName", AppDomain.CurrentDomain.FriendlyName);
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get AppDomain friendly name. Error message: {0}", ex.Message);
            }
#endif

#if !PORTABLE && !NETSTANDARD1_2
            try {
                IPHostEntry hostEntry = Dns.GetHostEntryAsync(Dns.GetHostName()).ConfigureAwait(false).GetAwaiter().GetResult();
                if (hostEntry != null && hostEntry.AddressList.Any())
                    info.IpAddress = String.Join(", ", hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).Select(a => a.ToString()).ToArray());
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get ip address. Error message: {0}", ex.Message);
            }
#endif
        }
 protected bool Equals(EnvironmentInfo other) {
     return ProcessorCount == other.ProcessorCount && TotalPhysicalMemory == other.TotalPhysicalMemory && AvailablePhysicalMemory == other.AvailablePhysicalMemory && string.Equals(CommandLine, other.CommandLine) && string.Equals(ProcessName, other.ProcessName) && string.Equals(ProcessId, other.ProcessId) && ProcessMemorySize == other.ProcessMemorySize && string.Equals(ThreadName, other.ThreadName) && string.Equals(ThreadId, other.ThreadId) && string.Equals(Architecture, other.Architecture) && string.Equals(OSName, other.OSName) && string.Equals(OSVersion, other.OSVersion) && string.Equals(IpAddress, other.IpAddress) && string.Equals(MachineName, other.MachineName) && string.Equals(InstallId, other.InstallId) && string.Equals(RuntimeVersion, other.RuntimeVersion) && Equals(Data, other.Data);
 }
        private void PopulateThreadInfo(EnvironmentInfo info) {
#if !PORTABLE && !NETSTANDARD1_2
            try {
                info.ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(NumberFormatInfo.InvariantInfo);
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get thread id. Error message: {0}", ex.Message);
            }

            try {
                info.ThreadName = Thread.CurrentThread.Name;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get current thread name. Error message: {0}", ex.Message);
            }
#endif
        }
        private void PopulateRuntimeInfo(EnvironmentInfo info) {
#if NETSTANDARD
            info.OSName = GetOSName(RuntimeInformation.OSDescription);
            info.OSVersion = GetVersion(RuntimeInformation.OSDescription)?.ToString();
            info.Architecture = RuntimeInformation.OSArchitecture.ToString();
            info.Data["FrameworkDescription"] = RuntimeInformation.FrameworkDescription;
            info.Data["ProcessArchitecture"] = RuntimeInformation.ProcessArchitecture;
#endif

            try {
#if NET45 || NETSTANDARD1_5
                info.MachineName = Environment.MachineName;
#elif !PORTABLE && !NETSTANDARD1_2
                Process process = Process.GetCurrentProcess();
                info.MachineName = process.MachineName;
#else
                info.MachineName = Guid.NewGuid().ToString("N");
#endif
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get machine name. Error message: {0}", ex.Message);
            }

#if !PORTABLE && !NETSTANDARD1_2
#if NETSTANDARD
            Microsoft.Extensions.PlatformAbstractions.PlatformServices computerInfo = null;
#elif 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.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get computer info. Error message: {0}", ex.Message);
            }

#if NETSTANDARD || NET45
            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;
#elif NET45
                info.OSName = computerInfo.OSFullName;
                info.OSVersion = computerInfo.OSVersion;
                info.Architecture = Is64BitOperatingSystem() ? "x64" : "x86";
                info.RuntimeVersion = Environment.Version.ToString();
#endif
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get populate runtime info. Error message: {0}", ex.Message);
            }
#endif
        }
        private void PopulateMemoryInfo(EnvironmentInfo info) {
#if !PORTABLE && !NETSTANDARD1_2
            try {
                Process process = Process.GetCurrentProcess();
                info.ProcessMemorySize = process.PrivateMemorySize64;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get process memory size. Error message: {0}", ex.Message);
            }
#endif

#if NET45
            try {
                if (IsMonoRuntime) {
                    if (PerformanceCounterCategory.Exists("Mono Memory")) {
                        var totalPhysicalMemory = new PerformanceCounter("Mono Memory", "Total Physical Memory");
                        info.TotalPhysicalMemory = Convert.ToInt64(totalPhysicalMemory.RawValue);

                        var availablePhysicalMemory = new PerformanceCounter("Mono Memory", "Available Physical Memory"); //mono 4.0+
                        info.AvailablePhysicalMemory = Convert.ToInt64(availablePhysicalMemory.RawValue);
                    }
                } else {
                    var computerInfo = new Microsoft.VisualBasic.Devices.ComputerInfo();
                    info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
                    info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
                }
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(DefaultEnvironmentInfoCollector), "Unable to get physical memory. Error message: {0}", ex.Message);
            }
#endif
        }
        public EnvironmentInfo GetEnvironmentInfo() {
            if (_environmentInfo != null)
                return _environmentInfo;

            var info = new EnvironmentInfo();
            ComputerInfo computerInfo = null;

            try {
                computerInfo = new ComputerInfo();
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get computer info. Error message: {0}", ex.Message);
            }

            try {
                if (computerInfo != null) {
                    info.OSName = computerInfo.OSFullName;
                    info.OSVersion = computerInfo.OSVersion;
                }
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get operating system version. Error message: {0}", ex.Message);
            }

            try {
                if (IsUnix) {
                    if (PerformanceCounterCategory.Exists("Mono Memory")) {
                        var totalPhysicalMemory = new PerformanceCounter("Mono Memory", "Total Physical Memory");
                        info.TotalPhysicalMemory = Convert.ToInt64(totalPhysicalMemory.RawValue);

                        var availablePhysicalMemory = new PerformanceCounter("Mono Memory", "Available Physical Memory"); //mono 4.0+
                        info.AvailablePhysicalMemory = Convert.ToInt64(availablePhysicalMemory.RawValue);
                    }
                } else {
                    if (computerInfo != null) {
                        info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
                        info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
                    }
                }
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get physical memory. Error message: {0}", ex.Message);
            }

            try {
                info.ProcessorCount = Environment.ProcessorCount;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get processor count. Error message: {0}", ex.Message);
            }

            try {
                info.MachineName = Environment.MachineName;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get machine name. Error message: {0}", ex.Message);
            }

            try {
                IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
                if (hostEntry != null && hostEntry.AddressList.Any())
                    info.IpAddress = String.Join(", ", hostEntry.AddressList.Where(x => x.AddressFamily == AddressFamily.InterNetwork).Select(a => a.ToString()).ToArray());
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get ip address. Error message: {0}", ex.Message);
            }

            try {
                Process proc = Process.GetCurrentProcess();
                info.ProcessMemorySize = proc.PrivateMemorySize64;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process memory size. Error message: {0}", ex.Message);
            }

            try {
                info.CommandLine = Environment.CommandLine;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get command line. Error message: {0}", ex.Message);
            }

            try {
                if (IsUnix) {
                    var currentProcess = Process.GetCurrentProcess();
                    info.ProcessId = currentProcess.Id.ToString(NumberFormatInfo.InvariantInfo);
                } else {
                    info.ProcessId = KernelNativeMethods.GetCurrentProcessId().ToString(NumberFormatInfo.InvariantInfo);
                }
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process id. Error message: {0}", ex.Message);
            }

            try {
                if (IsUnix) {
                    var currentProcess = Process.GetCurrentProcess();
                    info.ProcessName = currentProcess.ProcessName;
                } else {
                    info.ProcessName = GetProcessName();
                }
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process name. Error message: {0}", ex.Message);
            }

            try {
                if (IsUnix)
                    info.ThreadId = Thread.CurrentThread.ManagedThreadId.ToString(NumberFormatInfo.InvariantInfo);
                else
                    info.ThreadId = KernelNativeMethods.GetCurrentThreadId().ToString(NumberFormatInfo.InvariantInfo);
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get thread id. Error message: {0}", ex.Message);
            }

            try {
                info.Architecture = Is64BitOperatingSystem() ? "x64" : "x86";
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get CPU architecture. Error message: {0}", ex.Message);
            }

            try {
                info.RuntimeVersion = Environment.Version.ToString();
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get CLR version. Error message: {0}", ex.Message);
            }

            try {
                info.Data.Add("AppDomainName", AppDomain.CurrentDomain.FriendlyName);
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get AppDomain friendly name. Error message: {0}", ex.Message);
            }

            try {
                info.ThreadName = Thread.CurrentThread.Name;
            } catch (Exception ex) {
                _log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get current thread name. Error message: {0}", ex.Message);
            }

            _environmentInfo = info;
            return _environmentInfo;
        }