Skip to content

Durable file size rolled HTTP sink

FantasticFiasco edited this page Apr 10, 2022 · 9 revisions

The durable HTTP sink will send log events using HTTP POST over the network. The log events are initially stored on disk to prevent data loss, and then periodically sent over the network to the log server.

The buffer files will use a rolling behavior defined by the file size specified in bufferFileSizeLimitBytes, i.e. a new buffer file is created when the current buffer file has reached its limit. The maximum number of retained files is defined by retainedBufferFileCountLimit, and when that limit is reached the oldest file is dropped to make room for a new.

A durable sink will protect you against data loss after a system or process restart.

Example

ILogger log = new LoggerConfiguration()
  .MinimumLevel.Verbose()
  .WriteTo.DurableHttpUsingFileSizeRolledBuffers(requestUri: "https://www.mylogs.com")
  .CreateLogger();

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

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

{
  "Serilog": {
    "MinimumLevel": "Verbose",
    "WriteTo": [
      {
        "Name": "DurableHttpUsingFileSizeRolledBuffers",
        "Args": {
          "requestUri": "https://www.mylogs.com"
        }
      }
    ]
  }
}

Arguments

The following arguments are available when creating a durable HTTP sink using file size rolled buffers.

  • requestUri - The URI the request is sent to.

  • bufferBaseFileName - The relative or absolute path for a set of files that will be used to buffer events until they can be successfully transmitted across the network. Individual files will be created using the pattern bufferBaseFileName-*.txt, which should not clash with any other file names in the same directory. Default value is Buffer.

  • bufferFileSizeLimitBytes - The approximate maximum size, in bytes, to which a buffer file will be allowed to grow. For unrestricted growth, pass null. The default is 1 GB. To avoid writing partial events, the last event within the limit will be written in full even if it exceeds the limit.

  • bufferFileShared - Allow the buffer file to be shared by multiple processes. Default value is false.

  • retainedBufferFileCountLimit - The maximum number of buffer files that will be retained, including the current buffer file. Under normal operation only 2 files will be kept, however if the log server is unreachable, the number of files specified by retainedBufferFileCountLimit will be kept on the file system. For unlimited retention, pass null. Default value is 31.

  • logEventLimitBytes - The maximum size, in bytes, for a serialized representation of a log event. Log events exceeding this size will be dropped. Specify null for no limit. Default value is null.

  • logEventsInBatchLimit - The maximum number of log events sent as a single batch over the network. Default value is 1000.

  • batchSizeLimitBytes - The approximate maximum size, in bytes, for a single batch. The value is an approximation because only the size of the log events are considered. The extra characters added by the batch formatter, where the sequence of serialized log events are transformed into a payload, are not considered. Please make sure to accommodate for those.

    Another thing to mention is that although the sink does its best to optimize for this limit, if you decide to use an implementation of IHttpClient that is compressing the payload, e.g. JsonGzipHttpClient, this parameter describes the uncompressed size of the log events. The compressed size might be significantly smaller depending on the compression algorithm and the repetitiveness of the log events.

    Default value is null.

  • period - The time to wait between checking for event batches. Default value is 2 seconds.

  • textFormatter - The formatter rendering individual log events into text, for example JSON. Default value is NormalRenderedTextFormatter.

  • batchFormatter - The formatter batching multiple log events into a payload that can be sent over the network. Default value is ArrayBatchFormatter.

  • restrictedToMinimumLevel - The minimum level for events passed through the sink. Default value is LevelAlias.Minimum.

  • httpClient - A custom IHttpClient implementation. Default value is JsonHttpClient.

  • configuration - Configuration passed to httpClient. Parameter is either manually specified when configuring the sink in source code or automatically passed in when configuring the sink using Serilog.Settings.Configuration.