Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Apr 25, 2024
1 parent cf12599 commit 869fecc
Show file tree
Hide file tree
Showing 8 changed files with 929 additions and 645 deletions.
7 changes: 2 additions & 5 deletions samples/SampleWebApplication/Pages/Logs.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using SampleWebApplication.Models;

using Serilog.Sinks.AzureTableStorage;
using Serilog.Sinks.AzureTableStorage.Extensions;

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

var dateTime = Date.Date.ToUniversalTime();
var upper = dateTime.GeneratePartitionKey();
var lower = dateTime.AddDays(1).GeneratePartitionKey();

var filter = $"({nameof(ITableEntity.PartitionKey)} ge '{lower}') and ({nameof(ITableEntity.PartitionKey)} lt '{upper}')";
var filter = DefaultKeyGenerator.GeneratePartitionKeyQuery(Date.Date);

if (!string.IsNullOrWhiteSpace(Level))
filter += $" and ({nameof(LogEventModel.Level)} eq '{Level}')";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Threading;

using Azure.Data.Tables;

using Serilog.Events;
using Serilog.Sinks.AzureTableStorage.Extensions;

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

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


/// <summary>
/// Generates the PartitionKey based on the specified <paramref name="eventTime"/> timestamp
/// </summary>
/// <param name="eventTime">The event time.</param>
/// <param name="roundSpan">The round span.</param>
/// <returns>
/// The Generated PartitionKey
/// </returns>
/// <remarks>
/// The partition key based on the Timestamp rounded to the nearest 5 min
/// </remarks>
public static string GeneratePartitionKey(DateTimeOffset eventTime, TimeSpan? roundSpan = null)
{
return GeneratePartitionKey(eventTime.UtcDateTime, roundSpan);
}

/// <summary>
/// Generates the PartitionKey based on the logEvent timestamp
/// Generates the PartitionKey based on the specified <paramref name="eventTime"/> timestamp
/// </summary>
/// <param name="utcEventTime">The UTC event time.</param>
/// <param name="eventTime">The event time.</param>
/// <param name="roundSpan">The round span.</param>
/// <returns>
/// The Generated PartitionKey
/// </returns>
/// <remarks>
/// The partition key based on the Timestamp rounded to the nearest 5 min
/// </remarks>
public static string GeneratePartitionKey(DateTime utcEventTime, TimeSpan? roundSpan = null)
public static string GeneratePartitionKey(DateTime eventTime, TimeSpan? roundSpan = null)
{
var span = roundSpan ?? TimeSpan.FromMinutes(5);
var roundedEvent = utcEventTime.Round(span);
var dateTime = eventTime.ToUniversalTime();
var roundedEvent = dateTime.Round(span);

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


/// <summary>
/// Generates the RowKey using a reverse chronological ordering date, newest logs sorted first
/// </summary>
/// <param name="eventTime">The event time.</param>
/// <returns>
/// The generated RowKey
/// </returns>
public static string GenerateRowKey(DateTimeOffset eventTime)
{
return GenerateRowKey(eventTime.UtcDateTime);
}

/// <summary>
/// Generates the RowKey using the timestamp
/// Generates the RowKey using a reverse chronological ordering date, newest logs sorted first
/// </summary>
/// <param name="utcEventTime">The UTC event time.</param>
/// <param name="eventTime">The event time.</param>
/// <returns>
/// The generated RowKey
/// </returns>
public static string GenerateRowKey(DateTime utcEventTime)
public static string GenerateRowKey(DateTime eventTime)
{
var dateTime = eventTime.ToUniversalTime();

// create a reverse chronological ordering date, newest logs sorted first
var timestamp = utcEventTime.ToReverseChronological();
var timestamp = dateTime.ToReverseChronological();

// use Ulid for speed and efficiency
return Ulid.NewUlid(timestamp).ToString();
}


#if NET6_0_OR_GREATER
/// <summary>
/// Generates the partition key query using the specified <paramref name="date"/>.
/// </summary>
/// <param name="date">The date to use for query.</param>
/// <returns>An Azure Table partiion key query.</returns>
public static string GeneratePartitionKeyQuery(DateOnly date)
{
// date is assumed to be in local time, will be converted to UTC
var eventTime = new DateTime(date.Year, date.Month, date.Day, 0, 0, 0, DateTimeKind.Local);
return GeneratePartitionKeyQuery(eventTime);
}
#endif

/// <summary>
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
/// </summary>
/// <param name="eventTime">The date to use for query.</param>
/// <returns>An Azure Table partiion key query.</returns>
public static string GeneratePartitionKeyQuery(DateTime eventTime)
{
var dateTime = eventTime.ToUniversalTime();

var upper = dateTime.ToReverseChronological().Ticks.ToString("D19");
var lower = dateTime.AddDays(1).ToReverseChronological().Ticks.ToString("D19");

return $"({PartitionKeyName} ge '{lower}') and ({PartitionKeyName} lt '{upper}')";
}

/// <summary>
/// Generates the partition key query using the specified <paramref name="eventTime"/>.
/// </summary>
/// <param name="eventTime">The date to use for query.</param>
/// <returns>An Azure Table partiion key query.</returns>
public static string GeneratePartitionKeyQuery(DateTimeOffset eventTime)
{
return GeneratePartitionKeyQuery(eventTime.UtcDateTime);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public static class DateTimeExtensions
/// <remarks>
/// The partition key based on the Timestamp rounded to the nearest 5 min
/// </remarks>
[Obsolete("Use DefaultKeyGenerator instead")]
public static string GeneratePartitionKey(this DateTime utcEventTime, TimeSpan? roundSpan = null)
{
return DefaultKeyGenerator.GeneratePartitionKey(utcEventTime, roundSpan);
Expand All @@ -31,7 +30,6 @@ public static string GeneratePartitionKey(this DateTime utcEventTime, TimeSpan?
/// <returns>
/// The generated RowKey
/// </returns>
[Obsolete("Use DefaultKeyGenerator instead")]
public static string GenerateRowKey(this DateTime utcEventTime)
{
return DefaultKeyGenerator.GenerateRowKey(utcEventTime);
Expand Down
Loading

0 comments on commit 869fecc

Please sign in to comment.