11using EtabSharp . Core . Models ;
22using Microsoft . Extensions . Logging ;
3+ using Microsoft . Extensions . Logging . Abstractions ;
34using System . Diagnostics ;
45
56namespace EtabSharp . Core ;
@@ -21,19 +22,20 @@ public class ETABSWrapper
2122 /// <returns>ETABS application wrapper with typed access to API, or null if none found</returns>
2223 public static ETABSApplication Connect ( ILogger < ETABSApplication > ? logger = null )
2324 {
25+ var wrapperLogger = logger ?? NullLogger < ETABSApplication > . Instance ;
2426 var etabsProcesses = GetETABSProcesses ( ) ;
2527
2628 if ( ! etabsProcesses . Any ( ) )
2729 {
28- Console . WriteLine ( "No running ETABS instances found. " ) ;
30+ wrapperLogger . LogWarning ( "No running ETABS instances found" ) ;
2931 return null ;
3032 }
3133
32- var activeProcess = FindActiveProcess ( etabsProcesses ) ;
34+ var activeProcess = FindActiveProcess ( etabsProcesses , wrapperLogger ) ;
3335
3436 if ( activeProcess == null )
3537 {
36- Console . WriteLine ( "No ETABS instance with active window found. " ) ;
38+ wrapperLogger . LogWarning ( "No ETABS instance with active window found" ) ;
3739 return null ;
3840 }
3941
@@ -50,6 +52,8 @@ public static ETABSApplication Connect(ILogger<ETABSApplication>? logger = null)
5052 public static ETABSApplication CreateNew ( string programPath = null , bool startApplication = true ,
5153 ILogger < ETABSApplication > ? logger = null )
5254 {
55+ var wrapperLogger = logger ?? NullLogger < ETABSApplication > . Instance ;
56+
5357 try
5458 {
5559 // Create API helper object
@@ -59,11 +63,13 @@ public static ETABSApplication CreateNew(string programPath = null, bool startAp
5963 if ( ! string . IsNullOrEmpty ( programPath ) )
6064 {
6165 // Create instance from specified path
66+ wrapperLogger . LogDebug ( "Creating ETABS instance from path: {Path}" , programPath ) ;
6267 api = helper . CreateObject ( programPath ) ;
6368 }
6469 else
6570 {
6671 // Create instance from latest installed ETABS
72+ wrapperLogger . LogDebug ( "Creating ETABS instance using ProgID: {ProgID}" , ETABS_PROGID ) ;
6773 api = helper . CreateObjectProgID ( ETABS_PROGID ) ;
6874 }
6975
@@ -73,23 +79,23 @@ public static ETABSApplication CreateNew(string programPath = null, bool startAp
7379 int ret = api . ApplicationStart ( ) ;
7480 if ( ret != 0 )
7581 {
76- Console . WriteLine ( "Warning: ApplicationStart returned non-zero value" ) ;
82+ wrapperLogger . LogWarning ( " ApplicationStart returned non-zero value: {ReturnValue}" , ret ) ;
7783 }
7884 }
7985
8086 // Get version info
81- var versionInfo = GetVersionFromAPI ( api ) ;
87+ var versionInfo = GetVersionFromAPI ( api , wrapperLogger ) ;
8288 int version = versionInfo . majorVersion ;
8389 string fullVersion = versionInfo . fullVersion ;
84- double apiVersion = GetApiVersionNumber ( helper ) ;
90+ double apiVersion = GetApiVersionNumber ( helper , wrapperLogger ) ;
8591
86- Console . WriteLine ( $ "Created new ETABS instance v{ fullVersion } , API Version: { apiVersion } " ) ;
92+ wrapperLogger . LogInformation ( "Created new ETABS instance v{Version }, API Version: {ApiVersion}" , fullVersion , apiVersion ) ;
8793
8894 return new ETABSApplication ( api , version , apiVersion , fullVersion , logger ) ;
8995 }
9096 catch ( Exception ex )
9197 {
92- Console . WriteLine ( $ "Error creating new ETABS instance: { ex . Message } ") ;
98+ wrapperLogger . LogError ( ex , "Error creating new ETABS instance" ) ;
9399 return null ;
94100 }
95101 }
@@ -105,7 +111,7 @@ private static List<Process> GetETABSProcesses()
105111 /// <summary>
106112 /// Finds the first ETABS process with an active main window
107113 /// </summary>
108- private static ETABSProcessInfo FindActiveProcess ( List < Process > processes )
114+ private static ETABSProcessInfo FindActiveProcess ( List < Process > processes , ILogger logger )
109115 {
110116 foreach ( var process in processes )
111117 {
@@ -128,7 +134,7 @@ private static ETABSProcessInfo FindActiveProcess(List<Process> processes)
128134 }
129135 catch ( Exception ex )
130136 {
131- Console . WriteLine ( $ "Error reading process info: { ex . Message } " ) ;
137+ logger . LogWarning ( ex , "Error reading process info for PID {ProcessId}" , process . Id ) ;
132138 continue ;
133139 }
134140 }
@@ -142,28 +148,31 @@ private static ETABSProcessInfo FindActiveProcess(List<Process> processes)
142148 /// </summary>
143149 private static ETABSApplication ConnectToETABS ( ETABSProcessInfo processInfo , ILogger < ETABSApplication > ? logger )
144150 {
151+ var wrapperLogger = logger ?? NullLogger < ETABSApplication > . Instance ;
152+
145153 if ( processInfo . MajorVersion == 0 )
146154 {
147- logger ? . LogWarning ( "Unable to determine ETABS version. " ) ;
155+ wrapperLogger . LogWarning ( "Unable to determine ETABS version" ) ;
148156 return null ;
149157 }
150158
151159 if ( processInfo . MajorVersion < MINIMUM_SUPPORTED_VERSION )
152160 {
153- logger ? . LogWarning ( $ "ETABS v{ processInfo . FullVersion } is not supported.") ;
154- logger ? . LogWarning ( $ "This wrapper requires ETABS v{ MINIMUM_SUPPORTED_VERSION } or newer.") ;
155- logger ? . LogWarning ( "Please upgrade your ETABS installation." ) ;
161+ wrapperLogger . LogWarning (
162+ "ETABS v{Version} is not supported. This wrapper requires ETABS v{MinVersion} or newer. Please upgrade your ETABS installation." ,
163+ processInfo . FullVersion ,
164+ MINIMUM_SUPPORTED_VERSION ) ;
156165 return null ;
157166 }
158167
159168 try
160169 {
161- logger ? . LogInformation ( $ "Connecting to ETABS v{ processInfo . FullVersion } ...") ;
170+ wrapperLogger . LogInformation ( "Connecting to ETABS v{Version }..." , processInfo . FullVersion ) ;
162171 return CreateETABSApplication ( processInfo . MajorVersion , processInfo . FullVersion , logger ) ;
163172 }
164173 catch ( Exception ex )
165174 {
166- logger ? . LogError ( ex , $ "Error connecting to ETABS v{ processInfo . FullVersion } " ) ;
175+ wrapperLogger . LogError ( ex , "Error connecting to ETABS v{Version}" , processInfo . FullVersion ) ;
167176 return null ;
168177 }
169178 }
@@ -174,6 +183,8 @@ private static ETABSApplication ConnectToETABS(ETABSProcessInfo processInfo, ILo
174183 private static ETABSApplication CreateETABSApplication ( int majorVersion , string fullVersion ,
175184 ILogger < ETABSApplication > ? logger )
176185 {
186+ var wrapperLogger = logger ?? NullLogger < ETABSApplication > . Instance ;
187+
177188 try
178189 {
179190 // Create helper object
@@ -183,62 +194,65 @@ private static ETABSApplication CreateETABSApplication(int majorVersion, string
183194 ETABSv1 . cOAPI api = helper . GetObject ( ETABS_PROGID ) ;
184195
185196 // Get the API version number
186- double apiVersion = GetApiVersionNumber ( helper ) ;
197+ double apiVersion = GetApiVersionNumber ( helper , wrapperLogger ) ;
187198
188- logger ? . LogInformation ( $ "Connected to ETABS v{ fullVersion } , API Version: { apiVersion } ") ;
189- Console . WriteLine ( $ "Connected to ETABS v{ fullVersion } , API Version: { apiVersion } ") ;
199+ wrapperLogger . LogInformation ( "Connected to ETABS v{Version}, API Version: {ApiVersion}" , fullVersion , apiVersion ) ;
190200
191201 return new ETABSApplication ( api , majorVersion , apiVersion , fullVersion , logger ) ;
192202 }
193203 catch ( Exception ex )
194204 {
195- Console . WriteLine ( $ "Failed to attach to ETABS: { ex . Message } ") ;
205+ wrapperLogger . LogError ( ex , "Failed to attach to ETABS" ) ;
196206 throw ;
197207 }
198208 }
199209
200210 /// <summary>
201211 /// Gets the API version number from helper
202212 /// </summary>
203- private static double GetApiVersionNumber ( ETABSv1 . cHelper helper )
213+ private static double GetApiVersionNumber ( ETABSv1 . cHelper helper , ILogger logger )
204214 {
205215 try
206216 {
207217 return helper . GetOAPIVersionNumber ( ) ;
208218 }
209- catch
219+ catch ( Exception ex )
210220 {
221+ logger . LogWarning ( ex , "Unable to get API version number" ) ;
211222 return 0.0 ;
212223 }
213224 }
214225
215226 /// <summary>
216227 /// Gets version from API object by examining process
217228 /// </summary>
218- private static ( int majorVersion , string fullVersion ) GetVersionFromAPI ( ETABSv1 . cOAPI api )
229+ private static ( int majorVersion , string fullVersion ) GetVersionFromAPI ( ETABSv1 . cOAPI api , ILogger logger )
219230 {
220231 try
221232 {
222233 var processes = GetETABSProcesses ( ) ;
223- var activeProcess = FindActiveProcess ( processes ) ;
234+ var activeProcess = FindActiveProcess ( processes , logger ) ;
224235 if ( activeProcess != null )
225236 {
226237 return ( activeProcess . MajorVersion , activeProcess . FullVersion ) ;
227238 }
228239
240+ logger . LogWarning ( "Unable to determine ETABS version from process, defaulting to v22.0.0" ) ;
229241 return ( 22 , "22.0.0" ) ; // Default to 22.0.0 if can't determine
230242 }
231- catch
243+ catch ( Exception ex )
232244 {
245+ logger . LogWarning ( ex , "Error getting version from API, defaulting to v22.0.0" ) ;
233246 return ( 22 , "22.0.0" ) ; // Default to 22.0.0
234247 }
235248 }
236249
237250 /// <summary>
238251 /// Gets list of all running ETABS instances with their version info
239252 /// </summary>
240- public static List < ETABSInstanceInfo > GetAllRunningInstances ( )
253+ public static List < ETABSInstanceInfo > GetAllRunningInstances ( ILogger ? logger = null )
241254 {
255+ var wrapperLogger = logger ?? NullLogger . Instance ;
242256 var instances = new List < ETABSInstanceInfo > ( ) ;
243257 var processes = GetETABSProcesses ( ) ;
244258
@@ -264,7 +278,7 @@ public static List<ETABSInstanceInfo> GetAllRunningInstances()
264278 }
265279 catch ( Exception ex )
266280 {
267- Console . WriteLine ( $ "Error reading process { process . Id } : { ex . Message } " ) ;
281+ wrapperLogger . LogWarning ( ex , "Error reading process {ProcessId}" , process . Id ) ;
268282 }
269283 }
270284
@@ -282,20 +296,22 @@ public static bool IsRunning()
282296 /// <summary>
283297 /// Checks if a supported version of ETABS (v22+) is running
284298 /// </summary>
285- public static bool IsSupportedVersionRunning ( )
299+ public static bool IsSupportedVersionRunning ( ILogger ? logger = null )
286300 {
301+ var wrapperLogger = logger ?? NullLogger . Instance ;
287302 var processes = GetETABSProcesses ( ) ;
288- var activeProcess = FindActiveProcess ( processes ) ;
303+ var activeProcess = FindActiveProcess ( processes , wrapperLogger ) ;
289304 return activeProcess != null && activeProcess . MajorVersion >= MINIMUM_SUPPORTED_VERSION ;
290305 }
291306
292307 /// <summary>
293308 /// Gets the version of the active ETABS instance
294309 /// </summary>
295- public static string GetActiveVersion ( )
310+ public static string GetActiveVersion ( ILogger ? logger = null )
296311 {
312+ var wrapperLogger = logger ?? NullLogger . Instance ;
297313 var processes = GetETABSProcesses ( ) ;
298- var activeProcess = FindActiveProcess ( processes ) ;
314+ var activeProcess = FindActiveProcess ( processes , wrapperLogger ) ;
299315 return activeProcess ? . FullVersion ;
300316 }
301317}
0 commit comments