13
13
namespace Exceptionless . Services {
14
14
public class DefaultEnvironmentInfoCollector : IEnvironmentInfoCollector {
15
15
private static EnvironmentInfo _environmentInfo ;
16
- private readonly ExceptionlessConfiguration _config ;
17
- private readonly IExceptionlessLog _log ;
16
+ protected ExceptionlessConfiguration Config { get ; }
17
+ protected IExceptionlessLog Log { get ; }
18
18
19
19
public DefaultEnvironmentInfoCollector ( ExceptionlessConfiguration config , IExceptionlessLog log ) {
20
- _config = config ;
21
- _log = log ;
20
+ Config = config ;
21
+ Log = log ;
22
22
}
23
23
24
- public EnvironmentInfo GetEnvironmentInfo ( ) {
24
+ public virtual EnvironmentInfo GetEnvironmentInfo ( ) {
25
25
if ( _environmentInfo != null ) {
26
26
PopulateThreadInfo ( _environmentInfo ) ;
27
27
PopulateMemoryInfo ( _environmentInfo ) ;
28
28
return _environmentInfo ;
29
29
}
30
30
31
31
var info = new EnvironmentInfo ( ) ;
32
+ PopulateApplicationInfo ( info ) ;
32
33
PopulateRuntimeInfo ( info ) ;
33
34
PopulateProcessInfo ( info ) ;
34
35
PopulateThreadInfo ( info ) ;
@@ -42,16 +43,16 @@ private void PopulateApplicationInfo(EnvironmentInfo info) {
42
43
try {
43
44
info . Data . Add ( "AppDomainName" , AppDomain . CurrentDomain . FriendlyName ) ;
44
45
} catch ( Exception ex ) {
45
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get AppDomain friendly name. Error message: {0}" , ex . Message ) ;
46
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get AppDomain friendly name. Error message: {0}" , ex . Message ) ;
46
47
}
47
48
48
- if ( _config . IncludeIpAddress ) {
49
+ if ( Config . IncludeIpAddress ) {
49
50
try {
50
51
IPHostEntry hostEntry = Dns . GetHostEntryAsync ( Dns . GetHostName ( ) ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
51
52
if ( hostEntry != null && hostEntry . AddressList . Any ( ) )
52
53
info . IpAddress = String . Join ( ", " , hostEntry . AddressList . Where ( x => x . AddressFamily == AddressFamily . InterNetwork ) . Select ( a => a . ToString ( ) ) . ToArray ( ) ) ;
53
54
} catch ( Exception ex ) {
54
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get ip address. Error message: {0}" , ex . Message ) ;
55
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get ip address. Error message: {0}" , ex . Message ) ;
55
56
}
56
57
}
57
58
}
@@ -60,44 +61,46 @@ private void PopulateProcessInfo(EnvironmentInfo info) {
60
61
try {
61
62
info . ProcessorCount = Environment . ProcessorCount ;
62
63
} catch ( Exception ex ) {
63
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get processor count. Error message: {0}" , ex . Message ) ;
64
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get processor count. Error message: {0}" , ex . Message ) ;
64
65
}
65
66
66
67
try {
67
- Process process = Process . GetCurrentProcess ( ) ;
68
- info . ProcessName = process . ProcessName ;
69
- info . ProcessId = process . Id . ToString ( NumberFormatInfo . InvariantInfo ) ;
68
+ using ( Process process = Process . GetCurrentProcess ( ) ) {
69
+ info . ProcessName = process . ProcessName ;
70
+ info . ProcessId = process . Id . ToString ( NumberFormatInfo . InvariantInfo ) ;
71
+ }
70
72
} catch ( Exception ex ) {
71
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get process name or id. Error message: {0}" , ex . Message ) ;
73
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get process name or id. Error message: {0}" , ex . Message ) ;
72
74
}
73
75
74
76
try {
75
77
info . CommandLine = Environment . CommandLine ;
76
78
} catch ( Exception ex ) {
77
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get command line. Error message: {0}" , ex . Message ) ;
79
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get command line. Error message: {0}" , ex . Message ) ;
78
80
}
79
81
}
80
82
81
83
private void PopulateThreadInfo ( EnvironmentInfo info ) {
82
84
try {
83
85
info . ThreadId = Thread . CurrentThread . ManagedThreadId . ToString ( NumberFormatInfo . InvariantInfo ) ;
84
86
} catch ( Exception ex ) {
85
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get thread id. Error message: {0}" , ex . Message ) ;
87
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get thread id. Error message: {0}" , ex . Message ) ;
86
88
}
87
89
88
90
try {
89
91
info . ThreadName = Thread . CurrentThread . Name ;
90
92
} catch ( Exception ex ) {
91
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get current thread name. Error message: {0}" , ex . Message ) ;
93
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get current thread name. Error message: {0}" , ex . Message ) ;
92
94
}
93
95
}
94
96
95
97
private void PopulateMemoryInfo ( EnvironmentInfo info ) {
96
98
try {
97
- Process process = Process . GetCurrentProcess ( ) ;
98
- info . ProcessMemorySize = process . PrivateMemorySize64 ;
99
+ using ( Process process = Process . GetCurrentProcess ( ) ) {
100
+ info . ProcessMemorySize = process . PrivateMemorySize64 ;
101
+ }
99
102
} catch ( Exception ex ) {
100
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get process memory size. Error message: {0}" , ex . Message ) ;
103
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get process memory size. Error message: {0}" , ex . Message ) ;
101
104
}
102
105
103
106
#if NET45
@@ -116,7 +119,7 @@ private void PopulateMemoryInfo(EnvironmentInfo info) {
116
119
info . AvailablePhysicalMemory = Convert . ToInt64 ( computerInfo . AvailablePhysicalMemory ) ;
117
120
}
118
121
} catch ( Exception ex ) {
119
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get physical memory. Error message: {0}" , ex . Message ) ;
122
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get physical memory. Error message: {0}" , ex . Message ) ;
120
123
}
121
124
#endif
122
125
}
@@ -142,11 +145,11 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
142
145
info . Data [ "ProcessArchitecture" ] = RuntimeInformation . ProcessArchitecture . ToString ( ) ;
143
146
#endif
144
147
145
- if ( _config . IncludeMachineName ) {
148
+ if ( Config . IncludeMachineName ) {
146
149
try {
147
150
info . MachineName = Environment . MachineName ;
148
151
} catch ( Exception ex ) {
149
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get machine name. Error message: {0}" , ex . Message ) ;
152
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get machine name. Error message: {0}" , ex . Message ) ;
150
153
}
151
154
}
152
155
@@ -163,7 +166,7 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
163
166
computerInfo = new Microsoft . VisualBasic . Devices . ComputerInfo ( ) ;
164
167
#endif
165
168
} catch ( Exception ex ) {
166
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get computer info. Error message: {0}" , ex . Message ) ;
169
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get computer info. Error message: {0}" , ex . Message ) ;
167
170
}
168
171
169
172
if ( computerInfo == null )
@@ -182,7 +185,7 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
182
185
info . Architecture = Is64BitOperatingSystem ( ) ? "x64" : "x86" ;
183
186
#endif
184
187
} catch ( Exception ex ) {
185
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get populate runtime info. Error message: {0}" , ex . Message ) ;
188
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get populate runtime info. Error message: {0}" , ex . Message ) ;
186
189
}
187
190
}
188
191
@@ -225,7 +228,7 @@ private bool Is64BitOperatingSystem() {
225
228
226
229
return ( ( methodExist && KernelNativeMethods . IsWow64Process ( KernelNativeMethods . GetCurrentProcess ( ) , out is64 ) ) && is64 ) ;
227
230
} catch ( Exception ex ) {
228
- _log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get CPU architecture. Error message: {0}" , ex . Message ) ;
231
+ Log . FormattedWarn ( typeof ( DefaultEnvironmentInfoCollector ) , "Unable to get CPU architecture. Error message: {0}" , ex . Message ) ;
229
232
}
230
233
231
234
return false ;
0 commit comments