Skip to content

A simple (yet customizable) Slack logging sink for Serilog

License

Notifications You must be signed in to change notification settings

serilog-contrib/serilog-sinks-slack

Repository files navigation

serilog-sinks-slack

Simple and beautiful Serilog Slack sink :)

NuGet

Install-Package Serilog.Sinks.Slack

Sample

Sample

Usage

Minimal (using default WebHook integration settings)

Log.Logger = new LoggerConfiguration()
    .WriteTo.Slack("https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    .CreateLogger();

Custom channel, username or icon:

Log.Logger = new LoggerConfiguration()
    .WriteTo.Slack("https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", 20, TimeSpan.FromSeconds(10), "#general", "Slack Logger", ":ghost:", queueLimit: 1000)
    .CreateLogger();

Advanced:

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Slack(new SlackSinkOptions
    {
        WebHookUrl = "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        BatchSizeLimit = 20,
        QueueLimit = 1000,
        CustomUserName = "Slack Logger",
        CustomIcon = ":ghost:",
        Period = TimeSpan.FromSeconds(10),
        ShowDefaultAttachments = false,
        ShowExceptionAttachments = true,
        MinimumLogEventLevel = LogEventLevel.Warning,
        PropertyDenyList = new List<string> { "Level", "SourceContext" }
    })
    .CreateLogger();

It's possible to override CustomChannel, CustomUserName, CustomIcon configs using LogEvent properties. Config overrides can be enabled by specifying them in SlackSinkOptions property PropertyOverrideList.

new SlackSinkOptions
{
    WebHookUrl = "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    CustomChannel = "#static_channel_name",
    CustomUserName = "User Foo",
    MinimumLogEventLevel = LogEventLevel.Fatal,
    PropertyOverrideList = new List<OverridableProperties>() { OverridableProperties.CustomChannel }
});

Config override using Scope:

using (_logger.BeginScope("{CustomChannel}", dynamicChannelName))
{
    _logger.LogCritical(exception, message, args);
}