Skip to content

Commit a91bca8

Browse files
committed
Make EnvironmentInfoCollector working on Mono.
- Fixes #20.
1 parent 76d8f6f commit a91bca8

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

Source/Extras/Exceptionless.Extras.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="Utility\AssemblyHelper.cs" />
7575
<Compile Include="Storage\FolderObjectStorage.cs" />
7676
<Compile Include="Storage\IsolatedStorageObjectStorage.cs" />
77+
<Compile Include="Utility\EnvironmentHelper.cs" />
7778
<Compile Include="Utility\ExceptionlessTraceListener.cs" />
7879
<Compile Include="Utility\PathHelper.cs" />
7980
<Compile Include="Utility\Run.cs" />

Source/Extras/Services/EnvironmentInfoCollector.cs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.InteropServices;
88
using System.Text;
99
using System.Threading;
10+
using Exceptionless.Extras.Utility;
1011
using Exceptionless.Logging;
1112
using Exceptionless.Models.Data;
1213
using Microsoft.VisualBasic.Devices;
@@ -43,10 +44,24 @@ public EnvironmentInfo GetEnvironmentInfo() {
4344
}
4445

4546
try {
46-
if (computerInfo != null)
47-
info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
48-
if (computerInfo != null)
49-
info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
47+
if (EnvironmentHelper.IsUnix)
48+
{
49+
if (PerformanceCounterCategory.Exists("Mono Memory"))
50+
{
51+
//https://github.com/mono/mono/blob/f0834d5407f492a2a21e2f62f8f8c418d64ba6fa/mono/metadata/mono-perfcounters-def.h
52+
var performanceCounterTotalPhysicalMemory = new PerformanceCounter("Mono Memory", "Total Physical Memory");
53+
var performanceCounterAvailablePhysicalMemory = new PerformanceCounter("Mono Memory", "Available Physical Memory"); //mono 4.0+
54+
info.TotalPhysicalMemory = Convert.ToInt64(performanceCounterTotalPhysicalMemory.RawValue);
55+
info.AvailablePhysicalMemory = Convert.ToInt64(performanceCounterAvailablePhysicalMemory.RawValue);
56+
}
57+
}
58+
else
59+
{
60+
if (computerInfo != null)
61+
info.TotalPhysicalMemory = Convert.ToInt64(computerInfo.TotalPhysicalMemory);
62+
if (computerInfo != null)
63+
info.AvailablePhysicalMemory = Convert.ToInt64(computerInfo.AvailablePhysicalMemory);
64+
}
5065
} catch (Exception ex) {
5166
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get physical memory. Error message: {0}", ex.Message);
5267
}
@@ -85,19 +100,42 @@ public EnvironmentInfo GetEnvironmentInfo() {
85100
}
86101

87102
try {
88-
info.ProcessId = KernelNativeMethods.GetCurrentProcessId().ToString(NumberFormatInfo.InvariantInfo);
103+
if (EnvironmentHelper.IsUnix)
104+
{
105+
var currentProcess = Process.GetCurrentProcess();
106+
info.ProcessId = currentProcess.Id.ToString();
107+
}
108+
else
109+
{
110+
info.ProcessId = KernelNativeMethods.GetCurrentProcessId().ToString(NumberFormatInfo.InvariantInfo);
111+
}
89112
} catch (Exception ex) {
90113
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process id. Error message: {0}", ex.Message);
91114
}
92115

93116
try {
94-
info.ProcessName = GetProcessName();
117+
if (EnvironmentHelper.IsUnix)
118+
{
119+
var currentProcess = Process.GetCurrentProcess();
120+
info.ProcessName = currentProcess.ProcessName;
121+
}
122+
else
123+
{
124+
info.ProcessName = GetProcessName();
125+
}
95126
} catch (Exception ex) {
96127
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get process name. Error message: {0}", ex.Message);
97128
}
98129

99130
try {
100-
info.ThreadId = KernelNativeMethods.GetCurrentThreadId().ToString(NumberFormatInfo.InvariantInfo);
131+
if (EnvironmentHelper.IsUnix)
132+
{
133+
info.ThreadId = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString();
134+
}
135+
else
136+
{
137+
info.ThreadId = KernelNativeMethods.GetCurrentThreadId().ToString(NumberFormatInfo.InvariantInfo);
138+
}
101139
} catch (Exception ex) {
102140
_log.FormattedInfo(typeof(EnvironmentInfoCollector), "Unable to get thread id. Error message: {0}", ex.Message);
103141
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace Exceptionless.Extras.Utility
4+
{
5+
public static class EnvironmentHelper
6+
{
7+
/// <summary>
8+
/// Determine current os platform.
9+
/// </summary>
10+
/// <exception cref="InvalidOperationException" accessor="get"></exception>
11+
public static bool IsUnix
12+
{
13+
get
14+
{
15+
int p = (int)Environment.OSVersion.Platform;
16+
return (p == 4) || (p == 6) || (p == 128);
17+
}
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)