示例#1
0
        private static NetworkDetails GetNetworkDetails(DiagnosticReportType type)
        {
            if (!IsReportEnabled(type, DiagnosticReportType.Networks))
            {
                return(null);
            }

            var globalProps = IPGlobalProperties.GetIPGlobalProperties();

            return(new NetworkDetails
            {
                DHCPScope = ApplicationHelper.IsWindows ? globalProps.DhcpScopeName : string.Empty,
                Domain = globalProps.DomainName,
                Host = globalProps.HostName,
                IsWINSProxy = ApplicationHelper.IsWindows && globalProps.IsWinsProxy,
                NodeType = globalProps.NodeType.ToString(),
                InterfaceDetails = NetworkInterface.GetAllNetworkInterfaces()
                                   .Select(nic =>
                {
                    var macAsBytes = nic.GetPhysicalAddress().GetAddressBytes();
                    return new NetworkInterfaceDetails
                    {
                        Interface = nic,
                        Addresses = NetworkHelper.GetLocalIPAddresses(nic).ToArray(),
                        MAC = string.Join(":", macAsBytes.Select(b => b.ToString("X2")))
                    };
                })
                                   .ToArray()
            });
        }
示例#2
0
        private static SystemDetails GetSystemDetails(DiagnosticReportType type)
        {
            if (!IsReportEnabled(type, DiagnosticReportType.System))
            {
                return(null);
            }

            return(new SystemDetails
            {
                OSName = RuntimeInformation.OSDescription,
                OSType = GetOSPlatform(),
                Is64BitOS = Environment.Is64BitOperatingSystem,
                CLRRuntime = RuntimeInformation.FrameworkDescription,
                MachineName = Environment.MachineName,
                FQDN = NetworkHelper.GetFQDN(),
                User = Environment.UserDomainName + "\\" + Environment.UserName,
                CPU = GetProcessorName(),
                CPUCoreCount = (uint)Environment.ProcessorCount,
                InstalledRAMInGigaBytes = GetInstalledMemoryInGigaBytes(),
                SystemDirectory = Environment.SystemDirectory,
                CurrentDirectory = Environment.CurrentDirectory,
                RuntimeDirectory = RuntimeEnvironment.GetRuntimeDirectory(),
                Uptime = GetUptime()
            });
        }
示例#3
0
        public void When_generating_process_and_assemblies_and_networking_report()
        {
            const DiagnosticReportType Flags = DiagnosticReportType.Process
                                               | DiagnosticReportType.Assemblies
                                               | DiagnosticReportType.Networks;

            var report = DiagnosticReport.Generate(Flags);

            report.ShouldNotBeNull();
            report.Timestamp.ShouldBeLessThanOrEqualTo(DateTimeOffset.Now);
            report.TimeTaken.ShouldBeGreaterThan(0.Milliseconds());
            report.Type.ShouldBe(Flags);
            report.SystemDetails.ShouldBeNull();
            report.ProcessDetails.ShouldNotBeNull();
            report.DriveDetails.ShouldBeNull();
            report.Assemblies.ShouldNotBeNull();
            report.Assemblies.ShouldNotBeEmpty();
            report.EnvironmentVariables.ShouldBeNull();
            report.NetworkingDetails.ShouldNotBeNull();

            var formattedReport = report.ToString();

            formattedReport.ShouldNotBeNull();
            formattedReport.Length.ShouldBeGreaterThan(500);

            formattedReport.ShouldStartWith("/\r\n|Diagnostic Report generated at:");
            formattedReport.ShouldNotContain("\r\n|\r\n|System|...");
            formattedReport.ShouldContain("\r\n|\r\n|Process|...");
            formattedReport.ShouldNotContain("\r\n|\r\n|Drives|...");
            formattedReport.ShouldContain("\r\n|\r\n|Assemblies|...");
            formattedReport.ShouldNotContain("\r\n|\r\n|Environment-Variables|...");
            formattedReport.ShouldContain("\r\n|\r\n|Networks|...");
            formattedReport.ShouldContain("|\r\n|\t. Windows IP Configuration\r\n|");
            formattedReport.ShouldEndWith("\\");
        }
