Skip to content

Commit 4f25bb6

Browse files
committed
Consolidated the key value naming restriction
The original disallowed characters from LogEventEntity were correct, but not in AzureTableStorageEntityFactory, causing the errors in #10.
1 parent 50651b3 commit 4f25bb6

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace Serilog.Sinks.AzureTableStorage
4+
{
5+
/// <summary>
6+
/// Defines naming restrictions for Azure Table Storage objects
7+
/// </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])");
16+
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+
}
27+
}
28+
}

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableStorage/LogEventEntity.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ namespace Serilog.Sinks.AzureTableStorage
3232
/// </remarks>
3333
public class LogEventEntity : TableEntity
3434
{
35-
static readonly Regex RowKeyNotAllowedMatch = new Regex(@"(\\|/|#|\?|[\x00-\x1f]|[\x7f-\x9f])");
36-
3735
/// <summary>
3836
/// Default constructor for the Storage Client library to re-hydrate entities when querying.
3937
/// </summary>
@@ -62,7 +60,7 @@ public LogEventEntity(LogEvent log, IFormatProvider formatProvider, long partiti
6260
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179338.aspx
6361
static string GetValidRowKey(string rowKey)
6462
{
65-
rowKey = RowKeyNotAllowedMatch.Replace(rowKey, "");
63+
rowKey = ObjectNaming.KeyFieldValueCharactersNotAllowedMatch.Replace(rowKey, "");
6664
return rowKey.Length > 1024 ? rowKey.Substring(0, 1024) : rowKey;
6765
}
6866

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableStorageWithProperties/AzureTableStorageEntityFactory.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ namespace Serilog.Sinks.AzureTableStorage
2727
/// </summary>
2828
public static class AzureTableStorageEntityFactory
2929
{
30-
// Valid RowKey name characters
31-
static readonly Regex _rowKeyNotAllowedMatch = new Regex(@"(\\|/|#|\n|\r|\?)");
32-
3330
// Azure tables support a maximum of 255 properties. PartitionKey, RowKey and Timestamp
3431
// bring the maximum to 252.
3532
const int _maxNumberOfPropertiesPerRow = 252;
@@ -99,7 +96,7 @@ public static DynamicTableEntity CreateEntityWithProperties(LogEvent logEvent, I
9996
/// </returns>
10097
public static string GetValidStringForTableKey(string s)
10198
{
102-
return _rowKeyNotAllowedMatch.Replace(s, "");
99+
return ObjectNaming.KeyFieldValueCharactersNotAllowedMatch.Replace(s, "");
103100
}
104101

105102
// Generate a valid partition key from event timestamp.
@@ -133,7 +130,7 @@ private static string GenerateValidRowKey(LogEvent logEvent, string additionalRo
133130
postfixBuilder.Append('|').Append(additionalRowKeyPostfix);
134131
}
135132

136-
// Append GUID to postfix
133+
// Append GUID to postfix
137134
postfixBuilder.Append('|').Append(Guid.NewGuid());
138135

139136
// Truncate prefix if too long

0 commit comments

Comments
 (0)