Serilog.Sinks.AzureBlobStorage 4.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Serilog.Sinks.AzureBlobStorage --version 4.0.1                
NuGet\Install-Package Serilog.Sinks.AzureBlobStorage -Version 4.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Serilog.Sinks.AzureBlobStorage" Version="4.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.AzureBlobStorage --version 4.0.1                
#r "nuget: Serilog.Sinks.AzureBlobStorage, 4.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Serilog.Sinks.AzureBlobStorage as a Cake Addin
#addin nuget:?package=Serilog.Sinks.AzureBlobStorage&version=4.0.1

// Install Serilog.Sinks.AzureBlobStorage as a Cake Tool
#tool nuget:?package=Serilog.Sinks.AzureBlobStorage&version=4.0.1                

Serilog.Sinks.AzureBlobStorage

Build status NuGet Badge

Writes to a file in Windows Azure Blob Storage.

Azure Blob Storage offers appending blobs, which allow you to add content quickly to a single blob without locking it for updates. For this reason, appending blobs are ideal for logging applications.

The AzureBlobStorage sink appends data to the blob in text format. Here's a sample line:

[2018-10-17 23:03:56 INF] Hello World!

Package - Serilog.Sinks.AzureBlobStorage | Platforms - netstandard2.0; netstandard2.1; net6.0; net8.0

Usage

var azureConnectionString = "my connection string";

var log = new LoggerConfiguration()
    .WriteTo.AzureBlobStorage(connectionString: azureConnectionString)
    .CreateLogger();

Because there are many similar method invocations using a string, it is recommended that you use named parameters as shown above.

You can also specify a named connection string, using the connectionStringName property in the constructor.

You must also provide an IConfiguration instance, either manually in the constructor or through dependency injection.

This example uses a named connection string called "myConnection".


var builder = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
    .AddEnvironmentVariables();
IConfigurationRoot config = builder.Build();

var log = new LoggerConfiguration()
    .WriteTo.AzureBlobStorage(connectionStringName: "myConnection", config)
    .CreateLogger();

You can avoid manually creating an IConfiguration if you use Two-stage Initialization or you have established dependency injection for IConfiguration.


public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .UseSerilog((context, services, configuration) => configuration
            .Enrich.FromLogContext()
            .WriteTo.Console()
            .WriteTo.AzureBlobStorage(connectionStringName: "MyConnectionString", context.Configuration)
        )
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

Other options