示例#4
0
        private static DriveDetails[] GetDriveDetails(DiagnosticReportType type)
        {
            return(!IsReportEnabled(type, DiagnosticReportType.Drives)
                ? null
                : DriveInfo.GetDrives()
                   .Select(d =>
            {
                var dashString = Dash.ToString();
                string driveFormat = string.Empty, volumeLabel = string.Empty;
                double capacity = 0, free = 0, available = 0;

                if (d.IsReady)
                {
                    // ReSharper disable once ConstantNullCoalescingCondition
                    driveFormat = d.DriveFormat ?? dashString;
                    volumeLabel = d.VolumeLabel ?? dashString;
                    capacity = UnitConverter.BytesToGigaBytes(d.TotalSize);
                    free = UnitConverter.BytesToGigaBytes(d.TotalFreeSpace);
                    available = UnitConverter.BytesToGigaBytes(d.AvailableFreeSpace);
                }

                return new DriveDetails
                {
                    Name = d.Name,
                    Type = d.DriveType.ToString(),
                    Format = driveFormat,
                    Label = volumeLabel,
                    TotalCapacityInGigaBytes = capacity,
                    FreeCapacityInGigaBytes = free,
                    AvailableCapacityInGigaBytes = available,
                };
            })
                   .ToArray());
        }
示例#5
0
 private static IDictionary <string, string> GetEnvironmentVariables(DiagnosticReportType type)
 {
     return(!IsReportEnabled(type, DiagnosticReportType.EnvironmentVariables)
         ? null
         : Environment.GetEnvironmentVariables()
            .Cast <DictionaryEntry>()
            .ToDictionary(kv => kv.Key.ToString(), kv => kv.Value.ToString()));
 }
示例#6
0
 /// <summary>
 /// Returns the details related to <c>System</c>, <c>Process</c>, <c>Assemblies</c>
 /// and <c>Environment</c> on which the application executes.
 /// </summary>
 public static string Generate(DiagnosticReportType type = DiagnosticReportType.Full)
 {
     try
     {
         return(GenerateImpl(type));
     } catch (Exception e)
     {
         return($"Unable to generate the Diagnostic Report. Error:{NewLine}\t{e}");
     }
 }
示例#7
0
        private static ProcessDetails GetProcessDetails(DiagnosticReportType type)
        {
            if (!IsReportEnabled(type, DiagnosticReportType.Process))
            {
                return(null);
            }

            using (var p = Process.GetCurrentProcess())
            {
                var pVerInfo = p.MainModule.FileVersionInfo;
                var result   = new ProcessDetails
                {
                    PID                   = p.Id,
                    Name                  = p.ProcessName,
                    Started               = p.StartTime,
                    LoadedIn              = ApplicationHelper.GetProcessStartupDuration(),
                    IsInteractive         = Environment.UserInteractive,
                    IsOptimized           = IsOptimized(),
                    Is64Bit               = Environment.Is64BitProcess,
                    IsServerGC            = GCSettings.IsServerGC,
                    IsLargeAddressAware   = ApplicationHelper.IsProcessLargeAddressAware(),
                    WorkingSetInMegaBytes = UnitConverter.BytesToMegaBytes(Environment.WorkingSet),
                    FileVersion           = pVerInfo.FileVersion,
                    ProductVersion        = pVerInfo.ProductVersion,
                    Language              = pVerInfo.Language,
                    Copyright             = pVerInfo.LegalCopyright,
                    OriginalFileName      = pVerInfo.OriginalFilename,
                    FileName              = pVerInfo.FileName,
                    ModuleName            = p.MainModule.ModuleName,
                    ModuleFileName        = p.MainModule.FileName,
                    ProductName           = pVerInfo.ProductName,
                    CommandLine           = Environment.GetCommandLineArgs()
                };

                ThreadPool.GetMinThreads(out var minWrkrs, out var minComplWrkers);
                ThreadPool.GetMaxThreads(out var maxWrkrs, out var maxComplWrkers);
                result.ThreadPoolMinCompletionPortCount = (uint)minComplWrkers;
                result.ThreadPoolMaxCompletionPortCount = (uint)maxComplWrkers;
                result.ThreadPoolMinWorkerCount         = (uint)minWrkrs;
                result.ThreadPoolMaxWorkerCount         = (uint)maxWrkrs;

                result.ThreadCount = (uint)p.Threads.OfType <ProcessThread>().Count();
                return(result);

                string IsOptimized()
                {
                    var executingAssembly = Assembly.GetEntryAssembly();

                    return(executingAssembly == null
                        ? "N/A - Assembly was called from Unmanaged code."
                        : executingAssembly.IsOptimized().ToString());
                }
            }
        }
