@@ -31,7 +31,7 @@ public virtual string GeneratePartitionKey(LogEvent logEvent, AzureTableStorageS
31
31
var utcEventTime = logEvent . Timestamp . UtcDateTime ;
32
32
var partitionKeyRounding = options ? . PartitionKeyRounding ;
33
33
34
- return utcEventTime . GeneratePartitionKey ( partitionKeyRounding ) ;
34
+ return GeneratePartitionKey ( utcEventTime , partitionKeyRounding ) ;
35
35
}
36
36
37
37
/// <summary>
@@ -48,7 +48,7 @@ public virtual string GenerateRowKey(LogEvent logEvent, AzureTableStorageSinkOpt
48
48
// row key created in reverse chronological order so newest are always first
49
49
50
50
var utcEventTime = logEvent . Timestamp . UtcDateTime ;
51
- return utcEventTime . GenerateRowKey ( ) ;
51
+ return GenerateRowKey ( utcEventTime ) ;
52
52
}
53
53
54
54
@@ -67,7 +67,7 @@ public virtual string GenerateRowKey(LogEvent logEvent, AzureTableStorageSinkOpt
67
67
public static string GeneratePartitionKey ( DateTime utcEventTime , TimeSpan ? roundSpan = null )
68
68
{
69
69
var span = roundSpan ?? TimeSpan . FromMinutes ( 5 ) ;
70
- var roundedEvent = Round ( utcEventTime , span ) ;
70
+ var roundedEvent = utcEventTime . Round ( span ) ;
71
71
72
72
// create a 19 character String for reverse chronological ordering.
73
73
return $ "{ DateTime . MaxValue . Ticks - roundedEvent . Ticks : D19} ";
@@ -82,37 +82,10 @@ public static string GeneratePartitionKey(DateTime utcEventTime, TimeSpan? round
82
82
/// </returns>
83
83
public static string GenerateRowKey ( DateTime utcEventTime )
84
84
{
85
- // create a reverse chronological ordering date
86
- var targetTicks = DateTime . MaxValue . Ticks - utcEventTime . Ticks ;
85
+ // create a reverse chronological ordering date, newest logs sorted first
86
+ var timestamp = utcEventTime . ToReverseChronological ( ) ;
87
87
88
- // add incrementing value to ensure unique
89
- int padding = Next ( ) ;
90
-
91
- return $ "{ targetTicks : D19} { padding : D4} ";
88
+ // use Ulid for speed and efficiency
89
+ return Ulid . NewUlid ( timestamp ) . ToString ( ) ;
92
90
}
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
-
118
91
}
0 commit comments