Serilog.Sinks.Logz.Io 7.2.0

dotnet add package Serilog.Sinks.Logz.Io --version 7.2.0                
NuGet\Install-Package Serilog.Sinks.Logz.Io -Version 7.2.0                
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.Logz.Io" Version="7.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Serilog.Sinks.Logz.Io --version 7.2.0                
#r "nuget: Serilog.Sinks.Logz.Io, 7.2.0"                
#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.Logz.Io as a Cake Addin
#addin nuget:?package=Serilog.Sinks.Logz.Io&version=7.2.0

// Install Serilog.Sinks.Logz.Io as a Cake Tool
#tool nuget:?package=Serilog.Sinks.Logz.Io&version=7.2.0                

A Serilog sink sending log events over HTTP to logz.io

Available Packages

Serilog.Sinks.Logz.Io

NuGet Version NuGet Documentation Join the chat at https://gitter.im/serilog/serilog Help

Package - Serilog.Sinks.Logz.Io | Platforms - NET7.0, NET 6.0, .NET Standard 2.0, .NET 4.6.1

Warning: breaking changes

v7 has breaking changes.

  • DEFAULT FIELD NAME FORMATTING SETTINGS MIGHT BE CHANGED DEPENDING ON YOUR CONFIGURATION, PLEASE DOUBLE CHECK
  • batchPostingLimit field renamed to logEventsInBatchLimit to match Serilog.Sinks.Http library
  • bufferPathFormat renamed to bufferBaseFileName
  • in addition rolling interval is now set in separate property: bufferRollingInterval

So now instead of doing:

    "bufferPathFormat": "Buffer-{Hour}.json",

need to change config file to:

    "bufferBaseFileName": "Buffer",
    "bufferRollingInterval": "Hour",
  • data center configuration is moved to a separate object

So now instead of doing:

    "useHttps": true,
    "dataCenterSubDomain": "listener",
    "port": null

need to change config file to:

    "dataCenter": {
        "useHttps": true,
        "dataCenterSubDomain": "listener",
        "port": null
    }

For more information see: https://github.com/FantasticFiasco/serilog-sinks-http/releases/tag/v8.0.0

Installation

If you want to include the HTTP sink in your project, you can install it directly from NuGet.

To install the sink, run the following command in the Package Manager Console:

PM> Install-Package Serilog.Sinks.Logz.Io

Serilog.Sinks.Http.LogzIo

This package has been deprecated as it is legacy and is no longer maintained. Please update to Serilog.Sinks.Logz.Io. Latest version contains exact the same functionality and extension methods to configure loggers.

Usage

In the following example, the sink will POST log events to https://listener-eu.logz.io:8071/?type=app&token=<token> over HTTP. We configure the sink using named arguments instead of positional because historically we've seen that most breaking changes where the result of a new parameter describing a new feature. Using named arguments means that you more often than not can migrate to new major versions without any changes to your code.

Used in conjunction with Serilog.Settings.Configuration the same sink can be configured in the following way:

{
  "Serilog": {
    "MinimumLevel": "Warning",
    "WriteTo": [
      {
        "Name": "LogzIoDurableHttp",
        "Args": {
          "requestUri": "https://listener-eu.logz.io:8071/?type=app&token=<token>"
        }
      }
    ]
  }
}

The sink will be configured as durable, i.e. log events are persisted on disk before being sent over the network, thus protected against data loss after a system or process restart. For more information please read the wiki.

Internals

Library depends on a fantastic Serilog.Sinks.Http package which allows to create buffered files and controls HTTP uploads.

Here we are using DurableHttpUsingTimeRolledBuffers extension method to configure durable HTTP sink.

See: DurableHttpUsingTimeRolledBuffers

Wiki: Durable time rolled HTTP sink

Serializer

Library is using Newtonsoft.Json package for serialization. However it is possible to change/configure serialization behavior using LogzIoSerializer.Instance property. This instance should be configured at application startup (best if it happens before Serilog configuration).

Default behavior which is set automatically:

LogzIoSerializer.Instance = new LogzIoSerializer(LogzIoTextFormatterFieldNaming.CamelCase, false);

You can also set custom JsonSerializerSettings using following code:

LogzIoSerializer.Instance.WithSerializerSettings(customSetttings);

Of course you can override complete serialization logic by implementing ILogzIoSerializer interface and configuring your own implementation.

Multicast delegates

Newtonsoft.JSON package which is used to serialize log entries is also able to serialize lamdbas, delegates and similar functional constructs which normally does not make sense to have in logs. It also might produce huge blobs with assembly information thus increasing log entry size to unacceptable sizes.

In order to handle this issue MulticastDelegate type serialization is overriden using custom JsonConverter.

If you still want to enable this behavior - at application startup you can configure package serializer using following code:

LogzIoSerializer.Instance = new LogzIoSerializer(LogzIoTextFormatterFieldNaming.CamelCase, false);

More advanced examples

NOTE: in example bellow only requestUri is required. All other properties represents default values.

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft.AspNetCore": "Debug"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
    "WriteTo": [
      {
        "Name": "LogzIoDurableHttp",
        "Args": {
          "requestUri": "https://listener-eu.logz.io:8071/?type=app&token=<token>",
          "bufferBaseFileName": "Buffer",
          "bufferRollingInterval": "Day",
          "bufferFileSizeLimitBytes": "104857600",
          "bufferFileShared": false,
          "retainedBufferFileCountLimit": 31,
          "logEventsInBatchLimit": 1000,
          "period": null,
          "restrictedToMinimumLevel": "Minimum",
          "logzioTextFormatterOptions": {
            "BoostProperties": true,
            "LowercaseLevel": true,
            "IncludeMessageTemplate": true,
            "FieldNaming": "CamelCase",
            "EventSizeLimitBytes": 261120
          }
        }
      }
    ]
  }
}

