Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to Serilog 4.0 #157

Merged
merged 2 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,20 @@ public static class Program

### Change Log

10.0.0
* Breaking: writeInBatches removed, all writes are now batched
* Update: update to serilog 4.0
* Remove: removed dependance on Serilog.Sinks.PeriodicBatching, use serilog 4.0 `IBatchedLogEventSink`

9.6.0
* Fix: improve timezone support

9.5.0
* Add: use ULID for rowkey for speed and efficiency

9.4.0
* Fix: prevent duplicate rowkey

9.1.0
* Add: Built-in trace and span id support

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using Serilog.Formatting;
using Serilog.Formatting.Json;
using Serilog.Sinks.AzureTableStorage;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog;

Expand Down Expand Up @@ -53,7 +52,6 @@ public static class LoggerConfigurationAzureTableStorageExtensions
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -70,7 +68,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand All @@ -92,7 +89,6 @@ public static LoggerConfiguration AzureTableStorage(
restrictedToMinimumLevel: restrictedToMinimumLevel,
formatProvider: formatProvider,
storageTableName: storageTableName,
writeInBatches: writeInBatches,
period: period,
batchPostingLimit: batchPostingLimit,
keyGenerator: keyGenerator,
Expand All @@ -111,7 +107,6 @@ public static LoggerConfiguration AzureTableStorage(
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -128,7 +123,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand All @@ -150,7 +144,6 @@ public static LoggerConfiguration AzureTableStorage(
restrictedToMinimumLevel: restrictedToMinimumLevel,
formatProvider: formatProvider,
storageTableName: storageTableName,
writeInBatches: writeInBatches,
period: period,
batchPostingLimit: batchPostingLimit,
keyGenerator: keyGenerator,
Expand All @@ -171,7 +164,6 @@ public static LoggerConfiguration AzureTableStorage(
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -189,7 +181,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand All @@ -214,7 +205,6 @@ public static LoggerConfiguration AzureTableStorage(
restrictedToMinimumLevel: restrictedToMinimumLevel,
formatProvider: formatProvider,
storageTableName: storageTableName,
writeInBatches: writeInBatches,
period: period,
batchPostingLimit: batchPostingLimit,
keyGenerator: keyGenerator,
Expand All @@ -233,7 +223,6 @@ public static LoggerConfiguration AzureTableStorage(
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink;</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -251,7 +240,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand All @@ -268,7 +256,6 @@ public static LoggerConfiguration AzureTableStorage(
if (storageAccount == null)
throw new ArgumentNullException(nameof(storageAccount));

ILogEventSink sink;
try
{
var options = new AzureTableStorageSinkOptions
Expand All @@ -292,30 +279,22 @@ public static LoggerConfiguration AzureTableStorage(
keyGenerator: keyGenerator,
tableClientFactory: tableClientFactory);

if (writeInBatches)
var batchingOptions = new BatchingOptions
{
// wrap in PeriodicBatchingSink
var batchingOptions = new PeriodicBatchingSinkOptions
{
BatchSizeLimit = batchPostingLimit ?? DefaultBatchSizeLimit,
EagerlyEmitFirstEvent = true,
Period = period ?? DefaultPeriod,
};

sink = new PeriodicBatchingSink(tableStorageSink, batchingOptions);
}
else
{
sink = tableStorageSink;
}
BatchSizeLimit = batchPostingLimit ?? DefaultBatchSizeLimit,
EagerlyEmitFirstEvent = true,
BufferingTimeLimit = period ?? DefaultPeriod,
};

return loggerConfiguration.Sink(tableStorageSink, batchingOptions, restrictedToMinimumLevel);
}
catch (Exception ex)
{
Debugging.SelfLog.WriteLine($"Error configuring AzureTableStorage: {ex}");
sink = new LoggerConfiguration().CreateLogger();
var sink = new LoggerConfiguration().CreateLogger();
return loggerConfiguration.Sink(sink);
}

return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
}

/// <summary>
Expand All @@ -327,7 +306,6 @@ public static LoggerConfiguration AzureTableStorage(
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -345,7 +323,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand Down Expand Up @@ -373,7 +350,6 @@ public static LoggerConfiguration AzureTableStorage(
restrictedToMinimumLevel: restrictedToMinimumLevel,
formatProvider: formatProvider,
storageTableName: storageTableName,
writeInBatches: writeInBatches,
period: period,
batchPostingLimit: batchPostingLimit,
keyGenerator: keyGenerator,
Expand Down Expand Up @@ -404,7 +380,6 @@ public static LoggerConfiguration AzureTableStorage(
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="storageTableName">Table name that log entries will be written to. Note: Optional, setting this may impact performance</param>
/// <param name="writeInBatches">Use a periodic batching sink, as opposed to a synchronous one-at-a-time sink</param>
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param>
/// <param name="period">The time to wait between checking for event batches.</param>
/// <param name="keyGenerator">The key generator used to create the PartitionKey and the RowKey for each log entry</param>
Expand All @@ -423,7 +398,6 @@ public static LoggerConfiguration AzureTableStorage(
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
string storageTableName = null,
bool writeInBatches = true,
TimeSpan? period = null,
int? batchPostingLimit = null,
IKeyGenerator keyGenerator = null,
Expand Down Expand Up @@ -460,7 +434,6 @@ public static LoggerConfiguration AzureTableStorage(
restrictedToMinimumLevel: restrictedToMinimumLevel,
formatProvider: formatProvider,
storageTableName: storageTableName,
writeInBatches: writeInBatches,
period: period,
batchPostingLimit: batchPostingLimit,
keyGenerator: keyGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@

<ItemGroup>
<PackageReference Include="MinVer" Version="5.0.0" PrivateAssets="All" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.PeriodicBatching" Version="4.1.1" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Azure.Data.Tables" Version="12.8.3" />
<PackageReference Include="ulid" Version="1.3.3" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@

using Serilog.Core;
using Serilog.Events;
using Serilog.Sinks.PeriodicBatching;

namespace Serilog.Sinks.AzureTableStorage;

/// <summary>
/// Writes log events as records to an Azure Table Storage table.
/// </summary>
public class AzureTableStorageSink : ILogEventSink, IBatchedLogEventSink
public class AzureTableStorageSink : IBatchedLogEventSink
{
private readonly TableServiceClient _tableServiceClient;
private readonly AzureTableStorageSinkOptions _options;
Expand Down Expand Up @@ -71,25 +70,12 @@ public AzureTableStorageSink(
_tableClientFactory = tableClientFactory ?? new DefaultTableClientFactory();
}

/// <summary>
/// Emit the provided log event to the sink.
/// </summary>
/// <param name="logEvent">The log event to write.</param>
/// <exception cref="System.NotImplementedException"></exception>
public void Emit(LogEvent logEvent)
{
var document = _documentFactory.Create(logEvent, _options, _keyGenerator);
var tableClient = _tableClientFactory.CreateTableClient(_options, _tableServiceClient);

tableClient.AddEntity(document);
}

/// <summary>
/// Emit a batch of log events, running asynchronously.
/// </summary>
/// <param name="batch">The batch of events to emit.</param>
/// <returns></returns>
public async Task EmitBatchAsync(IEnumerable<LogEvent> batch)
public async Task EmitBatchAsync(IReadOnlyCollection<LogEvent> batch)
{
// write documents in batches by partition key
var documentGroups = batch
Expand All @@ -116,8 +102,5 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> batch)
/// or timers (thus avoiding additional flush/shut-down complexity).
/// </summary>
/// <returns></returns>
public Task OnEmptyBatchAsync()
{
return Task.CompletedTask;
}
public Task OnEmptyBatchAsync() => Task.CompletedTask;
}
Loading