split startup data logging to a new class and required a messagecollector to be passed in

This commit is contained in:
David Sparer
2017-02-03 10:13:37 -07:00
parent 88ec186b98
commit 356effc6a9
5 changed files with 126 additions and 96 deletions

View File

@@ -0,0 +1,119 @@
using System;
using System.Management;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.Messages;
namespace mRemoteNG.App.Initialization
{
public class StartupDataLogger
{
private readonly MessageCollector2 _messageCollector;
public StartupDataLogger(MessageCollector2 messageCollector)
{
if (messageCollector == null)
throw new ArgumentNullException(nameof(messageCollector));
_messageCollector = messageCollector;
}
public void LogStartupData()
{
LogApplicationData();
LogCmdLineArgs();
LogSystemData();
LogClrData();
LogCultureData();
}
private void LogSystemData()
{
var osData = GetOperatingSystemData();
var architecture = GetArchitectureData();
var nonEmptyData = Array.FindAll(new[] {osData, architecture}, s => !string.IsNullOrEmpty(s));
var data = string.Join(" ", nonEmptyData);
_messageCollector.AddMessage(MessageClass.InformationMsg, data, true);
}
private string GetOperatingSystemData()
{
var osVersion = string.Empty;
var servicePack = string.Empty;
try
{
foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem WHERE Primary=True").Get())
{
var managementObject = (ManagementObject)o;
osVersion = Convert.ToString(managementObject.GetPropertyValue("Caption")).Trim();
servicePack = GetOSServicePack(servicePack, managementObject);
}
}
catch (Exception ex)
{
_messageCollector.AddExceptionMessage("Error retrieving operating system information from WMI.", ex);
}
var osData = string.Join(" ", osVersion, servicePack);
return osData;
}
private string GetOSServicePack(string servicePack, ManagementObject managementObject)
{
var servicePackNumber = Convert.ToInt32(managementObject.GetPropertyValue("ServicePackMajorVersion"));
if (servicePackNumber != 0)
{
servicePack = $"Service Pack {servicePackNumber}";
}
return servicePack;
}
private string GetArchitectureData()
{
var architecture = string.Empty;
try
{
foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_Processor WHERE DeviceID=\'CPU0\'").Get())
{
var managementObject = (ManagementObject)o;
var addressWidth = Convert.ToInt32(managementObject.GetPropertyValue("AddressWidth"));
architecture = $"{addressWidth}-bit";
}
}
catch (Exception ex)
{
_messageCollector.AddExceptionMessage("Error retrieving operating system address width from WMI.", ex);
}
return architecture;
}
private void LogApplicationData()
{
var data = $"{Application.ProductName} {Application.ProductVersion}";
#if !PORTABLE
data += $" {Language.strLabelPortableEdition}";
#endif
data += " starting.";
_messageCollector.AddMessage(MessageClass.InformationMsg, data, true);
}
private void LogCmdLineArgs()
{
var data = $"Command Line: {string.Join(" ", Environment.GetCommandLineArgs())}";
_messageCollector.AddMessage(MessageClass.InformationMsg, data, true);
}
private void LogClrData()
{
var data = $"Microsoft .NET CLR {Environment.Version}";
_messageCollector.AddMessage(MessageClass.InformationMsg, data, true);
}
private void LogCultureData()
{
var data = $"System Culture: {Thread.CurrentThread.CurrentUICulture.Name}/{Thread.CurrentThread.CurrentUICulture.NativeName}";
_messageCollector.AddMessage(MessageClass.InformationMsg, data, true);
}
}
}

View File

@@ -26,7 +26,6 @@ namespace mRemoteNG.App
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Startup.Instance.InitializeProgram();
Application.Run(frmMain.Default);
}

View File

