@@ -34,19 +34,32 @@ public EnvironmentInfo GetEnvironmentInfo() {
34
34
}
35
35
36
36
try {
37
- if ( computerInfo != null )
37
+ if ( computerInfo != null ) {
38
38
info . OSName = computerInfo . OSFullName ;
39
- if ( computerInfo != null )
40
39
info . OSVersion = computerInfo . OSVersion ;
40
+ }
41
41
} catch ( Exception ex ) {
42
42
_log . FormattedInfo ( typeof ( EnvironmentInfoCollector ) , "Unable to get operating system version. Error message: {0}" , ex . Message ) ;
43
43
}
44
44
45
45
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
+ }
50
63
} catch ( Exception ex ) {
51
64
_log . FormattedInfo ( typeof ( EnvironmentInfoCollector ) , "Unable to get physical memory. Error message: {0}" , ex . Message ) ;
52
65
}
@@ -85,19 +98,42 @@ public EnvironmentInfo GetEnvironmentInfo() {
85
98
}
86
99
87
100
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
+ }
89
110
} catch ( Exception ex ) {
90
111
_log . FormattedInfo ( typeof ( EnvironmentInfoCollector ) , "Unable to get process id. Error message: {0}" , ex . Message ) ;
91
112
}
92
113
93
114
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
+ }
95
124
} catch ( Exception ex ) {
96
125
_log . FormattedInfo ( typeof ( EnvironmentInfoCollector ) , "Unable to get process name. Error message: {0}" , ex . Message ) ;
97
126
}
98
127
99
128
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
+ }
101
137
} catch ( Exception ex ) {
102
138
_log . FormattedInfo ( typeof ( EnvironmentInfoCollector ) , "Unable to get thread id. Error message: {0}" , ex . Message ) ;
103
139
}
@@ -146,6 +182,19 @@ private static bool Is64BitOperatingSystem() {
146
182
147
183
return ( ( methodExist && KernelNativeMethods . IsWow64Process ( KernelNativeMethods . GetCurrentProcess ( ) , out is64 ) ) && is64 ) ;
148
184
}
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
+ }
149
198
}
150
199
151
200
internal static class KernelNativeMethods {
0 commit comments