77using System . Threading . Tasks ;
88using Microsoft . Azure . Cosmos . Table ;
99using Microsoft . Azure . WebJobs . Host . Executors ;
10+ using Microsoft . Azure . WebJobs . Logging ;
1011using Microsoft . Azure . WebJobs . Script . WebHost . Helpers ;
1112using Microsoft . Extensions . Configuration ;
1213using Microsoft . Extensions . Logging ;
@@ -16,21 +17,20 @@ namespace Microsoft.Azure.WebJobs.Script.WebHost.Diagnostics
1617 public class DiagnosticEventTableStorageRepository : IDiagnosticEventRepository , IDisposable
1718 {
1819 internal const string TableNamePrefix = "AzureFunctionsDiagnosticEvents" ;
19- private const int LogFlushInterval = 1000 * 60 * 10 ; // 10 mins
20+ private const int LogFlushInterval = 1000 * 60 * 10 ; // 10 minutes
21+ private const int TableCreationMaxRetryCount = 5 ;
22+
2023 private readonly Timer _flushLogsTimer ;
24+ private readonly IConfiguration _configuration ;
25+ private readonly IHostIdProvider _hostIdProvider ;
26+ private readonly IEnvironment _environment ;
27+ private readonly ILogger < DiagnosticEventTableStorageRepository > _logger ;
28+ private readonly object _syncLock = new object ( ) ;
2129
22- private int _tableCreationRetries = 5 ;
2330 private ConcurrentDictionary < string , DiagnosticEvent > _events = new ConcurrentDictionary < string , DiagnosticEvent > ( ) ;
24-
25- private IConfiguration _configuration ;
26- private IHostIdProvider _hostIdProvider ;
27- private IEnvironment _environment ;
28- private ILogger < DiagnosticEventTableStorageRepository > _logger ;
29-
3031 private CloudTableClient _tableClient ;
3132 private CloudTable _diagnosticEventsTable ;
3233 private string _hostId ;
33- private object _syncLock = new object ( ) ;
3434 private bool _disposed = false ;
3535 private string _tableName ;
3636
@@ -81,13 +81,7 @@ internal string HostId
8181 }
8282 }
8383
84- internal ConcurrentDictionary < string , DiagnosticEvent > Events
85- {
86- get
87- {
88- return _events ;
89- }
90- }
84+ internal ConcurrentDictionary < string , DiagnosticEvent > Events => _events ;
9185
9286 internal CloudTable GetDiagnosticEventsTable ( DateTime ? now = null )
9387 {
@@ -135,7 +129,7 @@ internal virtual async Task FlushLogs(CloudTable table = null)
135129 return ;
136130 }
137131
138- bool tableCreated = await TableStorageHelpers . CreateIfNotExistsAsync ( table , _tableCreationRetries ) ;
132+ bool tableCreated = await TableStorageHelpers . CreateIfNotExistsAsync ( table , TableCreationMaxRetryCount ) ;
139133 if ( tableCreated )
140134 {
141135 TableStorageHelpers . QueueBackgroundTablePurge ( table , TableClient , TableNamePrefix , _logger ) ;
@@ -153,7 +147,7 @@ internal virtual async Task FlushLogs(CloudTable table = null)
153147 // All existing events are logged to other logging pipelines already.
154148 ConcurrentDictionary < string , DiagnosticEvent > tempDictionary = _events ;
155149 _events = new ConcurrentDictionary < string , DiagnosticEvent > ( ) ;
156- if ( tempDictionary . Count > 0 )
150+ if ( ! tempDictionary . IsEmpty )
157151 {
158152 await ExecuteBatchAsync ( tempDictionary , table ) ;
159153 }
@@ -166,7 +160,10 @@ internal async Task ExecuteBatchAsync(ConcurrentDictionary<string, DiagnosticEve
166160 var batch = new TableBatchOperation ( ) ;
167161 foreach ( string errorCode in events . Keys )
168162 {
169- TableOperation insertOperation = TableOperation . Insert ( events [ errorCode ] ) ;
163+ var diagnosticEvent = events [ errorCode ] ;
164+ diagnosticEvent . Message = Sanitizer . Sanitize ( diagnosticEvent . Message ) ;
165+ diagnosticEvent . Details = Sanitizer . Sanitize ( diagnosticEvent . Details ) ;
166+ TableOperation insertOperation = TableOperation . Insert ( diagnosticEvent ) ;
170167 batch . Add ( insertOperation ) ;
171168 }
172169 await table . ExecuteBatchAsync ( batch ) ;
@@ -205,7 +202,7 @@ public void WriteDiagnosticEvent(DateTime timestamp, string errorCode, LogLevel
205202 }
206203 }
207204
208- internal void StopTimer ( )
205+ private void StopTimer ( )
209206 {
210207 _logger . LogInformation ( "Stopping the flush logs timer" ) ;
211208 _flushLogsTimer ? . Change ( Timeout . Infinite , Timeout . Infinite ) ;
0 commit comments