11using System ;
22using System . Threading ;
33
4+ using Azure . Data . Tables ;
5+
46using Serilog . Events ;
57using Serilog . Sinks . AzureTableStorage . Extensions ;
68
@@ -11,6 +13,7 @@ namespace Serilog.Sinks.AzureTableStorage;
1113/// </summary>
1214public 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}
0 commit comments