|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +using System; |
| 16 | +using System.Collections.Generic; |
15 | 17 | using System.Threading.Tasks; |
| 18 | + |
| 19 | +using Azure.Data.Tables; |
| 20 | + |
16 | 21 | using Serilog.Events; |
| 22 | +using Serilog.Formatting; |
17 | 23 | using Serilog.Sinks.AzureTableStorage.AzureTableProvider; |
18 | 24 | using Serilog.Sinks.AzureTableStorage.KeyGenerator; |
19 | 25 | using Serilog.Sinks.PeriodicBatching; |
20 | | -using System; |
21 | | -using System.Collections.Generic; |
22 | | -using Azure.Data.Tables; |
23 | | -using Serilog.Formatting; |
24 | 26 |
|
25 | | -namespace Serilog.Sinks.AzureTableStorage |
| 27 | +namespace Serilog.Sinks.AzureTableStorage; |
| 28 | + |
| 29 | +/// <summary> |
| 30 | +/// Writes log events as records to an Azure Table Storage table. |
| 31 | +/// </summary> |
| 32 | +public class AzureBatchingTableStorageSink : PeriodicBatchingSink |
26 | 33 | { |
| 34 | + readonly ITextFormatter _textFormatter; |
| 35 | + readonly IKeyGenerator _keyGenerator; |
| 36 | + readonly TableServiceClient _storageAccount; |
| 37 | + readonly string _storageTableName; |
| 38 | + readonly bool _bypassTableCreationValidation; |
| 39 | + readonly ICloudTableProvider _cloudTableProvider; |
| 40 | + |
27 | 41 | /// <summary> |
28 | | - /// Writes log events as records to an Azure Table Storage table. |
| 42 | + /// Construct a sink that saves logs to the specified storage account. |
29 | 43 | /// </summary> |
30 | | - public class AzureBatchingTableStorageSink : PeriodicBatchingSink |
| 44 | + /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param> |
| 45 | + /// <param name="textFormatter"></param> |
| 46 | + /// <param name="batchSizeLimit"></param> |
| 47 | + /// <param name="period"></param> |
| 48 | + /// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param> |
| 49 | + /// <param name="cloudTableProvider">Cloud table provider to get current log table.</param> |
| 50 | + public AzureBatchingTableStorageSink( |
| 51 | + TableServiceClient storageAccount, |
| 52 | + ITextFormatter textFormatter, |
| 53 | + int batchSizeLimit, |
| 54 | + TimeSpan period, |
| 55 | + string storageTableName = null, |
| 56 | + ICloudTableProvider cloudTableProvider = null) |
| 57 | + : this(storageAccount, textFormatter, batchSizeLimit, period, storageTableName, new DefaultKeyGenerator(), cloudTableProvider: cloudTableProvider) |
31 | 58 | { |
32 | | - readonly ITextFormatter _textFormatter; |
33 | | - readonly IKeyGenerator _keyGenerator; |
34 | | - readonly TableServiceClient _storageAccount; |
35 | | - readonly string _storageTableName; |
36 | | - readonly bool _bypassTableCreationValidation; |
37 | | - readonly ICloudTableProvider _cloudTableProvider; |
38 | | - |
39 | | - /// <summary> |
40 | | - /// Construct a sink that saves logs to the specified storage account. |
41 | | - /// </summary> |
42 | | - /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param> |
43 | | - /// <param name="textFormatter"></param> |
44 | | - /// <param name="batchSizeLimit"></param> |
45 | | - /// <param name="period"></param> |
46 | | - /// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param> |
47 | | - /// <param name="cloudTableProvider">Cloud table provider to get current log table.</param> |
48 | | - public AzureBatchingTableStorageSink( |
49 | | - TableServiceClient storageAccount, |
50 | | - ITextFormatter textFormatter, |
51 | | - int batchSizeLimit, |
52 | | - TimeSpan period, |
53 | | - string storageTableName = null, |
54 | | - ICloudTableProvider cloudTableProvider = null) |
55 | | - : this(storageAccount, textFormatter, batchSizeLimit, period, storageTableName, new DefaultKeyGenerator(), cloudTableProvider: cloudTableProvider) |
56 | | - { |
57 | | - } |
| 59 | + } |
58 | 60 |
|
59 | | - /// <summary> |
60 | | - /// Construct a sink that saves logs to the specified storage account. |
61 | | - /// </summary> |
62 | | - /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param> |
63 | | - /// <param name="textFormatter"></param> |
64 | | - /// <param name="batchSizeLimit"></param> |
65 | | - /// <param name="period"></param> |
66 | | - /// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param> |
67 | | - /// <param name="keyGenerator">generator used for partition keys and row keys</param> |
68 | | - /// <param name="bypassTableCreationValidation">Bypass the exception in case the table creation fails.</param> |
69 | | - /// <param name="cloudTableProvider">Cloud table provider to get current log table.</param> |
70 | | - public AzureBatchingTableStorageSink( |
71 | | - TableServiceClient storageAccount, |
72 | | - ITextFormatter textFormatter, |
73 | | - int batchSizeLimit, |
74 | | - TimeSpan period, |
75 | | - string storageTableName = null, |
76 | | - IKeyGenerator keyGenerator = null, |
77 | | - bool bypassTableCreationValidation = false, |
78 | | - ICloudTableProvider cloudTableProvider = null) |
| 61 | + /// <summary> |
| 62 | + /// Construct a sink that saves logs to the specified storage account. |
| 63 | + /// </summary> |
| 64 | + /// <param name="storageAccount">The Cloud Storage Account to use to insert the log entries to.</param> |
| 65 | + /// <param name="textFormatter"></param> |
| 66 | + /// <param name="batchSizeLimit"></param> |
| 67 | + /// <param name="period"></param> |
| 68 | + /// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param> |
| 69 | + /// <param name="keyGenerator">generator used for partition keys and row keys</param> |
| 70 | + /// <param name="bypassTableCreationValidation">Bypass the exception in case the table creation fails.</param> |
| 71 | + /// <param name="cloudTableProvider">Cloud table provider to get current log table.</param> |
| 72 | + public AzureBatchingTableStorageSink( |
| 73 | + TableServiceClient storageAccount, |
| 74 | + ITextFormatter textFormatter, |
| 75 | + int batchSizeLimit, |
| 76 | + TimeSpan period, |
| 77 | + string storageTableName = null, |
| 78 | + IKeyGenerator keyGenerator = null, |
| 79 | + bool bypassTableCreationValidation = false, |
| 80 | + ICloudTableProvider cloudTableProvider = null) |
79 | 81 | #pragma warning disable CS0618 // Type or member is obsolete |
80 | | - : base(batchSizeLimit, period) |
| 82 | + : base(batchSizeLimit, period) |
81 | 83 | #pragma warning restore CS0618 // Type or member is obsolete |
| 84 | + { |
| 85 | + if (batchSizeLimit < 1 || batchSizeLimit > 100) |
| 86 | + throw new ArgumentException("batchSizeLimit must be between 1 and 100 for Azure Table Storage"); |
| 87 | + |
| 88 | + _textFormatter = textFormatter; |
| 89 | + _keyGenerator = keyGenerator ?? new DefaultKeyGenerator(); |
| 90 | + |
| 91 | + if (string.IsNullOrEmpty(storageTableName)) |
82 | 92 | { |
83 | | - if (batchSizeLimit < 1 || batchSizeLimit > 100) |
84 | | - throw new ArgumentException("batchSizeLimit must be between 1 and 100 for Azure Table Storage"); |
| 93 | + storageTableName = typeof(LogEventEntity).Name; |
| 94 | + } |
85 | 95 |
|
86 | | - _textFormatter = textFormatter; |
87 | | - _keyGenerator = keyGenerator ?? new DefaultKeyGenerator(); |
| 96 | + _storageAccount = storageAccount; |
| 97 | + _storageTableName = storageTableName; |
| 98 | + _bypassTableCreationValidation = bypassTableCreationValidation; |
| 99 | + _cloudTableProvider = cloudTableProvider ?? new DefaultCloudTableProvider(); |
| 100 | + } |
88 | 101 |
|
89 | | - if (string.IsNullOrEmpty(storageTableName)) |
90 | | - { |
91 | | - storageTableName = typeof(LogEventEntity).Name; |
92 | | - } |
| 102 | + /// <summary> |
| 103 | + /// Emit a batch of log events, running asynchronously. |
| 104 | + /// </summary> |
| 105 | + /// <param name="events">The events to emit.</param> |
| 106 | + /// <remarks> |
| 107 | + /// Override either <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" /> or <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />, |
| 108 | + /// not both. |
| 109 | + /// </remarks> |
| 110 | + protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events) |
| 111 | + { |
| 112 | + var table = _cloudTableProvider.GetCloudTable(_storageAccount, _storageTableName, _bypassTableCreationValidation); |
| 113 | + var transactionActions = new List<TableTransactionAction>(); |
93 | 114 |
|
94 | | - _storageAccount = storageAccount; |
95 | | - _storageTableName = storageTableName; |
96 | | - _bypassTableCreationValidation = bypassTableCreationValidation; |
97 | | - _cloudTableProvider = cloudTableProvider ?? new DefaultCloudTableProvider(); |
98 | | - } |
| 115 | + string lastPartitionKey = null; |
99 | 116 |
|
100 | | - /// <summary> |
101 | | - /// Emit a batch of log events, running asynchronously. |
102 | | - /// </summary> |
103 | | - /// <param name="events">The events to emit.</param> |
104 | | - /// <remarks> |
105 | | - /// Override either <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatchAsync(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" /> or <see cref="M:Serilog.Sinks.PeriodicBatching.PeriodicBatchingSink.EmitBatch(System.Collections.Generic.IEnumerable{Serilog.Events.LogEvent})" />, |
106 | | - /// not both. |
107 | | - /// </remarks> |
108 | | - protected override async Task EmitBatchAsync(IEnumerable<LogEvent> events) |
| 117 | + foreach (var logEvent in events) |
109 | 118 | { |
110 | | - var table = _cloudTableProvider.GetCloudTable(_storageAccount, _storageTableName, _bypassTableCreationValidation); |
111 | | - var transactionActions = new List<TableTransactionAction>(); |
| 119 | + var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent); |
112 | 120 |
|
113 | | - string lastPartitionKey = null; |
114 | | - |
115 | | - foreach (var logEvent in events) |
| 121 | + if (partitionKey != lastPartitionKey) |
116 | 122 | { |
117 | | - var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent); |
118 | | - |
119 | | - if (partitionKey != lastPartitionKey) |
| 123 | + lastPartitionKey = partitionKey; |
| 124 | + if (transactionActions.Count > 0) |
120 | 125 | { |
121 | | - lastPartitionKey = partitionKey; |
122 | | - if (transactionActions.Count > 0) |
123 | | - { |
124 | | - await table.SubmitTransactionAsync(transactionActions).ConfigureAwait(false); |
125 | | - transactionActions = new List<TableTransactionAction>(); |
126 | | - } |
| 126 | + await table.SubmitTransactionAsync(transactionActions).ConfigureAwait(false); |
| 127 | + transactionActions = new List<TableTransactionAction>(); |
127 | 128 | } |
128 | | - var logEventEntity = new LogEventEntity( |
129 | | - logEvent, |
130 | | - _textFormatter, |
131 | | - partitionKey, |
132 | | - _keyGenerator.GenerateRowKey(logEvent) |
133 | | - ); |
134 | | - var transactionAction = |
135 | | - new TableTransactionAction(TableTransactionActionType.UpdateMerge, logEventEntity); |
136 | | - transactionActions.Add(transactionAction); |
137 | | - } |
138 | | - if (transactionActions.Count > 0) |
139 | | - { |
140 | | - await table.SubmitTransactionAsync(transactionActions).ConfigureAwait(false); |
141 | 129 | } |
| 130 | + var logEventEntity = new LogEventEntity( |
| 131 | + logEvent, |
| 132 | + _textFormatter, |
| 133 | + partitionKey, |
| 134 | + _keyGenerator.GenerateRowKey(logEvent) |
| 135 | + ); |
| 136 | + var transactionAction = |
| 137 | + new TableTransactionAction(TableTransactionActionType.UpdateMerge, logEventEntity); |
| 138 | + transactionActions.Add(transactionAction); |
| 139 | + } |
| 140 | + if (transactionActions.Count > 0) |
| 141 | + { |
| 142 | + await table.SubmitTransactionAsync(transactionActions).ConfigureAwait(false); |
142 | 143 | } |
143 | 144 | } |
144 | 145 | } |
0 commit comments