Skip to content

Commit 94b62ba

Browse files
committed
Merge branch 'ChrisSimmons-dev' into dev
2 parents 271d233 + 74e0813 commit 94b62ba

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
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
@@ -27,8 +27,6 @@ namespace Serilog.Sinks.AzureTableStorage
2727
/// </summary>
2828
public class LogEventEntity : TableEntity
2929
{
30-
static readonly Regex RowKeyNotAllowedMatch = new Regex(@"(\\|/|#|\?|[\x00-\x1f]|[\x7f-\x9f])");
31-
3230
/// <summary>
3331
/// Default constructor for the Storage Client library to re-hydrate entities when querying.
3432
/// </summary>
@@ -62,7 +60,7 @@ public LogEventEntity(
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

test/Serilog.Sinks.AzureTableStorageWithProperties.Tests/AzureTableStorageWithPropertiesSinkTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,75 @@ public async Task WhenALoggerWritesToTheSinkItIsRetrievableFromTheTableWithPrope
5353
Assert.Equal(" ", result.Properties["Space"].PropertyAsObject);
5454
}
5555

56+
[Fact]
57+
public void WhenALoggerWritesToTheSinkWithAWindowsNewlineInTheTemplateItIsRetrievable()
58+
{
59+
// Prompted from https://github.com/serilog/serilog-sinks-azuretablestorage/issues/10
60+
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
61+
var tableClient = storageAccount.CreateCloudTableClient();
62+
var table = tableClient.GetTableReference("LogEventEntity");
63+
64+
table.DeleteIfExists();
65+
66+
var logger = new LoggerConfiguration()
67+
.WriteTo.AzureTableStorageWithProperties(storageAccount)
68+
.CreateLogger();
69+
70+
const string messageTemplate = "Line 1\r\nLine2";
71+
72+
logger.Information(messageTemplate);
73+
74+
var result = table.ExecuteQuery(new TableQuery().Take(1)).FirstOrDefault();
75+
76+
Assert.NotNull(result);
77+
}
78+
79+
[Fact]
80+
public void WhenALoggerWritesToTheSinkWithALineFeedInTheTemplateItIsRetrievable()
81+
{
82+
// Prompted from https://github.com/serilog/serilog-sinks-azuretablestorage/issues/10
83+
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
84+
var tableClient = storageAccount.CreateCloudTableClient();
85+
var table = tableClient.GetTableReference("LogEventEntity");
86+
87+
table.DeleteIfExists();
88+
89+
var logger = new LoggerConfiguration()
90+
.WriteTo.AzureTableStorageWithProperties(storageAccount)
91+
.CreateLogger();
92+
93+
const string messageTemplate = "Line 1\nLine2";
94+
95+
logger.Information(messageTemplate);
96+
97+
var result = table.ExecuteQuery(new TableQuery().Take(1)).FirstOrDefault();
98+
99+
Assert.NotNull(result);
100+
}
101+
102+
[Fact]
103+
public void WhenALoggerWritesToTheSinkWithACarriageReturnInTheTemplateItIsRetrievable()
104+
{
105+
// Prompted from https://github.com/serilog/serilog-sinks-azuretablestorage/issues/10
106+
var storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
107+
var tableClient = storageAccount.CreateCloudTableClient();
108+
var table = tableClient.GetTableReference("LogEventEntity");
109+
110+
table.DeleteIfExists();
111+
112+
var logger = new LoggerConfiguration()
113+
.WriteTo.AzureTableStorageWithProperties(storageAccount)
114+
.CreateLogger();
115+
116+
const string messageTemplate = "Line 1\rLine2";
117+
118+
logger.Information(messageTemplate);
119+
120+
var result = table.ExecuteQuery(new TableQuery().Take(1)).FirstOrDefault();
121+
122+
Assert.NotNull(result);
123+
}
124+
56125
[Fact]
57126
public async Task WhenALoggerWritesToTheSinkItStoresTheCorrectTypesForScalar()
58127
{

0 commit comments

Comments
 (0)