In addition to the storage connection, you can also specify:

  • Message line format (default: [{Timestamp:yyyy-MM-dd HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception})
  • Blob container (default: logs)
  • Blob filename (default: log.txt)

Configuration examples

Rolling file example

By default, the log file name is logs.txt, but you can add date substitutions to create a rolling file implementation. These are more fully shown in the Unit Test project. But as an example, you can create a log file name like this: {yyyy}/{MM}/{dd}/log.txt

  .WriteTo.AzureBlobStorage(cloudAccount, Serilog.Events.LogEventLevel.Information, storageFileName: "{yyyy}/{MM}/{dd}/log.txt")

On December 15, 2018 (when this was written), log files would appear to be in a folder structure as shown below:


\2018
-----\12
      ----\15
            log.txt

As of version 2.0.0, the values are not required to appear in descending order, e.g.: yy MM dd hh mm. In addition, the values can appear more than once. For example, this is a valid format string which will create the following file name:

{yyyy}/{MM}/{dd}/{yyyy}-{MM}-{dd}_{HH}:{mm}.txt
2019/06/20/2019-06-20_14:40.txt
Other substitutions in the file name.
  • You can add the LogEventLevel to the file name by using the {Level} descriptor. For example, use this file name template: {Level}.txt.
  • If you push properties into Serilog, you can use those within your file name template. Caution! If you do this, you must do it consistently. For more information, see the 'Multi-tenant support' example below.

All of these substitutions can be used in together and also with the date formats.

Maximum file size

You can limit the size of each file created as of version 2.0.0. There is a constructor parameter called blobSizeLimitBytes. By default, this is null, meaning that files can grow without limitation. By providing a value, you can specify the maximum size of a file. Logging more than this amount will cause a new file to be created.

Maximum number of files per container

You can limit the number of files created as of version 2.1.0. There is a constructor parameter called retainedBlobCountLimit to control this behavior. Once the limit is reached, a file will be deleted every time a new file is created in order to stay within this limit.

Batch posting example

As of version 4.0, the AzureBlobStorageSink uses batching exclusively for posting events, and uses Serilog 4.0's native batching features. There is no configuration required to take advantage of this feature. Batches are emitted every 2 seconds, if events are waiting. A single batch can include up to 1000 events.

If you want to control the batch posting limit and the period, you can do so by using the batchPostingLimit and period parameters.

An example configuration is:

  .WriteTo.AzureBlobStorage(blobServiceClient, Serilog.Events.LogEventLevel.Information, period:TimeSpan.FromSeconds(30), batchPostingLimit:50)

This configuration would post a new batch of events every 30 seconds, unless there were 50 or more events to post, in which case they would post before the time limit.

To specify batch posting using appsettings configuration, configure these values:

"WriteTo": [
    {
        "Name": "AzureBlobStorage",
        "Args": {
            "connectionString": "",
            "storageContainerName": "",
            "storageFileName": "",
            "period": "00:00:30", // optional sets the period to 30 secs
            "batchPostingLimit": "50", // optional, sets the max batch limit to 50
        }
    }
  ]

Multi-tenant support

From version 3.2.0, you can log using multiple log files, one per each tenant.

To configure, create a storage filename that includes a tenant id property.

loggerConfiguration.WriteTo.
     AzureBlobStorage("blobconnectionstring",
     LogEventLevel.Information, "Containername", storageFileName: "/{TenantId}/{yyyy}/{MM}/log{yyyy}{MM}{dd}.txt",
     writeInBatches: true, period: TimeSpan.FromSeconds(15), batchPostingLimit: 100);

Then, before writing a log entry, use the Serilog PushProperty method to add the TenantId property.

LogContext.PushProperty("TenantId", tenantId);

Development

Do not use the Azure Storage Emulator as a development tool, because it does not support Append Blobs. Instead, use Azurite, which is Microsoft's new tool for local storage emulation.

JSON configuration

It is possible to configure the sink using Serilog.Settings.Configuration by specifying the folder and file name and connection string in appsettings.json:

"Serilog": {
   "Using": [
      "Serilog.Sinks.AzureBlobStorage"
   ],
  "WriteTo": [
    {"Name": "AzureBlobStorage", "Args": {"connectionString": "", "storageContainerName": "", "storageFileName": ""}}
  ]
}

Example of authentication using Managed identity:

"Serilog": {
   "Using": [
      "Serilog.Sinks.AzureBlobStorage"
   ],
  "WriteTo": [
    {"Name": "AzureBlobStorage", "Args": { "formatter": "Serilog.Formatting.Json.JsonFormatter", "storageAccountUri": "", "storageContainerName": "", "storageFileName": ""}}
  ]
}

JSON configuration must be enabled using ReadFrom.Configuration(); see the documentation of the JSON configuration package for details.

XML <appSettings> configuration

To 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.AppSettings

Instead 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:AzureBlobStorage" value="Serilog.Sinks.AzureBlobStorage" />
    <add key="serilog:write-to:AzureBlobStorage.connectionString" value="DefaultEndpointsProtocol=https;AccountName=ACCOUNT_NAME;AccountKey=KEY;EndpointSuffix=core.windows.net" />
    <add key="serilog:write-to:AzureBlobStorage.formatter" value="Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact" />

Using Managed Identity

Managed identity allows you to access blob storage using either a system-assigned or user-assigned managed identity, rather than a connection string.

If you are using a system-assigned managed identity, provide the storageAccountUri argument as shown in the example above. To use a user-assigned managed identity, retrieve the identity value from your AppService or Virtual Machine configuration and provide it using the managedIdentityClientId parameter.

For more information on Managed Identity, please visit Managed identities for Azure resources.

Acknowledgements

This is a fork of the Serilog Azure Table storage sink. Thanks and acknowledgements to the original authors of that work.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on Serilog.Sinks.AzureBlobStorage:

Package Downloads
FenixAlliance.ACL.Dependencies

Application Component for the Alliance Business Suite.

FamilyHubs.SharedKernel

Package Description

VianaSoft-BuildingBlock

Os building blocks são projetados para serem independentes e autônomos, com funcionalidades específicas. Essa abordagem modular facilita a criação, manutenção e evolução de software, pois permite que os desenvolvedores construam sistemas complexos a partir de partes menores e mais gerenciáveis.

VianaHub-BuildingBlocks

Os building blocks são projetados para serem independentes e autônomos, com funcionalidades específicas. Essa abordagem modular facilita a criação, manutenção e evolução de software, pois permite que os desenvolvedores construam sistemas complexos a partir de partes menores e mais gerenciáveis.

SAWL

Utility for creating Serilog Azure Blob Logs for COREWCF and other project types.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
4.0.5 17,493 10/30/2024
4.0.3 144,188 8/2/2024
4.0.2 21,308 7/23/2024
4.0.1 5,335 7/18/2024
4.0.0 5,557 7/17/2024
3.3.2 23,215 7/13/2024
3.3.1 194,317 4/29/2024
3.3.0 407,566 10/19/2023
3.2.0 542,294 5/31/2023
3.1.3 1,275,332 8/3/2022
3.1.2 860,868 4/9/2022
3.1.1 161,738 2/23/2022
3.1.0 5,427 2/23/2022
3.0.3 818,481 9/6/2021
3.0.2 168,961 7/5/2021
3.0.1 60,729 5/31/2021
3.0.0 45,108 5/21/2021
2.1.2 160,316 4/20/2021
2.1.1 261,786 2/4/2021
2.1.0 134,181 12/17/2020
2.0.2 253,684 8/29/2020
1.4.0 614,015 6/15/2019
1.2.3 167,446 3/27/2019
1.1.1 31,617 2/26/2019
1.0.4 797 2/22/2019
1.0.2 10,908 1/24/2019
1.0.1 7,671 1/23/2019
1.0.0.1 4,372 12/13/2018
0.8.15 1,722 11/28/2018
0.8.12 1,104 10/20/2018
0.8.10 845 10/18/2018
0.8.9 858 10/18/2018
0.8.8 823 10/16/2018
0.8.6 859 10/16/2018
0.8.5 859 10/13/2018
0.8.4 2,445 10/13/2018