Serilog.Sinks.GraylogGelf 1.0.6

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

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

Serilog.Sinks.GraylogGelf

Introduction

This projects aims to make Graylog a comfortable logging sink for Serilog The project and optional dependencies are all .NetStandard 2.0 libraries, so cross platform usage over .Net platforms is ensured. More information about Graylog or Serilog.

Features

  • Support for Graylog Extended Log Format (GELF) 1.1
  • Suport configuration via code or appsettings.json
  • Support user defined fiedls

Supported Graylog transports

  • UDP (with optional GZIP compression using SharpCompress)
  • TCP without encryption and optional Null-Byte delimiter
  • TCP with TLS and optional Null-Byte delimiter
  • HTTP without encryption

All transports can be used synchoniously and asynchroniouly.

Usage

Samples are only for your understandig of base usase you must not use exact same code into your application.

Configuration via code

In order to create a new sink one has to create an instance of class GraylogSinkConfiguration first. Then you could use the fluent syntax and just call Graylog() extension method with the prepared config instance.

The following snippet will create a new sink:

var graylogConfig = new GraylogSinkConfiguration
{
    TransportType = GraylogTransportType.Tcp,
    Host = "example.graylog.local",
    Port = 12201,
    UseSecureConnection = true,
    UseAsyncLogging = true
};
using(var log = new LoggerConfiguration()
    .WriteTo.Graylog(graylogConfig)
    .CreateLogger())
{
    log.Information("testing TLS secured {connectionType}", graylogConfig.TransportType);
}

Configuration via appsettings.json

If you need variable configuration settings, you could use the Serilog.Settings.Configuration package and read confuguration from appsettings.json file. As appsettings.json is a JSON external file, you could modify it in any time. You can change values into pipeline or after deplotment without recompiling your application.

Sample for .NET Core console application:

var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory()) //  Microsoft.Extensions.Configuration.FileExtensions
                .AddJsonFile("appsettings.json") // Microsoft.Extensions.Configuration.Json
                .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
                .Build();
Log.Logger = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration)
                .CreateLogger()

appsettings.json:

{
  "Serilog": {
	"Using": [ "Serilog.Sinks.GraylogGelf" ],
	"MinimumLevel": "Debug",
	"WriteTo": [
	  {
		"Name": "Graylog",
		"Args": {
		  "hostnameOrAddress": "example.graylog.local",
		  "port": 12201,
		  "transportType": "Tcp",
		  "useAsyncLogging": true
		}
	  }
	]
  }
}

How to add user defined fields for every log item:

{
  "Serilog": {
	"Using": [ "Serilog.Sinks.GraylogGelf" ],
	"MinimumLevel": "Debug",
	"WriteTo": [
	  {
		"Name": "Graylog",
		"Args": {
		  "hostnameOrAddress": "example.graylog.local",
		  "port": 12201,
		  "transportType": "Tcp",
		  "useAsyncLogging": true,
		  "additionalFields": {
			"application": "MyApplication",
			"environment": "Production"
		  }
		}
	  }
	]
  }
}

Further examples can be found in the sample projects.

Possible json settings

For compatibility to different sinks project we using not the same name for code settings and json settings. Possible we could change it as users wants.

Name Description Sample
hostnameOrAddress Graylog server hostname or IP address example.graylog.local
port Graylog server port 12201
transportType Transport type Tcp, Udp, Http
useAsyncLogging Use async logging true, false
useSecureConnectionInHttp Use TLS for TCP transport true, false
retryCount Number of retries for TCP transport 5
retryIntervalMs Interval between retries for TCP transport 150
useGzipCompression Use GZIP compression for UDP transport true, false
useNullDelimiter Use Null-Byte delimiter for TCP transport true, false
additionalFields User defined fields { "application": "MyApplication" }
minimumLogEventLevel Minimum log event level Debug, Information, Warning, Error, Fatal
propertyPrefix Prefix for all fields MyVar_
maxMessageSizeInUdp Maximum message size for UDP transport 8192
minUdpMessageSizeForCompression Minimum message size for UDP compression 512

FAQ

    1. What is the differencess to original project?
    2. Add possibility to read configuration from appsettings.json. Correct some small issues.

Versions

V1.0.5 - Fix TCP reconnection for each message. Introduce 10 seconds reconnect interval
V1.0.0 - Initial release

License

This project is licensed under Apache 2.0 license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 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 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 2,238 8/17/2023
1.0.4 236 7/23/2023
1.0.3 181 7/23/2023
1.0.1 201 7/23/2023