1
1
using System ;
2
+ using System . Threading ;
2
3
3
4
using Serilog . Events ;
4
5
using Serilog . Sinks . AzureTableStorage . Extensions ;
@@ -10,6 +11,7 @@ namespace Serilog.Sinks.AzureTableStorage;
10
11
/// </summary>
11
12
public class DefaultKeyGenerator : IKeyGenerator
12
13
{
14
+
13
15
/// <summary>
14
16
/// Automatically generates the PartitionKey based on the logEvent timestamp
15
17
/// </summary>
@@ -33,7 +35,7 @@ public virtual string GeneratePartitionKey(LogEvent logEvent, AzureTableStorageS
33
35
}
34
36
35
37
/// <summary>
36
- /// Automatically generates the RowKey using the timestamp
38
+ /// Automatically generates the RowKey using the timestamp
37
39
/// </summary>
38
40
/// <param name="logEvent">the log event</param>
39
41
/// <param name="options">The table storage options.</param>
@@ -48,4 +50,69 @@ public virtual string GenerateRowKey(LogEvent logEvent, AzureTableStorageSinkOpt
48
50
var utcEventTime = logEvent . Timestamp . UtcDateTime ;
49
51
return utcEventTime . GenerateRowKey ( ) ;
50
52
}
53
+
54
+
55
+
56
+ /// <summary>
57
+ /// Generates the PartitionKey based on the logEvent timestamp
58
+ /// </summary>
59
+ /// <param name="utcEventTime">The UTC event time.</param>
60
+ /// <param name="roundSpan">The round span.</param>
61
+ /// <returns>
62
+ /// The Generated PartitionKey
63
+ /// </returns>
64
+ /// <remarks>
65
+ /// The partition key based on the Timestamp rounded to the nearest 5 min
66
+ /// </remarks>
67
+ public static string GeneratePartitionKey ( DateTime utcEventTime , TimeSpan ? roundSpan = null )
68
+ {
69
+ var span = roundSpan ?? TimeSpan . FromMinutes ( 5 ) ;
70
+ var roundedEvent = Round ( utcEventTime , span ) ;
71
+
72
+ // create a 19 character String for reverse chronological ordering.
73
+ return $ "{ DateTime . MaxValue . Ticks - roundedEvent . Ticks : D19} ";
74
+ }
75
+
76
+ /// <summary>
77
+ /// Generates the RowKey using the timestamp
78
+ /// </summary>
79
+ /// <param name="utcEventTime">The UTC event time.</param>
80
+ /// <returns>
81
+ /// The generated RowKey
82
+ /// </returns>
83
+ public static string GenerateRowKey ( DateTime utcEventTime )
84
+ {
85
+ // create a reverse chronological ordering date
86
+ var targetTicks = DateTime . MaxValue . Ticks - utcEventTime . Ticks ;
87
+
88
+ // add incrementing value to ensure unique
89
+ int padding = Next ( ) ;
90
+
91
+ return $ "{ targetTicks : D19} { padding : D4} ";
92
+ }
93
+
94
+ /// <summary>
95
+ /// Rounds the specified date.
96
+ /// </summary>
97
+ /// <param name="date">The date to round.</param>
98
+ /// <param name="span">The span.</param>
99
+ /// <returns>The rounded date</returns>
100
+ public static DateTime Round ( DateTime date , TimeSpan span )
101
+ {
102
+ long ticks = ( date . Ticks + ( span . Ticks / 2 ) + 1 ) / span . Ticks ;
103
+ return new DateTime ( ticks * span . Ticks ) ;
104
+ }
105
+
106
+
107
+ private static int _counter = new Random ( ) . Next ( _minCounter , _maxCounter ) ;
108
+
109
+ private const int _minCounter = 1 ;
110
+ private const int _maxCounter = 9999 ;
111
+
112
+ private static int Next ( )
113
+ {
114
+ Interlocked . Increment ( ref _counter ) ;
115
+ return Interlocked . CompareExchange ( ref _counter , _minCounter , _maxCounter ) ;
116
+ }
117
+
51
118
}
0 commit comments