22
33using System ;
44using System . Collections . Generic ;
5- using System . Linq ;
65using System . Net ;
76using System . Threading . Tasks ;
87using Microsoft . Azure . Documents ;
@@ -19,6 +18,7 @@ public class Engine : IEngine, IDisposable
1918 private readonly ILogger log ;
2019 private readonly IInstance instance ;
2120 private readonly IFactory factory ;
21+ private readonly IDiagnosticsLogger diagnosticsLogger ;
2222
2323 private Config storageConfig ;
2424
@@ -34,10 +34,12 @@ public class Engine : IEngine, IDisposable
3434 public Engine (
3535 IFactory factory ,
3636 ILogger logger ,
37+ IDiagnosticsLogger diagnosticsLogger ,
3738 IInstance instance )
3839 {
3940 this . log = logger ;
4041 this . instance = instance ;
42+ this . diagnosticsLogger = diagnosticsLogger ;
4143 this . factory = factory ;
4244
4345 this . disposedValue = false ;
@@ -118,7 +120,11 @@ public async Task<IEnumerable<IDataRecord>> GetAllAsync()
118120 }
119121 catch ( Exception e )
120122 {
121- this . log . Error ( "Unexpected error while reading from Cosmos DB SQL" , ( ) => new { this . storageName , e } ) ;
123+ const string MSG = "Unexpected error while reading from Cosmos DB SQL" ;
124+ var errorData = new { this . storageName , e } ;
125+
126+ this . log . Error ( MSG , ( ) => errorData ) ;
127+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
122128 throw new ExternalDependencyException ( e ) ;
123129 }
124130 }
@@ -146,8 +152,11 @@ public async Task<IDataRecord> CreateAsync(IDataRecord input)
146152 }
147153 catch ( Exception e )
148154 {
149- this . log . Error ( "Unexpected error while writing to Cosmos DB SQL" ,
150- ( ) => new { this . storageName , Id = input . GetId ( ) , e } ) ;
155+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
156+ var errorData = new { this . storageName , Id = input . GetId ( ) , e } ;
157+
158+ this . log . Error ( MSG , ( ) => errorData ) ;
159+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
151160 throw new ExternalDependencyException ( e ) ;
152161 }
153162 }
@@ -182,8 +191,11 @@ public async Task<IDataRecord> UpsertAsync(IDataRecord input, string eTag)
182191 }
183192 catch ( Exception e )
184193 {
185- this . log . Error ( "Unexpected error while writing to Cosmos DB SQL" ,
186- ( ) => new { this . storageName , Id = input . GetId ( ) , eTag , e } ) ;
194+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
195+ var errorData = new { this . storageName , Id = input . GetId ( ) , eTag , e } ;
196+
197+ this . log . Error ( MSG , ( ) => errorData ) ;
198+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
187199 throw new ExternalDependencyException ( e ) ;
188200 }
189201 }
@@ -207,8 +219,11 @@ public async Task DeleteAsync(string id)
207219 }
208220 catch ( Exception e )
209221 {
210- this . log . Error ( "Unexpected error while writing to Cosmos DB SQL" ,
211- ( ) => new { this . storageName , id , e } ) ;
222+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
223+ var errorData = new { this . storageName , id , e } ;
224+
225+ this . log . Error ( MSG , ( ) => errorData ) ;
226+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
212227 throw new ExternalDependencyException ( e ) ;
213228 }
214229 }
@@ -274,8 +289,11 @@ public async Task<bool> TryToLockAsync(
274289 }
275290 catch ( Exception e )
276291 {
277- this . log . Error ( "Unexpected error while writing to Cosmos DB SQL" ,
278- ( ) => new { this . storageName , id , ownerId , ownerType , lockDurationSecs = durationSeconds , e } ) ;
292+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
293+ var errorData = new { this . storageName , id , ownerId , ownerType , lockDurationSecs = durationSeconds , e } ;
294+
295+ this . log . Error ( MSG , ( ) => errorData ) ;
296+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
279297 }
280298
281299 return false ;
@@ -321,8 +339,11 @@ public async Task<bool> TryToUnlockAsync(string id, string ownerId, string owner
321339 }
322340 catch ( Exception e )
323341 {
324- this . log . Error ( "Unexpected error while writing to Cosmos DB SQL" ,
325- ( ) => new { this . storageName , id , ownerId , ownerType , e } ) ;
342+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
343+ var errorData = new { this . storageName , id , ownerId , ownerType , e } ;
344+
345+ this . log . Error ( MSG , ( ) => errorData ) ;
346+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
326347 }
327348
328349 return false ;
@@ -397,8 +418,11 @@ private async Task SetupStorageAsync()
397418 }
398419 catch ( Exception e )
399420 {
400- this . log . Error ( "Unexpected error while reading from Cosmos DB SQL" ,
401- ( ) => new { this . storageName , id , e } ) ;
421+ const string MSG = "Unexpected error while reading from Cosmos DB SQL" ;
422+ var errorData = new { this . storageName , id , e } ;
423+
424+ this . log . Error ( MSG , ( ) => errorData ) ;
425+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
402426 throw new ExternalDependencyException ( e ) ;
403427 }
404428 }
@@ -436,7 +460,11 @@ private async Task TryToDeleteExpiredRecord(string id)
436460 catch ( Exception e )
437461 {
438462 // Log and do not throw, we're just trying to delete and will retry automatically later
439- this . log . Warn ( "Unexpected error while writing to Cosmos DB SQL" , ( ) => new { this . storageName , id , e } ) ;
463+ const string MSG = "Unexpected error while writing to Cosmos DB SQL" ;
464+ var errorData = new { this . storageName , id , e } ;
465+
466+ this . log . Error ( MSG , ( ) => errorData ) ;
467+ this . diagnosticsLogger . LogServiceError ( MSG , errorData ) ;
440468 }
441469 }
442470 }
0 commit comments