Skip to content

Commit 1779784

Browse files
committed
format style
1 parent c939d1e commit 1779784

17 files changed

+1201
-1203
lines changed

src/Serilog.Sinks.AzureTableStorage/LoggerConfigurationAzureTableStorageExtensions.cs

Lines changed: 299 additions & 298 deletions
Large diffs are not rendered by default.

src/Serilog.Sinks.AzureTableStorage/LoggerConfigurationAzureTableStorageWithPropertiesExtensions.cs

Lines changed: 171 additions & 170 deletions
Large diffs are not rendered by default.
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
using System.Text.RegularExpressions;
22

3-
namespace Serilog.Sinks.AzureTableStorage
3+
namespace Serilog.Sinks.AzureTableStorage;
4+
5+
/// <summary>
6+
/// Defines naming restrictions for Azure Table Storage objects
7+
/// </summary>
8+
public static class ObjectNaming
49
{
510
/// <summary>
6-
/// Defines naming restrictions for Azure Table Storage objects
11+
/// The regex defining characters which are disallowed for key field values.
712
/// </summary>
8-
public static class ObjectNaming
9-
{
10-
/// <summary>
11-
/// The regex defining characters which are disallowed for key field values.
12-
/// </summary>
13-
/// <see href="https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx"/>
14-
public static readonly Regex KeyFieldValueCharactersNotAllowedMatch =
15-
new Regex(@"(\\|/|#|\?|[\x00-\x1f]|[\x7f-\x9f])");
13+
/// <see href="https://msdn.microsoft.com/en-us/library/azure/dd179338.aspx"/>
14+
public static readonly Regex KeyFieldValueCharactersNotAllowedMatch =
15+
new Regex(@"(\\|/|#|\?|[\x00-\x1f]|[\x7f-\x9f])");
1616

17-
/// <summary>
18-
/// Given a <param name="keyValue">key value</param>, returns a value
19-
/// which has been 'cleaned' of any disallowed characters and trimmed
20-
/// to the allowed length.
21-
/// </summary>
22-
public static string GetValidKeyValue(string keyValue)
23-
{
24-
keyValue = KeyFieldValueCharactersNotAllowedMatch.Replace(keyValue, "");
25-
return keyValue.Length > 1024 ? keyValue.Substring(0, 1024) : keyValue;
26-
}
17+
/// <summary>
18+
/// Given a <param name="keyValue">key value</param>, returns a value
19+
/// which has been 'cleaned' of any disallowed characters and trimmed
20+
/// to the allowed length.
21+
/// </summary>
22+
public static string GetValidKeyValue(string keyValue)
23+
{
24+
keyValue = KeyFieldValueCharactersNotAllowedMatch.Replace(keyValue, "");
25+
return keyValue.Length > 1024 ? keyValue.Substring(0, 1024) : keyValue;
2726
}
2827
}

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableProvider/DefaultCloudTableProvider.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,31 @@
1616

1717
using Azure.Data.Tables;
1818

19-
namespace Serilog.Sinks.AzureTableStorage.AzureTableProvider
19+
namespace Serilog.Sinks.AzureTableStorage.AzureTableProvider;
20+
21+
class DefaultCloudTableProvider : ICloudTableProvider
2022
{
21-
class DefaultCloudTableProvider : ICloudTableProvider
23+
TableClient _tableClient;
24+
25+
public TableClient GetCloudTable(TableServiceClient tableServiceClient, string storageTableName, bool bypassTableCreationValidation)
2226
{
23-
TableClient _tableClient;
27+
if (_tableClient != null) return _tableClient;
2428

25-
public TableClient GetCloudTable(TableServiceClient tableServiceClient, string storageTableName, bool bypassTableCreationValidation)
29+
// In some cases (e.g.: SAS URI), we might not have enough permissions to create the table if
30+
// it does not already exists. So, if we are in that case, we ignore the error as per bypassTableCreationValidation.
31+
try
2632
{
27-
if (_tableClient != null) return _tableClient;
28-
29-
// In some cases (e.g.: SAS URI), we might not have enough permissions to create the table if
30-
// it does not already exists. So, if we are in that case, we ignore the error as per bypassTableCreationValidation.
31-
try
32-
{
33-
tableServiceClient.CreateTableIfNotExistsWithout409(storageTableName);
34-
}
35-
catch (Exception ex)
33+
tableServiceClient.CreateTableIfNotExistsWithout409(storageTableName);
34+
}
35+
catch (Exception ex)
36+
{
37+
Debugging.SelfLog.WriteLine($"Failed to create table: {ex}");
38+
if (!bypassTableCreationValidation)
3639
{
37-
Debugging.SelfLog.WriteLine($"Failed to create table: {ex}");
38-
if (!bypassTableCreationValidation)
39-
{
40-
throw;
41-
}
40+
throw;
4241
}
43-
_tableClient = tableServiceClient.GetTableClient(storageTableName);
44-
return _tableClient;
4542
}
43+
_tableClient = tableServiceClient.GetTableClient(storageTableName);
44+
return _tableClient;
4645
}
4746
}

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableProvider/ICloudTableProvider.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,19 @@
1414

1515
using Azure.Data.Tables;
1616

17-
namespace Serilog.Sinks.AzureTableStorage.AzureTableProvider
17+
namespace Serilog.Sinks.AzureTableStorage.AzureTableProvider;
18+
19+
/// <summary>
20+
/// TableClient Provider
21+
/// </summary>
22+
public interface ICloudTableProvider
1823
{
1924
/// <summary>
20-
/// TableClient Provider
25+
/// Gets the cloud table.
2126
/// </summary>
22-
public interface ICloudTableProvider
23-
{
24-
/// <summary>
25-
/// Gets the cloud table.
26-
/// </summary>
27-
/// <param name="storageAccount">The storage account.</param>
28-
/// <param name="storageTableName">Name of the storage table.</param>
29-
/// <param name="bypassTableCreationValidation">if set to <c>true</c> [bypass table creation validation].</param>
30-
/// <returns></returns>
31-
TableClient GetCloudTable(TableServiceClient storageAccount, string storageTableName, bool bypassTableCreationValidation);
32-
}
27+
/// <param name="storageAccount">The storage account.</param>
28+
/// <param name="storageTableName">Name of the storage table.</param>
29+
/// <param name="bypassTableCreationValidation">if set to <c>true</c> [bypass table creation validation].</param>
30+
/// <returns></returns>
31+
TableClient GetCloudTable(TableServiceClient storageAccount, string storageTableName, bool bypassTableCreationValidation);
3332
}

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableStorage/AzureBatchingTableStorageSink.cs

Lines changed: 104 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -12,133 +12,134 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
using System;
16+
using System.Collections.Generic;
1517
using System.Threading.Tasks;
18+
19+
using Azure.Data.Tables;
20+
1621
using Serilog.Events;
22+
using Serilog.Formatting;
1723
using Serilog.Sinks.AzureTableStorage.AzureTableProvider;
1824
using Serilog.Sinks.AzureTableStorage.KeyGenerator;
1925
using Serilog.Sinks.PeriodicBatching;
20-
using System;
21-
using System.Collections.Generic;
22-
using Azure.Data.Tables;
23-
using Serilog.Formatting;
2426

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
2633
{
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+
2741
/// <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.
2943
/// </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)
3158
{
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+
}
5860

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)
7981
#pragma warning disable CS0618 // Type or member is obsolete
80-
: base(batchSizeLimit, period)
82+
: base(batchSizeLimit, period)
8183
#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))
8292
{
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+
}
8595

86-
_textFormatter = textFormatter;
87-
_keyGenerator = keyGenerator ?? new DefaultKeyGenerator();
96+
_storageAccount = storageAccount;
97+
_storageTableName = storageTableName;
98+
_bypassTableCreationValidation = bypassTableCreationValidation;
99+
_cloudTableProvider = cloudTableProvider ?? new DefaultCloudTableProvider();
100+
}
88101

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>();
93114

94-
_storageAccount = storageAccount;
95-
_storageTableName = storageTableName;
96-
_bypassTableCreationValidation = bypassTableCreationValidation;
97-
_cloudTableProvider = cloudTableProvider ?? new DefaultCloudTableProvider();
98-
}
115+
string lastPartitionKey = null;
99116

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)
109118
{
110-
var table = _cloudTableProvider.GetCloudTable(_storageAccount, _storageTableName, _bypassTableCreationValidation);
111-
var transactionActions = new List<TableTransactionAction>();
119+
var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent);
112120

113-
string lastPartitionKey = null;
114-
115-
foreach (var logEvent in events)
121+
if (partitionKey != lastPartitionKey)
116122
{
117-
var partitionKey = _keyGenerator.GeneratePartitionKey(logEvent);
118-
119-
if (partitionKey != lastPartitionKey)
123+
lastPartitionKey = partitionKey;
124+
if (transactionActions.Count > 0)
120125
{
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>();
127128
}
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);
141129
}
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);
142143
}
143144
}
144145
}

0 commit comments

Comments
 (0)