@@ -8,6 +8,7 @@ using System.Management;
using System.Threading;
using System.Windows.Forms;
using mRemoteNG.App.Info;
using mRemoteNG.App.Initialization;
using mRemoteNG.App.Update;
using mRemoteNG.Config.Connections;
using mRemoteNG.Config.Connections.Multiuser;
@@ -34,10 +35,11 @@ namespace mRemoteNG.App
{
}
public void InitializeProgram()
public void InitializeProgram(MessageCollector2 messageCollector)
{
Debug.Print("---------------------------" + Environment.NewLine + "[START] - " + Convert.ToString(DateTime.Now, CultureInfo.InvariantCulture));
LogStartupData();
var startupLogger = new StartupDataLogger(messageCollector);
startupLogger.LogStartupData();
CompatibilityChecker.CheckCompatibility();
ParseCommandLineArgs();
IeBrowserEmulation.Register();
@@ -62,99 +64,6 @@ namespace mRemoteNG.App
}
}
private static void LogStartupData()
{
if (!Settings.Default.WriteLogFile) return;
LogApplicationData();
LogCmdLineArgs();
LogSystemData();
LogCLRData();
LogCultureData();
}
private static void LogSystemData()
{
var osData = GetOperatingSystemData();
var architecture = GetArchitectureData();
Logger.Instance.InfoFormat(string.Join(" ", Array.FindAll(new[] { osData, architecture }, s => !string.IsNullOrEmpty(Convert.ToString(s)))));
}
private static string GetOperatingSystemData()
{
var osVersion = string.Empty;
var servicePack = string.Empty;
try
{
foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem WHERE Primary=True").Get())
{
var managementObject = (ManagementObject) o;
osVersion = Convert.ToString(managementObject.GetPropertyValue("Caption")).Trim();
servicePack = GetOSServicePack(servicePack, managementObject);
}
}
catch (Exception ex)
{
Logger.Instance.WarnFormat($"Error retrieving operating system information from WMI. {ex.Message}");
}
var osData = string.Join(" ", osVersion, servicePack);
return osData;
}
private static string GetOSServicePack(string servicePack, ManagementObject managementObject)
{
var servicePackNumber = Convert.ToInt32(managementObject.GetPropertyValue("ServicePackMajorVersion"));
if (servicePackNumber != 0)
{
servicePack = $"Service Pack {servicePackNumber}";
}
return servicePack;
}
private static string GetArchitectureData()
{
var architecture = string.Empty;
try
{
foreach (var o in new ManagementObjectSearcher("SELECT * FROM Win32_Processor WHERE DeviceID=\'CPU0\'").Get())
{
var managementObject = (ManagementObject) o;
var addressWidth = Convert.ToInt32(managementObject.GetPropertyValue("AddressWidth"));
architecture = $"{addressWidth}-bit";
}
}
catch (Exception ex)
{
Logger.Instance.WarnFormat($"Error retrieving operating system address width from WMI. {ex.Message}");
}
return architecture;
}
private static void LogApplicationData()
{
#if !PORTABLE
Logger.Instance.InfoFormat($"{Application.ProductName} {Application.ProductVersion} starting.");
#else
Logger.Instance.InfoFormat(
$"{Application.ProductName} {Application.ProductVersion} {Language.strLabelPortableEdition} starting.");
#endif
}
private static void LogCmdLineArgs()
{
Logger.Instance.InfoFormat($"Command Line: {string.Join(" ", Environment.GetCommandLineArgs())}");
}
private static void LogCLRData()
{
Logger.Instance.InfoFormat($"Microsoft .NET CLR {Environment.Version}");
}
private static void LogCultureData()
{
Logger.Instance.InfoFormat(
$"System Culture: {Thread.CurrentThread.CurrentUICulture.Name}/{Thread.CurrentThread.CurrentUICulture.NativeName}");
}
public void CreateConnectionsProvider()
{

View File

@@ -202,6 +202,8 @@ namespace mRemoteNG.UI.Forms
MessageCollectorSetup.SetupMessageCollector(Runtime.MessageCollector, Runtime.MessageWriters);
MessageCollectorSetup.BuildMessageWritersFromSettings(Runtime.MessageWriters);
Startup.Instance.InitializeProgram(Runtime.MessageCollector);
ApplyLanguage();
PopulateQuickConnectProtocolMenu();
ThemeManager.ThemeChanged += ApplyThemes;

View File

@@ -112,6 +112,7 @@
<ItemGroup>
<Compile Include="App\CompatibilityChecker.cs" />
<Compile Include="App\Info\GeneralAppInfo.cs" />
<Compile Include="App\Initialization\StartupDataLogger.cs" />
<Compile Include="App\Logger.cs" />
<Compile Include="App\MessageCollectorSetup.cs" />
<Compile Include="App\NativeMethods.cs" />