示例#8
0
        /// <summary>
        /// Creates a new instance of the <see cref="DiagnosticReport"/>.
        /// </summary>
        private DiagnosticReport(DiagnosticReportType type)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Timestamp = DateTimeOffset.Now;
            Type      = type;

            SystemDetails        = GetSystemDetails(type);
            ProcessDetails       = GetProcessDetails(type);
            DriveDetails         = GetDriveDetails(type);
            Assemblies           = GetAssemblies(type);
            EnvironmentVariables = GetEnvironmentVariables(type);
            NetworkingDetails    = GetNetworkDetails(type);

            TimeTaken = sw.Elapsed;
        }
示例#9
0
        private static ProcessDetails GetProcessDetails(DiagnosticReportType type)
        {
            if (!IsReportEnabled(type, DiagnosticReportType.Process))
            {
                return(null);
            }

            using (var p = Process.GetCurrentProcess())
            {
                var pVerInfo = p.MainModule.FileVersionInfo;
                return(new ProcessDetails
                {
                    PID = p.Id,
                    Name = p.ProcessName,
                    Started = p.StartTime,
                    LoadedIn = ApplicationHelper.GetProcessStartupDuration(),
                    IsInteractive = Environment.UserInteractive,
                    IsOptimized = IsOptimized(),
                    Is64Bit = Environment.Is64BitProcess,
                    IsLargeAddressAware = ApplicationHelper.IsProcessLargeAddressAware(),
                    WorkingSetInMegaBytes = UnitConverter.BytesToMegaBytes(Environment.WorkingSet),
                    FileVersion = pVerInfo.FileVersion,
                    ProductVersion = pVerInfo.ProductVersion,
                    Language = pVerInfo.Language,
                    Copyright = pVerInfo.LegalCopyright,
                    OriginalFileName = pVerInfo.OriginalFilename,
                    FileName = pVerInfo.FileName,
                    ModuleName = p.MainModule.ModuleName,
                    ModuleFileName = p.MainModule.FileName,
                    ProductName = pVerInfo.ProductName,
                    CommandLine = Environment.GetCommandLineArgs()
                });

                string IsOptimized()
                {
                    var executingAssembly = Assembly.GetEntryAssembly();

                    return(executingAssembly == null
                        ? "N/A - Assembly was called from Unmanaged code."
                        : executingAssembly.IsOptimized().ToString());
                }
            }
        }
示例#10
0
 private static AssemblyDetails[] GetAssemblies(DiagnosticReportType type)
 {
     return(!IsReportEnabled(type, DiagnosticReportType.Assemblies)
         ? null
         : AppDomain.CurrentDomain.GetAssemblies()
            .Where(ass => !ass.IsDynamic)
            .OrderByDescending(o => o.GlobalAssemblyCache)
            .Select(ass => new AssemblyDetails
     {
         Name = ass.FullName,
         IsGAC = ass.GlobalAssemblyCache,
         Is64Bit = !ass.Is32Bit(),
         IsOptimized = ass.IsOptimized(),
         Framework = ass.GetFrameworkVersion(),
         Location = ass.Location,
         CodeBase = new Uri(ass.CodeBase),
     })
            .ToArray());
 }
示例#11
0
        private static string GenerateImpl(DiagnosticReportType type)
        {
            var sw = Stopwatch.StartNew();

            var builder = new StringBuilder();

            for (var i = 0; i < Actions.Length; i++)
            {
                var enabled = ((int)type & (1 << i)) != 0;
                if (enabled)
                {
                    Actions[i](builder);
                }
            }

            sw.Stop();

            builder.Insert(0, $"/{NewLine}{Pipe}Diagnostic Report generated at: {DateTime.Now:dd-MM-yyyy HH:mm:ss.fff} in: {sw.Elapsed.TotalMilliseconds} milliseconds.{NewLine}");
            builder.Append('\\');
            return(builder.ToString());
        }
示例#12
0
 private static SystemDetails GetSystemDetails(DiagnosticReportType type)
 {
     if (!IsReportEnabled(type, DiagnosticReportType.System))
     {
         return(default);
示例#13
0
 /// <summary>
 /// Generates and returns an instance of <see cref="DiagnosticReport"/>.
 /// </summary>
 public static DiagnosticReport Generate(DiagnosticReportType type = DiagnosticReportType.Full) => new DiagnosticReport(type);
示例#14
0
        private static bool IsReportEnabled(DiagnosticReportType requestedType, DiagnosticReportType section)
        {
            var indexOf = Array.IndexOf(ReportSections, section);

            return(((int)requestedType & (1 << indexOf)) != 0);
        }