@@ -30,6 +30,8 @@ public class BrowserStackTunnel : IDisposable
30
30
31
31
protected StringBuilder output ;
32
32
public LocalState localState ;
33
+ protected string logFilePath = "" ;
34
+ protected FileSystemWatcher logfileWatcher ;
33
35
34
36
Job job = null ;
35
37
Process process = null ;
@@ -120,6 +122,10 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
120
122
process . Close ( ) ;
121
123
}
122
124
125
+ if ( File . Exists ( logFilePath ) )
126
+ {
127
+ File . WriteAllText ( logFilePath , string . Empty ) ;
128
+ }
123
129
Local . logger . Info ( "BrowserStackLocal binary is located at " + binaryAbsolute ) ;
124
130
Local . logger . Info ( "Starting Binary with arguments " + arguments . Replace ( accessKey , "<access_key>" ) ) ;
125
131
ProcessStartInfo processStartInfo = new ProcessStartInfo ( )
@@ -190,8 +196,30 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
190
196
191
197
private void readFile ( string filename )
192
198
{
193
- using ( Stream fileStream = File . Open ( filename , FileMode . Create ) )
194
- fileStream . Write ( new Byte [ 1 ] , 0 , 1 ) ;
199
+ logFilePath = filename ;
200
+ if ( File . Exists ( filename ) )
201
+ {
202
+ readAlreadyPresentFile ( filename ) ;
203
+ }
204
+ else
205
+ {
206
+ logfileWatcher = new FileSystemWatcher ( new FileInfo ( filename ) . Directory . FullName ) ;
207
+ logfileWatcher . Created += readAlreadyPresentFile ;
208
+ logfileWatcher . Changed += readAlreadyPresentFile ;
209
+ logfileWatcher . EnableRaisingEvents = true ;
210
+ }
211
+ }
212
+
213
+ private void readAlreadyPresentFile ( object sender , FileSystemEventArgs e )
214
+ {
215
+ if ( e . FullPath . Equals ( logFilePath ) )
216
+ {
217
+ readAlreadyPresentFile ( e . FullPath ) ;
218
+ }
219
+ }
220
+
221
+ private void readAlreadyPresentFile ( string filename )
222
+ {
195
223
using ( FileStream fs = File . Open ( filename , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
196
224
{
197
225
byte [ ] bytes = new byte [ 1024 ] ;
@@ -224,7 +252,6 @@ private void TunnelStateChanged(LocalState prevState, LocalState state)
224
252
{
225
253
connectingEvent . Set ( ) ;
226
254
}
227
- Local . logger . Info ( "Current tunnel state " + state ) ;
228
255
}
229
256
230
257
public bool IsConnected ( )
@@ -236,10 +263,12 @@ public virtual void Kill()
236
263
{
237
264
try
238
265
{
239
- this . process . Kill ( ) ;
240
- this . process = null ;
241
- this . job . Close ( ) ;
242
- this . job = null ;
266
+ if ( process != null )
267
+ process . Kill ( ) ;
268
+ process = null ;
269
+ if ( job != null )
270
+ job . Close ( ) ;
271
+ job = null ;
243
272
}
244
273
catch ( Exception e )
245
274
{
@@ -251,7 +280,6 @@ public virtual void Kill()
251
280
TunnelStateChanged ( localState , LocalState . Disconnected ) ;
252
281
253
282
localState = LocalState . Disconnected ;
254
- Local . logger . Info ( "TunnelState: " + localState . ToString ( ) ) ;
255
283
}
256
284
}
257
285
0 commit comments