Skip to content

An implementation of ILoggerProvider, from the Microsoft.Extensions.Logging.Abstractions framework, for writing log events to a Seq server.

License

Notifications You must be signed in to change notification settings

JakenVeina/SeqLoggerProvider

Repository files navigation

SeqLoggerProvider

License Continuous Deployment NuGet

An implementation of ILoggerProvider, from the .NET Extensions Logging Framework framework, for writing log entries to a Seq server.

This library provides an alternative to the official Seq logger provider, with a goal of providing a slimmer and more extensible implementation. This library includes the following key features that separate it from the official one:

  • Includes only first-party and .NET dependencies, including... -- System.Text.Json, -- System.Threading.Channels, -- Microsoft.Extensions.Http, -- Microsoft.Extensions.Configuration -- Microsoft.Extensions.DependencyInjection
  • Allows for global-scope log data, I.E. data fields that are included upon every log event sent to the server.
  • Allows for more granular control of event batching, including both size-based and time-based thresholds, and flood control
  • Provides extension points for JSON serialization, based on System.Text.Json, allowing for consumers to write custom serializers for log entries that can optimize for performance or data size.
  • Provides extension points for HTTP transmission, allowing for consumers to apply any custom configurations to the HttpClient instances that are used to deliver log entries to the Seq server.

Usage

Basic Setup

To setup the logger provider within a .NET application, simply call the setup method, while setting up your logging system. Either upon your IHostBuilder...

.ConfigureLogging(builder => builder
    .AddSeq())

...or upon your IServiceCollection...

.AddLogging(builder => builder
    .AddSeq());

Configuration

Configuration is automatically extracted from the ambient IConfiguration system, if present, in the same fashion as all first-party logger providers. For example, if you're using an appsettings.json file...

{
  "Logging": {
    "Seq": {
      "ServerUrl": "http://localhost:5341/"
      "ApiKey": "...",
      "GlobalScopeState": {
        "Application": "SeqLogger.Test"
      },
      "LogLevel": {
        "Default": "Debug",
        "SeqLoggerProvider": "None"
      }
    }
  }
}

If you would like to customize the configuration manually, the .AddSeq() method supports a standard configuration delegate being passed in...

builder.AddSeq(options =>
{
	var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
	if (assemblyVersion is not null)
        options.GlobalScopeState.Add("Version", assemblyVersion.ToString();
});

All configuration properties are optional, except for ServerUrl, for obvious reasons.

JSON Customization

In order to customize JSON serialization behavior, simply supply an options configuration delegate, when adding the provider.

builder.AddSeq(configureJsonSerializer: options =>
{
    options.Converters.Add(new MyJsonConverter());
    options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

HTTP Customization

In order to customize HTTP transmission behavior, simply use the configureHttpClient parameter supported by the .AddSeq() method:

builder.AddSeq(configureHttpClient: builder => builder
    .RedactLoggedHeaders(new[] { SeqLoggerConstants.ApiKeyHeaderName }));

About

An implementation of ILoggerProvider, from the Microsoft.Extensions.Logging.Abstractions framework, for writing log events to a Seq server.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages