Skip to content

Commit 869fecc

Browse files
committed
add tests
1 parent cf12599 commit 869fecc

File tree

8 files changed

+929
-645
lines changed

8 files changed

+929
-645
lines changed

samples/SampleWebApplication/Pages/Logs.cshtml.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using SampleWebApplication.Models;
77

8+
using Serilog.Sinks.AzureTableStorage;
89
using Serilog.Sinks.AzureTableStorage.Extensions;
910

1011
namespace SampleWebApplication.Pages;
@@ -38,11 +39,7 @@ public async Task<IActionResult> OnGetAsync(CancellationToken cancellationToken)
3839
{
3940
var logTable = _tableServiceClient.GetTableClient("SampleLog");
4041

41-
var dateTime = Date.Date.ToUniversalTime();
42-
var upper = dateTime.GeneratePartitionKey();
43-
var lower = dateTime.AddDays(1).GeneratePartitionKey();
44-
45-
var filter = $"({nameof(ITableEntity.PartitionKey)} ge '{lower}') and ({nameof(ITableEntity.PartitionKey)} lt '{upper}')";
42+
var filter = DefaultKeyGenerator.GeneratePartitionKeyQuery(Date.Date);
4643

4744
if (!string.IsNullOrWhiteSpace(Level))
4845
filter += $" and ({nameof(LogEventModel.Level)} eq '{Level}')";

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableStorage/DefaultKeyGenerator.cs

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Threading;
33

4+
using Azure.Data.Tables;
5+
46
using Serilog.Events;
57
using Serilog.Sinks.AzureTableStorage.Extensions;
68

@@ -11,6 +13,7 @@ namespace Serilog.Sinks.AzureTableStorage;
1113
/// </summary>
1214
public class DefaultKeyGenerator : IKeyGenerator
1315
{
16+
private const string PartitionKeyName = nameof(ITableEntity.PartitionKey);
1417

1518
/// <summary>
1619
/// Automatically generates the PartitionKey based on the logEvent timestamp
@@ -52,40 +55,112 @@ public virtual string GenerateRowKey(LogEvent logEvent, AzureTableStorageSinkOpt
5255
}
5356

5457

58+
/// <summary>
59+
/// Generates the PartitionKey based on the specified <paramref name="eventTime"/> timestamp
60+
/// </summary>
61+
/// <param name="eventTime">The event time.</param>
62+
/// <param name="roundSpan">The round span.</param>
63+
/// <returns>
64+
/// The Generated PartitionKey
65+
/// </returns>
66+
/// <remarks>
67+
/// The partition key based on the Timestamp rounded to the nearest 5 min
68+
/// </remarks>
69+
public static string GeneratePartitionKey(DateTimeOffset eventTime, TimeSpan? roundSpan = null)
70+
{
71+
return GeneratePartitionKey(eventTime.UtcDateTime, roundSpan);
72+
}
5573

5674
/// <summary>
57-
/// Generates the PartitionKey based on the logEvent timestamp
75+
/// Generates the PartitionKey based on the specified <paramref name="eventTime"/> timestamp
5876
/// </summary>
59-
/// <param name="utcEventTime">The UTC event time.</param>
77+
/// <param name="eventTime">The event time.</param>
6078
/// <param name="roundSpan">The round span.</param>
6179
/// <returns>
6280
/// The Generated PartitionKey
6381
/// </returns>
6482
/// <remarks>
6583
/// The partition key based on the Timestamp rounded to the nearest 5 min
6684
/// </remarks>
67-
public static string GeneratePartitionKey(DateTime utcEventTime, TimeSpan? roundSpan = null)
85+
public static string GeneratePartitionKey(DateTime eventTime, TimeSpan? roundSpan = null)
6886
{
6987
var span = roundSpan ?? TimeSpan.FromMinutes(5);
70-
var roundedEvent = utcEventTime.Round(span);
88+
var dateTime = eventTime.ToUniversalTime();
89+
var roundedEvent = dateTime.Round(span);
7190

7291
// create a 19 character String for reverse chronological ordering.
7392
return $"{DateTime.MaxValue.Ticks - roundedEvent.Ticks:D19}";
7493
}
7594

95+
96+
/// <summary>
97+
/// Generates the RowKey using a reverse chronological ordering date, newest logs sorted first
98+
/// </summary>
99+
/// <param name="eventTime">The event time.</param>
100+
/// <returns>
101+
/// The generated RowKey
102+
/// </returns>
103+
public static string GenerateRowKey(DateTimeOffset eventTime)
104+
{
105+
return GenerateRowKey(eventTime.UtcDateTime);
106+
}
107+
76108
/// <summary>
77-
/// Generates the RowKey using the timestamp
109+
/// Generates the RowKey using a reverse chronological ordering date, newest logs sorted first
78110
/// </summary>
79-
/// <param name="utcEventTime">The UTC event time.</param>
111+
/// <param name="eventTime">The event time.</param>
80112
/// <returns>
81113
/// The generated RowKey
82114
/// </returns>
83-
public static string GenerateRowKey(DateTime utcEventTime)
115+
public static string GenerateRowKey(DateTime eventTime)
84116
{
117+
var dateTime = eventTime.ToUniversalTime();
118+
85119
// create a reverse chronological ordering date, newest logs sorted first
86-
var timestamp = utcEventTime.ToReverseChronological();
120+
var timestamp = dateTime.ToReverseChronological();
87121

88122
// use Ulid for speed and efficiency
89123
return Ulid.NewUlid(timestamp).ToString();
90124
}
125+
126+
127+
#if NET6_0_OR_GREATER
128+
/// <summary>
129+
/// Generates the partition key query using the specified <paramref name="date"/>.
130+
/// </summary>
131+
/// <param name="date">The date to use for query.</param>
132+
/// <returns>An Azure Table partiion key query.</returns>
133+
public static string GeneratePartitionKeyQuery(DateOnly date)
134+
{
135+
// date is assumed to be in local time, will be converted to UTC
136+
var eventTime = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Local);
137+
return GeneratePartitionKeyQuery(eventTime);
138+
}
139+
#endif
140+
141+
/// <summary>
142+
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
143+
/// </summary>
144+
/// <param name="eventTime">The date to use for query.</param>
145+
/// <returns>An Azure Table partiion key query.</returns>
146+
public static string GeneratePartitionKeyQuery(DateTime eventTime)
147+
{
148+
var dateTime = eventTime.ToUniversalTime();
149+
150+
var upper = dateTime.ToReverseChronological().Ticks.ToString("D19");
151+
var lower = dateTime.AddDays(1).ToReverseChronological().Ticks.ToString("D19");
152+
153+
return $"({PartitionKeyName} ge '{lower}') and ({PartitionKeyName} lt '{upper}')";
154+
}
155+
156+
/// <summary>
157+
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
158+
/// </summary>
159+
/// <param name="eventTime">The date to use for query.</param>
160+
/// <returns>An Azure Table partiion key query.</returns>
161+
public static string GeneratePartitionKeyQuery(DateTimeOffset eventTime)
162+
{
163+
return GeneratePartitionKeyQuery(eventTime.UtcDateTime);
164+
}
165+
91166
}

src/Serilog.Sinks.AzureTableStorage/Sinks/AzureTableStorage/Extensions/DateTimeExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public static class DateTimeExtensions
1818
/// <remarks>
1919
/// The partition key based on the Timestamp rounded to the nearest 5 min
2020
/// </remarks>
21-
[Obsolete("Use DefaultKeyGenerator instead")]
2221
public static string GeneratePartitionKey(this DateTime utcEventTime, TimeSpan? roundSpan = null)
2322
{
2423
return DefaultKeyGenerator.GeneratePartitionKey(utcEventTime, roundSpan);
@@ -31,7 +30,6 @@ public static string GeneratePartitionKey(this DateTime utcEventTime, TimeSpan?
3130
/// <returns>
3231
/// The generated RowKey
3332
/// </returns>
34-
[Obsolete("Use DefaultKeyGenerator instead")]
3533
public static string GenerateRowKey(this DateTime utcEventTime)
3634
{
3735
return DefaultKeyGenerator.GenerateRowKey(utcEventTime);

0 commit comments

Comments
 (0)