using NLog; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace MP.AppAuth { /// /// Helper x estrarre dati sintetici su HW, Software, librerie installate... /// public class HwSwInfo { #region Private Fields private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); #endregion Private Fields #region Protected Fields /// /// Assembly di base /// protected Assembly assembly = Assembly.GetExecutingAssembly(); #endregion Protected Fields #region Public Constructors /// /// Istanza base /// public HwSwInfo() { assembly = Assembly.GetExecutingAssembly(); } public HwSwInfo(Assembly targetAssembly) { assembly = targetAssembly; } #endregion Public Constructors #region Public Properties /// /// Statistiche IIS /// public string IISStats { get { StringBuilder sb = new StringBuilder(); using (var iis = Process.GetCurrentProcess()) { //Process iis = Process.GetCurrentProcess(); sb.AppendLine(string.Format("UPTIME: {0}", ToDateString(DateTime.Now - iis.StartTime))); sb.AppendLine(string.Format("Priority: {0}", iis.PriorityClass)); sb.AppendLine(string.Format("Physical Memory Used: {0}", memFormat(iis.WorkingSet64))); sb.AppendLine(string.Format("Virtual Memory Used: {0}", memFormat(iis.VirtualMemorySize64))); } return sb.ToString(); } } /// /// Versioni di ogni libreria compresa /// public string librariesVers { get { string answ = ""; try { AssemblyName[] referencedAssemblyNames = assembly.GetReferencedAssemblies(); foreach (AssemblyName referencedAssemblyName in referencedAssemblyNames.OrderBy(n => n.Name)) { answ += referencedAssemblyName.FullName + Environment.NewLine; } } catch { } return answ; } } /// /// Nome MainAssembly /// public string mainAssembly { get { string answ = ""; try { answ = assembly.FullName; } catch { } return answ; } } /// /// Num di librerie inserite /// public int numLibraries { get { int numLib = 0; ; try { AssemblyName[] referencedAssemblyNames = assembly.GetReferencedAssemblies(); foreach (AssemblyName referencedAssemblyName in referencedAssemblyNames.OrderBy(n => n.Name)) { numLib++; } } catch { } return numLib; } } ///// ///// Dati sui server redis... ///// //public string redisServersData //{ // get // { // StringBuilder sb = new StringBuilder(); // try // { // var servData = memLayer.ML.redServInfo(); // foreach (var item in servData) // { // sb.AppendLine(string.Format("Server: {0} | {1} | {2}", item, item.Version, item.ServerType)); // // da completare con altre info?!? // var srvInfo = item.Info(); // // esporto un pò di info x gruppo... // foreach (IGrouping> capitolo in srvInfo) // { // sb.AppendLine("----------------------------------------------"); // sb.AppendLine(string.Format("Capitolo: {0}", capitolo.Key)); // sb.AppendLine("----------------------------------------------"); // foreach (var kvp in capitolo) // { // sb.AppendLine(string.Format("{0}:{1}", kvp.Key, kvp.Value)); // } // sb.AppendLine(); // } // } // } // catch (Exception exc) // { // Log.Error($"Errore in redisServersData{Environment.NewLine}{exc}"); // } // return sb.ToString(); // } //} /// /// Runtime assembly /// public string runtimeImg { get { string answ = ""; try { answ = assembly.ImageRuntimeVersion; } catch { } return answ; } } /// /// Statistiche server /// public string ServerStats { get { StringBuilder sb = new StringBuilder(); try { // calcoli preliminari // compongo output sb.AppendLine(string.Format("NAME: {0}", Environment.MachineName)); sb.AppendLine(System.Runtime.InteropServices.RuntimeInformation.OSDescription); sb.AppendLine(System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription); sb.AppendLine(string.Format("OS 64bit: {0} | PROC 64bit: {1}", Environment.Is64BitOperatingSystem, Environment.Is64BitProcess)); //sb.AppendLine(string.Format("SYS UPTIME: {0}", ToDateString(uptime))); sb.AppendLine(string.Format("CPU: {0}", Environment.ProcessorCount)); //sb.AppendLine(string.Format("RAM: {0}", memFormat(computer.TotalPhysicalMemory))); //sb.AppendLine(string.Format("Virtual Memory: {0}", memFormat(computer.TotalVirtualMemory))); //sb.AppendLine(string.Format("Available RAM: {0}", memFormat(computer.AvailablePhysicalMemory))); //sb.AppendLine(string.Format("Available Virtual Memory: {0}", memFormat(computer.AvailableVirtualMemory))); sb.AppendLine(string.Format("DIR: {0}", Environment.CurrentDirectory)); sb.AppendLine(string.Format("USER: {0} | USER: {1}", Environment.UserDomainName, Environment.UserName)); } catch (Exception exc) { Log.Error($"Errore in ServerStats{Environment.NewLine}{exc}"); } return sb.ToString(); } } #endregion Public Properties #region Public Methods /// /// Singleton! /// public static HwSwInfo man(Assembly targetAssembly) { HwSwInfo answ = new HwSwInfo(targetAssembly); return answ; } /// /// Formatta un numero da int a size in Kb/Mb/Gb/Tb /// /// /// public string memFormat(double rawSize) { string[] sizes = { "B", "KB", "MB", "GB", "TB" }; int order = 0; while (rawSize >= 1024 && order < sizes.Length - 1) { order++; rawSize = rawSize / 1024; } // Adjust the format string to your preferences. For example "{0:0.#}{1}" would // show a single decimal place, and no space. return String.Format("{0:0.##} {1}", rawSize, sizes[order]); } /// /// Stringa timing in gg, ore, ... da timespan /// /// /// public string ToDateString(TimeSpan time) { return string.Format("{0}gg, {1:00}:{2:00}:{3:00} (h:m:s)", time.Days, time.Hours, time.Minutes, time.Seconds); } #endregion Public Methods } }