1313namespace Exceptionless . Services {
1414 public class DefaultEnvironmentInfoCollector : IEnvironmentInfoCollector {
1515 private static EnvironmentInfo _environmentInfo ;
16- private readonly ExceptionlessConfiguration _config ;
17- private readonly IExceptionlessLog _log ;
16+ protected ExceptionlessConfiguration Config { get ; }
17+ protected IExceptionlessLog Log { get ; }
1818
1919 public DefaultEnvironmentInfoCollector ( ExceptionlessConfiguration config , IExceptionlessLog log ) {
20- _config = config ;
21- _log = log ;
20+ Config = config ;
21+ Log = log ;
2222 }
2323
24- public EnvironmentInfo GetEnvironmentInfo ( ) {
24+ public virtual EnvironmentInfo GetEnvironmentInfo ( ) {
2525 if ( _environmentInfo != null ) {
2626 PopulateThreadInfo ( _environmentInfo ) ;
2727 PopulateMemoryInfo ( _environmentInfo ) ;
2828 return _environmentInfo ;
2929 }
3030
3131 var info = new EnvironmentInfo ( ) ;
32+ PopulateApplicationInfo ( info ) ;
3233 PopulateRuntimeInfo ( info ) ;
3334 PopulateProcessInfo ( info ) ;
3435 PopulateThreadInfo ( info ) ;
@@ -42,16 +43,16 @@ private void PopulateApplicationInfo(EnvironmentInfo info) {
4243 try {
4344 info . Data . Add ( "AppDomainName" , AppDomain . CurrentDomain . FriendlyName ) ;
4445 } 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 ) ;
4647 }
4748
48- if ( _config . IncludeIpAddress ) {
49+ if ( Config . IncludeIpAddress ) {
4950 try {
5051 IPHostEntry hostEntry = Dns . GetHostEntryAsync ( Dns . GetHostName ( ) ) . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
5152 if ( hostEntry != null && hostEntry . AddressList . Any ( ) )
5253 info . IpAddress = String . Join ( ", " , hostEntry . AddressList . Where ( x => x . AddressFamily == AddressFamily . InterNetwork ) . Select ( a => a . ToString ( ) ) . ToArray ( ) ) ;
5354 } 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 ) ;
5556 }
5657 }
5758 }
@@ -60,44 +61,46 @@ private void PopulateProcessInfo(EnvironmentInfo info) {
6061 try {
6162 info . ProcessorCount = Environment . ProcessorCount ;
6263 } 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 ) ;
6465 }
6566
6667 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+ }
7072 } 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 ) ;
7274 }
7375
7476 try {
7577 info . CommandLine = Environment . CommandLine ;
7678 } 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 ) ;
7880 }
7981 }
8082
8183 private void PopulateThreadInfo ( EnvironmentInfo info ) {
8284 try {
8385 info . ThreadId = Thread . CurrentThread . ManagedThreadId . ToString ( NumberFormatInfo . InvariantInfo ) ;
8486 } 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 ) ;
8688 }
8789
8890 try {
8991 info . ThreadName = Thread . CurrentThread . Name ;
9092 } 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 ) ;
9294 }
9395 }
9496
9597 private void PopulateMemoryInfo ( EnvironmentInfo info ) {
9698 try {
97- Process process = Process . GetCurrentProcess ( ) ;
98- info . ProcessMemorySize = process . PrivateMemorySize64 ;
99+ using ( Process process = Process . GetCurrentProcess ( ) ) {
100+ info . ProcessMemorySize = process . PrivateMemorySize64 ;
101+ }
99102 } 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 ) ;
101104 }
102105
103106#if NET45
@@ -116,7 +119,7 @@ private void PopulateMemoryInfo(EnvironmentInfo info) {
116119 info . AvailablePhysicalMemory = Convert . ToInt64 ( computerInfo . AvailablePhysicalMemory ) ;
117120 }
118121 } 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 ) ;
120123 }
121124#endif
122125 }
@@ -142,11 +145,11 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
142145 info . Data [ "ProcessArchitecture" ] = RuntimeInformation . ProcessArchitecture . ToString ( ) ;
143146#endif
144147
145- if ( _config . IncludeMachineName ) {
148+ if ( Config . IncludeMachineName ) {
146149 try {
147150 info . MachineName = Environment . MachineName ;
148151 } 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 ) ;
150153 }
151154 }
152155
@@ -163,7 +166,7 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
163166 computerInfo = new Microsoft . VisualBasic . Devices . ComputerInfo ( ) ;
164167#endif
165168 } 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 ) ;
167170 }
168171
169172 if ( computerInfo == null )
@@ -182,7 +185,7 @@ private void PopulateRuntimeInfo(EnvironmentInfo info) {
182185 info . Architecture = Is64BitOperatingSystem ( ) ? "x64" : "x86" ;
183186#endif
184187 } 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 ) ;
186189 }
187190 }
188191
@@ -225,7 +228,7 @@ private bool Is64BitOperatingSystem() {
225228
226229 return ( ( methodExist && KernelNativeMethods . IsWow64Process ( KernelNativeMethods . GetCurrentProcess ( ) , out is64 ) ) && is64 ) ;
227230 } 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 ) ;
229232 }
230233
231234 return false ;
0 commit comments