Serilog event sink that writes to Azure Table Storage over HTTP.
$ dotnet add package Serilog.Sinks.AzureTableStorageWrites to a table in Azure Table Storage.
Package - Serilog.Sinks.AzureTableStorage | Platforms - .NET Standard 2.0
var log = new LoggerConfiguration()
.WriteTo.AzureTableStorage("<connectionString>")
.CreateLogger();
| Configuration | Description | Default |
|---|---|---|
| connectionString | The Cloud Storage Account connection string | |
| sharedAccessSignature | The storage account/table SAS key | |
| accountName | The storage account name | |
| restrictedToMinimumLevel | The minimum log event level required in order to write an event to the sink. | Verbose |
| formatProvider | Culture-specific formatting information | |
| storageTableName | Table name that log entries will be written to | LogEvent |
| batchPostingLimit | The maximum number of events to post in a single batch | 100 |
| period | The time to wait between checking for event batches | 0:0:2 |
| keyGenerator | The key generator used to create the PartitionKey and the RowKey for each log entry | DefaultKeyGenerator |
| propertyColumns | Specific log event properties to be written as table columns |
| bypassTableCreationValidation | Bypass the exception in case the table creation fails | false |
| documentFactory | Provider to create table document from LogEvent | DefaultDocumentFactory |
| tableClientFactory | Provider to create table client | DefaultTableClientFactory |
| partitionKeyRounding | Partition key rounding time span | 0:5:0 |
It is possible to configure the sink using Serilog.Settings.Configuration by specifying the table name and connection string in appsettings.json:
"Serilog": {
"WriteTo": [
{"Name": "AzureTableStorage", "Args": {"storageTableName": "", "connectionString": ""}}
]
}JSON configuration must be enabled using ReadFrom.Configuration(); see the documentation of the JSON configuration package for details.
<appSettings> configurationTo use the file sink with the Serilog.Settings.AppSettings package, first install that package if you haven't already done so:
Install-Package Serilog.Settings.AppSettingsInstead of configuring the logger in code, call ReadFrom.AppSettings():
var log = new LoggerConfiguration()
.ReadFrom.AppSettings()
.CreateLogger();In your application's App.config or Web.config file, specify the file sink assembly and required path format under the <appSettings> node:
<configuration>
<appSettings>
<add key="serilog:using:AzureTableStorage" value="Serilog.Sinks.AzureTableStorage" />
<add key="serilog:write-to:AzureTableStorage.connectionString" value="DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=KEY;EndpointSuffix=core.windows.net" />
<add key="serilog:write-to:AzureTableStorage.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />
</appSettings>
</configuration>public static class Program
{
private const string OutputTemplate = "{Timestamp:HH:mm:ss.fff} [{Level:u1}] {Message:lj}{NewLine}{Exception}";
public static async Task<int> Main(string[] args)
{
// azure home directory
var homeDirectory = Environment.GetEnvironmentVariable("HOME") ?? ".";
var logDirectory = Path.Combine(homeDirectory, "LogFiles");
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Verbose()
.MinimumLevel.Override("Microsoft", LogEventLevel.Information)
.Enrich.FromLogContext()
.WriteTo.Console(outputTemplate: OutputTemplate)
.WriteTo.File(
path: $"{logDirectory}/boot.txt",
rollingInterval: RollingInterval.Day,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1),
outputTemplate: OutputTemplate,
retainedFileCountLimit: 10
)
.CreateBootstrapLogger();
try
{
Log.Information("Starting web host");
var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args);
builder.Host
.UseSerilog((context, services, configuration) => configuration
.ReadFrom.Configuration(context.Configuration)
.ReadFrom.Services(services)
.Enrich.FromLogContext()
.Enrich.WithProperty("ApplicationName", builder.Environment.ApplicationName)
.Enrich.WithProperty("EnvironmentName", builder.Environment.EnvironmentName)
.WriteTo.Console(outputTemplate: OutputTemplate)
.WriteTo.File(
path: $"{logDirectory}/log.txt",
rollingInterval: RollingInterval.Day,
shared: true,
flushToDiskInterval: TimeSpan.FromSeconds(1),
outputTemplate: OutputTemplate,
retainedFileCountLimit: 10
)
.WriteTo.AzureTableStorage(
connectionString: context.Configuration.GetConnectionString("StorageAccount"),
propertyColumns: new[] { "SourceContext", "RequestId", "RequestPath", "ConnectionId", "ApplicationName", "EnvironmentName" }
)
);
ConfigureServices(builder);
var app = builder.Build();
ConfigureMiddleware(app);
await app.RunAsync();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
finally
{
await Log.CloseAndFlushAsync();
}
}
private static void ConfigureServices(WebApplicationBuilder builder)
{
}
private static void ConfigureMiddleware(Microsoft.AspNetCore.Builder.WebApplication app)
{
}
}10.0.0
IBatchedLogEventSink9.6.0
9.5.0
9.4.0
9.1.0
9.0.0
IDocumentFactory.Create add AzureTableStorageSinkOptions and IKeyGenerator argumentsIKeyGenerator.GeneratePartitionKey add AzureTableStorageSinkOptions argumentIKeyGenerator.GenerateRowKey add AzureTableStorageSinkOptions argument8.5.0
8.0.0
7.0.0
6.0.0
5.0.0
1.5