In the example above logzIo formatter options are default, there is no need to specify these if it fits your needs.

  • BoostProperties: When true, does not add 'properties' prefix.
  • LowercaseLevel: Set to true to push log level as lowercase.
  • IncludeMessageTemplate: When true the message template is included in the logs.
  • FieldNaming: allows to transform field names, possible values: null (default), CamelCase, LowerCase

Configuration from code

    configuration
        .WriteTo.LogzIoDurableHttp(
            "https://listener-eu.logz.io:8071/?type=app&token=<token>",
            logzioTextFormatterOptions: new LogzioTextFormatterOptions
            {
                BoostProperties = true,
                LowercaseLevel = true,
                IncludeMessageTemplate = true,
                FieldNaming = LogzIoTextFormatterFieldNaming.CamelCase,
                EventSizeLimitBytes = 261120,
            })
        .MinimumLevel.Verbose();

Logs without buffered file

NOTE: it is strongly recommended to use durable logger. It is much more robust and tollerates various network issues thus your logs won't be lost.

In the following example, the sink will POST log events to https://listener.logz.io over HTTPS.

ILogger log = new LoggerConfiguration()
  .MinimumLevel.Verbose()
  .WriteTo.LogzIo("<logzio token>", "<log type>", useHttps: true)
  .CreateLogger();

log.Information("Logging {@Heartbeat} from {Computer}", heartbeat, computer);

More advanced configuration is also available:

ILogger log = new LoggerConfiguration()
  .MinimumLevel.Verbose()
  .WriteTo.LogzIo("<logzio token>", "<log type>",
    new LogzioOptions 
    { 
        RestrictedToMinimumLevel = LogEventLevel.Debug,
        Period = TimeSpan.FromSeconds(15),
        LogEventsInBatchLimit = 50
    })
  .CreateLogger();

Alternatively configuration can be done within your appsettings.json file:

{
  "Serilog": {
    "MinimumLevel": {
      "Default": "Verbose"
    },
    "WriteTo": [
      {
        "Name": "LogzIo",
        "Args": {
          "authToken": "<logzio token>",
          "type": "<log type>",
          "dataCenterSubDomain": "listener",
          "dataCenter": {
              "subDomain": "listener",
              "useHttps": true
          },
          "logEventsInBatchLimit": 5000,
          "period": "00:00:02",
          "restrictedToMinimumLevel": "Debug",
          "lowercaseLevel": false,
          "environment": "",
          "serviceName": ""
        }
      }
    ]
  }
}

Elastic Common Schema support

See for more details: https://www.elastic.co/guide/en/ecs/current/ecs-reference.html

ILogger log = new LoggerConfiguration()
  .MinimumLevel.Verbose()
  .WriteTo.LogzIo(new LogzioEcsOptions
        {
            Type = "<log type>",
            AuthToken = "<logzio token>",
            DataCenter = new LogzioDataCenter
            {
                SubDomain = "listener",
                Port = 8701,
                UseHttps = true
            }
        },
        batchPostingLimit: 50,
        period: TimeSpan.FromSeconds(15),
        restrictedToMinimumLevel: LogEventLevel.Debug
    )
  .CreateLogger();

Alternatively configuration can be done within your appsettings.json file, please note CustomEcsTextFormatterConfiguration setup is optional:

{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.Logz.Io"
    ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "System": "Warning"
      }
    },
    "WriteTo": [
      { "Name": "Console" },
      {
        "Name": "LogzIoEcs",
        "Args": {
          "options": {
            "type": "<log type>",
            "authToken": "<logzio token>"
          },
          "logEventsInBatchLimit": 30,
          "period": "00:00:02",
          "restrictedToMinimumLevel": "Debug",
          "formatterConfiguration": "Serilog.Sinks.Logz.Io.AspNetCoreApi.Logging.CustomEcsTextFormatterConfiguration, Serilog.Sinks.Logz.Io.AspNetCoreApi"
        }
      }
    ]
  }
}
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 is compatible.  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 was computed.  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 was computed. 
.NET Framework net461 is compatible.  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 (2)

Showing the top 2 NuGet packages that depend on Serilog.Sinks.Logz.Io:

Package Downloads
AlbaLogger

Package Description

AllbatrossLogger

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.2.0 342,942 12/12/2022
7.1.0 63,869 10/12/2022
7.0.0 6,968 10/12/2022
6.0.0 569,750 11/14/2021
5.1.0 22,905 10/2/2021
5.0.0 23,139 9/25/2021
3.0.1 93,344 2/9/2021
3.0.0 755 2/9/2021
2.3.0 94,377 10/30/2020
2.2.3 10,662 10/1/2020
2.2.2 82,233 4/16/2020
2.2.1 93,835 12/26/2018
2.2.0 2,568 12/20/2018
2.1.2 1,245 12/17/2018
2.1.1 4,349 10/17/2018
2.1.1-alpha.1 473 10/16/2018
2.1.0 911 10/10/2018
2.0.0 5,792 5/21/2018
1.2.4 1,196 5/16/2018
1.2.3 3,136 3/15/2018
1.2.2 1,112 3/13/2018
1.1.1 2,403 3/4/2018
1.1.0 1,067 3/4/2018
1.0.0 1,214 3/1/2018

Upgrade dependencies