Code Monkey home page Code Monkey logo

serilog-sinks-azureblobstorage's Introduction

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 - .NET 4.5.2, .Net Standard 2.0

Usage

var connectionString = CloudStorageAccount.Parse("ConnnectionString");

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

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(connectionString, Serilog.Events.LogEventLevel.Information, null, "{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

In the file name, the values must appear in descending order, e.g.: yy MM dd hh mm, although it is not required to include all date elements.

Batch posting example

By default, whenever there is a new event to post, the Azure Blob Storage sink will send it to Azure storage. For cost-management or performance reasons, you can choose to "batch" the posting of new log events.

You should create the sink by calling the AzureBatchingBlobStorageSink class, which inherits from PeriodicBatchingSink.

An example configuration is:

  .WriteTo.AzureBlobStorage(connectionString, Serilog.Events.LogEventLevel.Information, null, null, null, true, TimeSpan.FromSeconds(15), 10)

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

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": ""}}
  ]
}

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 '' 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" />

A note about Unit Testing

Unfortunately the Azure Storage emulator does not support append blobs, so I'm omitted unit tests from this project. I'd love to have unit tests, but I'd like to have them be able to run on Azure Dev Ops hosted build agents. Suggestions?

Acknowledgements

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

serilog-sinks-azureblobstorage's People

Contributors

alexeysas avatar azure-pipelines[bot] avatar brolaugh avatar chriswill avatar daanwasscher avatar tomseida avatar vip32 avatar yvonnearnoldus avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.