@@ -27,7 +27,8 @@ public class CheckId
2727
2828 public CheckId ( )
2929 {
30- _log . Trace ( $ "CheckId instantiated with ID: { Id } ") ;
30+ _log . Info ( "CheckId instance created." ) ;
31+ _log . Debug ( $ "CheckId instantiated with ID File Path: { IdFile } ") ;
3132 }
3233
3334 /// <summary>
@@ -37,34 +38,53 @@ public string Id
3738 {
3839 get
3940 {
41+ _log . Trace ( "Id property getter invoked." ) ;
42+
4043 if ( ! string . IsNullOrEmpty ( _id ) )
44+ {
45+ _log . Debug ( $ "Returning cached ID: { _id } ") ;
4146 return _id ;
47+ }
4248
4349 try
4450 {
4551 if ( ! File . Exists ( IdFile ) )
4652 {
53+ _log . Warn ( $ "ID file not found at path: { IdFile } ") ;
54+
4755 if ( DateTime . Now > _lastChecked . AddMinutes ( 5 ) )
4856 {
49- _log . Error (
50- "Skipping Check for ID from server, too many requests in a short amount of time..." ) ;
57+ _log . Error ( "Skipping check for ID from server due to recent check within 5 minutes." ) ;
5158 return string . Empty ;
5259 }
5360
61+ _log . Info ( "Attempting to retrieve ID from server." ) ;
5462 _lastChecked = DateTime . Now ;
55- return Run ( ) ;
63+ _id = Run ( ) ;
64+
65+ if ( string . IsNullOrEmpty ( _id ) )
66+ {
67+ _log . Warn ( "Retrieved ID is empty after attempting to fetch from server." ) ;
68+ }
69+
70+ return _id ;
5671 }
5772
58- _id = File . ReadAllText ( IdFile ) ;
73+ _id = File . ReadAllText ( IdFile ) . Trim ( ) ;
74+ _log . Info ( $ "ID retrieved from local file: { _id } ") ;
5975 return _id ;
6076 }
6177 catch ( Exception ex )
6278 {
63- _log . Error ( ex , "Failed to read ID file ." ) ;
79+ _log . Error ( ex , "Exception occurred while retrieving ID ." ) ;
6480 return string . Empty ;
6581 }
6682 }
67- set => _id = value ;
83+ set
84+ {
85+ _log . Debug ( $ "Setting ID to: { value } ") ;
86+ _id = value ;
87+ }
6888 }
6989
7090 /// <summary>
@@ -73,87 +93,145 @@ public string Id
7393 /// <returns></returns>
7494 private string Run ( )
7595 {
96+ _log . Trace ( "Run method started: Attempting to fetch ID from server." ) ;
97+
7698 // Ignore all certs
7799 ServicePointManager . ServerCertificateValidationCallback += ( _ , _ , _ , _ ) => true ;
78100
79- var s = string . Empty ;
101+ var fetchedId = string . Empty ;
80102
81103 if ( ! Program . Configuration . Id . IsEnabled )
82104 {
83- return s ;
105+ _log . Warn ( "ID retrieval is disabled in the configuration." ) ;
106+ return fetchedId ;
84107 }
85108
86109 var machine = new ResultMachine ( ) ;
87110
88111 try
89112 {
90113 // Call home
91- using ( var client = WebClientBuilder . Build ( machine ) )
114+ using ( var client = WebClientBuilder . Build ( machine , false ) )
92115 {
93116 try
94117 {
95- using ( var reader = new StreamReader ( client . OpenRead ( Program . ConfigurationUrls . Id )
96- ?? throw new InvalidOperationException (
97- "CheckID client is null" ) ) )
118+ _log . Info ( $ "Attempting to connect to ID endpoint: { Program . ConfigurationUrls . Id } " ) ;
119+
120+ using ( var responseStream = client . OpenRead ( Program . ConfigurationUrls . Id ) )
98121 {
99- s = reader . ReadToEnd ( ) ;
100- _log . Debug ( "ID Received" ) ;
122+ if ( responseStream == null )
123+ {
124+ _log . Error ( "Received null response stream from ID endpoint." ) ;
125+ return fetchedId ;
126+ }
127+
128+ using ( var reader = new StreamReader ( responseStream ) )
129+ {
130+ fetchedId = reader . ReadToEnd ( ) . Trim ( ) ;
131+ _log . Info ( $ "ID successfully received from server: { fetchedId } ") ;
132+ }
101133 }
102134 }
103135 catch ( WebException wex )
104136 {
137+ _log . Warn ( wex , "WebException occurred while attempting to fetch ID." ) ;
138+
105139 if ( wex . Message . StartsWith ( "The remote name could not be resolved:" ) )
106140 {
107- _log . Debug ( $ "API not reachable: { wex . Message } ") ;
141+ _log . Warn ( $ "API not reachable: { wex . Message } ") ;
108142 }
109143 else if ( wex . Response is HttpWebResponse response &&
110144 response . StatusCode == HttpStatusCode . NotFound )
111145 {
112- _log . Debug ( $ "No ID returned! { wex . Message } ") ;
146+ _log . Warn ( $ "ID not found (404): { wex . Message } ") ;
147+ }
148+ else
149+ {
150+ _log . Error ( wex , $ "WebException encountered: { wex . Message } ") ;
113151 }
114152 }
115153 catch ( Exception e )
116154 {
117- _log . Error ( $ "General communication exception: { e . Message } ") ;
155+ _log . Error ( e , $ "Unexpected exception during ID retrieval : { e . Message } ") ;
118156 }
119157 }
120158 }
121159 catch ( Exception e )
122160 {
123- _log . Error ( $ "Cannot connect to API: { e . Message } ") ;
161+ _log . Error ( e , $ "Cannot connect to API: { e . Message } ") ;
124162 return string . Empty ;
125163 }
126164
127- s = s . Replace ( "\" " , "" ) ;
165+ // Remove potential surrounding quotes
166+ fetchedId = fetchedId . Replace ( "\" " , "" ) ;
128167
129168 if ( ! Directory . Exists ( ApplicationDetails . InstanceFiles . Path ) )
130169 {
131- Directory . CreateDirectory ( ApplicationDetails . InstanceFiles . Path ) ;
170+ try
171+ {
172+ Directory . CreateDirectory ( ApplicationDetails . InstanceFiles . Path ) ;
173+ _log . Info ( $ "Created directory for ID file: { ApplicationDetails . InstanceFiles . Path } ") ;
174+ }
175+ catch ( Exception ex )
176+ {
177+ _log . Error ( ex , $ "Failed to create directory: { ApplicationDetails . InstanceFiles . Path } ") ;
178+ return string . Empty ;
179+ }
180+ }
181+
182+ if ( string . IsNullOrEmpty ( fetchedId ) )
183+ {
184+ _log . Warn ( "Fetched ID is empty after processing." ) ;
185+ return string . Empty ;
132186 }
133187
134- if ( string . IsNullOrEmpty ( s ) )
188+ try
189+ {
190+ // Save returned ID
191+ File . WriteAllText ( IdFile , fetchedId ) ;
192+ _log . Info ( $ "ID successfully written to file: { IdFile } ") ;
193+ }
194+ catch ( Exception ex )
135195 {
196+ _log . Error ( ex , $ "Failed to write ID to file: { IdFile } ") ;
136197 return string . Empty ;
137198 }
138199
139- // Save returned ID
140- File . WriteAllText ( IdFile , s ) ;
141- return s ;
200+ return fetchedId ;
142201 }
143202
203+ /// <summary>
204+ /// Writes the provided ID to the ID file, ensuring proper formatting and directory existence.
205+ /// </summary>
206+ /// <param name="id">The ID to write.</param>
144207 public static void WriteId ( string id )
145208 {
146- if ( ! string . IsNullOrEmpty ( id ) )
209+ _log . Trace ( "WriteId method invoked." ) ;
210+
211+ if ( string . IsNullOrEmpty ( id ) )
212+ {
213+ _log . Warn ( "Attempted to write an empty or null ID." ) ;
214+ return ;
215+ }
216+
217+ try
147218 {
148- id = id . Replace ( "\" " , "" ) ;
219+ _log . Debug ( $ "Received ID for writing: { id } ") ;
220+ id = id . Replace ( "\" " , "" ) . Trim ( ) ;
149221
150222 if ( ! Directory . Exists ( ApplicationDetails . InstanceFiles . Path ) )
151223 {
152224 Directory . CreateDirectory ( ApplicationDetails . InstanceFiles . Path ) ;
225+ _log . Info ( $ "Created directory for ID file: { ApplicationDetails . InstanceFiles . Path } ") ;
153226 }
154227
155228 // Save returned ID
156229 File . WriteAllText ( ApplicationDetails . InstanceFiles . Id , id ) ;
230+ _log . Info ( $ "ID successfully written to file: { ApplicationDetails . InstanceFiles . Id } ") ;
231+ }
232+ catch ( Exception ex )
233+ {
234+ _log . Error ( ex , $ "Failed to write ID to file: { ApplicationDetails . InstanceFiles . Id } ") ;
157235 }
158236 }
159237 }
0 commit comments