7
7
using System . Linq ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
- using Microsoft . Azure . Cosmos . Table ;
10
+ using Azure . Data . Tables ;
11
11
using Microsoft . Azure . WebJobs . Host . Executors ;
12
12
using Microsoft . Azure . WebJobs . Hosting ;
13
13
using Microsoft . Azure . WebJobs . Logging ;
@@ -33,8 +33,8 @@ public class DiagnosticEventTableStorageRepository : IDiagnosticEventRepository,
33
33
private readonly object _syncLock = new object ( ) ;
34
34
35
35
private ConcurrentDictionary < string , DiagnosticEvent > _events = new ConcurrentDictionary < string , DiagnosticEvent > ( ) ;
36
- private CloudTableClient _tableClient ;
37
- private CloudTable _diagnosticEventsTable ;
36
+ private TableServiceClient _tableClient ;
37
+ private TableClient _diagnosticEventsTable ;
38
38
private string _hostId ;
39
39
private bool _disposed = false ;
40
40
private bool _purged = false ;
@@ -55,22 +55,21 @@ public DiagnosticEventTableStorageRepository(IConfiguration configuration, IHost
55
55
ILogger < DiagnosticEventTableStorageRepository > logger )
56
56
: this ( configuration , hostIdProvider , environment , scriptHost , logger , LogFlushInterval ) { }
57
57
58
- internal CloudTableClient TableClient
58
+ internal TableServiceClient TableClient
59
59
{
60
60
get
61
61
{
62
62
if ( ! _environment . IsPlaceholderModeEnabled ( ) && _tableClient == null )
63
63
{
64
64
string storageConnectionString = _configuration . GetWebJobsConnectionString ( ConnectionStringNames . Storage ) ;
65
- if ( ! string . IsNullOrEmpty ( storageConnectionString )
66
- && CloudStorageAccount . TryParse ( storageConnectionString , out CloudStorageAccount account ) )
65
+
66
+ try
67
67
{
68
- var tableClientConfig = new TableClientConfiguration ( ) ;
69
- _tableClient = new CloudTableClient ( account . TableStorageUri , account . Credentials , tableClientConfig ) ;
68
+ _tableClient = new TableServiceClient ( storageConnectionString ) ;
70
69
}
71
- else
70
+ catch ( Exception ex )
72
71
{
73
- _logger . LogError ( "Azure Storage connection string is empty or invalid. Unable to write diagnostic events." ) ;
72
+ _logger . LogError ( ex , "Azure Storage connection string is empty or invalid. Unable to write diagnostic events." ) ;
74
73
}
75
74
}
76
75
@@ -92,7 +91,7 @@ internal string HostId
92
91
93
92
internal ConcurrentDictionary < string , DiagnosticEvent > Events => _events ;
94
93
95
- internal CloudTable GetDiagnosticEventsTable ( DateTime ? now = null )
94
+ internal TableClient GetDiagnosticEventsTable ( DateTime ? now = null )
96
95
{
97
96
if ( TableClient != null )
98
97
{
@@ -103,7 +102,7 @@ internal CloudTable GetDiagnosticEventsTable(DateTime? now = null)
103
102
if ( _diagnosticEventsTable == null || currentTableName != _tableName )
104
103
{
105
104
_tableName = currentTableName ;
106
- _diagnosticEventsTable = TableClient . GetTableReference ( _tableName ) ;
105
+ _diagnosticEventsTable = TableClient . GetTableClient ( _tableName ) ;
107
106
}
108
107
}
109
108
@@ -134,31 +133,27 @@ await Utility.InvokeWithRetriesAsync(async () =>
134
133
135
134
foreach ( var table in tables )
136
135
{
137
- var tableRecords = await table . ExecuteQuerySegmentedAsync ( new TableQuery < DiagnosticEvent > ( ) , null ) ;
138
-
139
- // Skip tables that have 0 records
140
- if ( tableRecords . Results . Count == 0 )
141
- {
142
- continue ;
143
- }
144
-
145
- // Delete table if it doesn't have records with EventVersion
146
- var eventVersionDoesNotExists = tableRecords . Results . Any ( record => string . IsNullOrEmpty ( record . EventVersion ) == true ) ;
147
- if ( eventVersionDoesNotExists )
148
- {
149
- _logger . LogDebug ( "Deleting table '{tableName}' as it contains records without an EventVersion." , table . Name ) ;
150
- await table . DeleteIfExistsAsync ( ) ;
151
- tableDeleted = true ;
152
- continue ;
153
- }
136
+ var tableQuery = table . QueryAsync < DiagnosticEvent > ( cancellationToken : default ) ;
154
137
155
- // If the table does have EventVersion, query if it is an outdated version
156
- var eventVersionOutdated = tableRecords . Results . Any ( record => string . Compare ( DiagnosticEvent . CurrentEventVersion , record . EventVersion , StringComparison . Ordinal ) > 0 ) ;
157
- if ( eventVersionOutdated )
138
+ await foreach ( var record in tableQuery )
158
139
{
159
- _logger . LogDebug ( "Deleting table '{tableName}' as it contains records with an outdated EventVersion." , table . Name ) ;
160
- await table . DeleteIfExistsAsync ( ) ;
161
- tableDeleted = true ;
140
+ // Delete table if it doesn't have records with EventVersion
141
+ if ( string . IsNullOrEmpty ( record . EventVersion ) == true )
142
+ {
143
+ _logger . LogDebug ( "Deleting table '{tableName}' as it contains records without an EventVersion." , table . Name ) ;
144
+ await table . DeleteAsync ( ) ;
145
+ tableDeleted = true ;
146
+ break ;
147
+ }
148
+
149
+ // If the table does have EventVersion, query if it is an outdated version
150
+ if ( string . Compare ( DiagnosticEvent . CurrentEventVersion , record . EventVersion , StringComparison . Ordinal ) > 0 )
151
+ {
152
+ _logger . LogDebug ( "Deleting table '{tableName}' as it contains records with an outdated EventVersion." , table . Name ) ;
153
+ await table . DeleteAsync ( ) ;
154
+ tableDeleted = true ;
155
+ break ;
156
+ }
162
157
}
163
158
}
164
159
@@ -177,7 +172,7 @@ await Utility.InvokeWithRetriesAsync(async () =>
177
172
}
178
173
}
179
174
180
- internal virtual async Task FlushLogs ( CloudTable table = null )
175
+ internal virtual async Task FlushLogs ( TableClient table = null )
181
176
{
182
177
if ( _environment . IsPlaceholderModeEnabled ( ) )
183
178
{
@@ -200,7 +195,7 @@ internal virtual async Task FlushLogs(CloudTable table = null)
200
195
return ;
201
196
}
202
197
203
- bool tableCreated = await TableStorageHelpers . CreateIfNotExistsAsync ( table , TableCreationMaxRetryCount ) ;
198
+ bool tableCreated = await TableStorageHelpers . CreateIfNotExistsAsync ( table , TableClient , TableCreationMaxRetryCount ) ;
204
199
if ( tableCreated )
205
200
{
206
201
_logger . LogDebug ( "Queueing background table purge." ) ;
@@ -225,20 +220,20 @@ internal virtual async Task FlushLogs(CloudTable table = null)
225
220
}
226
221
}
227
222
228
- internal async Task ExecuteBatchAsync ( ConcurrentDictionary < string , DiagnosticEvent > events , CloudTable table )
223
+ internal async Task ExecuteBatchAsync ( ConcurrentDictionary < string , DiagnosticEvent > events , TableClient table )
229
224
{
230
225
try
231
226
{
232
- var batch = new TableBatchOperation ( ) ;
227
+ var batch = new List < TableTransactionAction > ( ) ;
233
228
foreach ( string errorCode in events . Keys )
234
229
{
235
230
var diagnosticEvent = events [ errorCode ] ;
236
231
diagnosticEvent . Message = Sanitizer . Sanitize ( diagnosticEvent . Message ) ;
237
232
diagnosticEvent . Details = Sanitizer . Sanitize ( diagnosticEvent . Details ) ;
238
- TableOperation insertOperation = TableOperation . Insert ( diagnosticEvent ) ;
239
- batch . Add ( insertOperation ) ;
233
+ TableTransactionAction insertAction = new TableTransactionAction ( TableTransactionActionType . Add , diagnosticEvent ) ;
234
+ batch . Add ( insertAction ) ;
240
235
}
241
- await table . ExecuteBatchAsync ( batch ) ;
236
+ await table . SubmitTransactionAsync ( batch ) ;
242
237
events . Clear ( ) ;
243
238
}
244
239
catch ( Exception ex )
0 commit comments