Skip to content

Commit 299def5

Browse files
committed
Merge pull request #21 from bitbeans/master
Make EnvironmentInfoCollector working on Mono.
2 parents 76d8f6f + a000db0 commit 299def5

File tree

1 file changed

+58
-9
lines changed

1 file changed

+58
-9
lines changed

Source/Extras/Services/EnvironmentInfoCollector.cs

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,32 @@ public EnvironmentInfo GetEnvironmentInfo() {
3434
}
3535

3636
try {
37-
if (computerInfo != null)
37+
if (computerInfo != null) {
3838
info.OSName = computerInfo.OSFullName;
39-
if (computerInfo != null)
4039
info.OSVersion = computerInfo.OSVersion;
40+
}
4141
} catch (Exception ex) {
4242
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get operating system version. Error message: {0}", ex.Message);
4343
}
4444

4545
try {
46-
if (computerInfo != null)
47-
info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
48-
if (computerInfo != null)
49-
info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
46+
if (IsUnix)
47+
{
48+
if (PerformanceCounterCategory.Exists("Mono Memory"))
49+
{
50+
var totalPhysicalMemory = new PerformanceCounter("Mono Memory", "Total Physical Memory");
51+
var availablePhysicalMemory = new PerformanceCounter("Mono Memory", "Available Physical Memory"); //mono 4.0+
52+
info.TotalPhysicalMemory = Convert.ToInt64(totalPhysicalMemory.RawValue);
53+
info.AvailablePhysicalMemory = Convert.ToInt64(availablePhysicalMemory.RawValue);
54+
}
55+
}
56+
else
57+
{
58+
if (computerInfo != null) {
59+
info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
60+
info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
61+
}
62+
}
5063
} catch (Exception ex) {
5164
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get physical memory. Error message: {0}", ex.Message);
5265
}
@@ -85,19 +98,42 @@ public EnvironmentInfo GetEnvironmentInfo() {
8598
}
8699

87100
try {
88-
info.ProcessId = KernelNativeMethods.GetCurrentProcessId().ToString(NumberFormatInfo.InvariantInfo);
101+
if (IsUnix)
102+
{
103+
var currentProcess = Process.GetCurrentProcess();
104+
info.ProcessId = currentProcess.Id.ToString(NumberFormatInfo.InvariantInfo);
105+
}
106+
else
107+
{
108+
info.ProcessId = KernelNativeMethods.GetCurrentProcessId().ToString(NumberFormatInfo.InvariantInfo);
109+
}
89110
} catch (Exception ex) {
90111
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process id. Error message: {0}", ex.Message);
91112
}
92113

93114
try {
94-
info.ProcessName = GetProcessName();
115+
if (IsUnix)
116+
{
117+
var currentProcess = Process.GetCurrentProcess();
118+
info.ProcessName = currentProcess.ProcessName;
119+
}
120+
else
121+
{
122+
info.ProcessName = GetProcessName();
123+
}
95124
} catch (Exception ex) {
96125
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process name. Error message: {0}", ex.Message);
97126
}
98127

99128
try {
100-
info.ThreadId = KernelNativeMethods.GetCurrentThreadId().ToString(NumberFormatInfo.InvariantInfo);
129+
if (IsUnix)
130+
{
131+
info.ThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(NumberFormatInfo.InvariantInfo);
132+
}
133+
else
134+
{
135+
info.ThreadId = KernelNativeMethods.GetCurrentThreadId().ToString(NumberFormatInfo.InvariantInfo);
136+
}
101137
} catch (Exception ex) {
102138
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get thread id. Error message: {0}", ex.Message);
103139
}
@@ -146,6 +182,19 @@ private static bool Is64BitOperatingSystem() {
146182

147183
return ((methodExist && KernelNativeMethods.IsWow64Process(KernelNativeMethods.GetCurrentProcess(), out is64)) && is64);
148184
}
185+
186+
/// <summary>
187+
/// Determine current os platform.
188+
/// </summary>
189+
/// <exception cref="InvalidOperationException" accessor="get"></exception>
190+
private static bool IsUnix
191+
{
192+
get
193+
{
194+
int p = (int)Environment.OSVersion.Platform;
195+
return (p == 4) || (p == 6) || (p == 128);
196+
}
197+
}
149198
}
150199

151200
internal static class KernelNativeMethods {

0 commit comments

Comments
 (0)