Serilog.Sinks.SlackMessage
1.0.0
dotnet add package Serilog.Sinks.SlackMessage --version 1.0.0
NuGet\Install-Package Serilog.Sinks.SlackMessage -Version 1.0.0
<PackageReference Include="Serilog.Sinks.SlackMessage" Version="1.0.0" />
paket add Serilog.Sinks.SlackMessage --version 1.0.0
#r "nuget: Serilog.Sinks.SlackMessage, 1.0.0"
// Install Serilog.Sinks.SlackMessage as a Cake Addin
#addin nuget:?package=Serilog.Sinks.SlackMessage&version=1.0.0
// Install Serilog.Sinks.SlackMessage as a Cake Tool
#tool nuget:?package=Serilog.Sinks.SlackMessage&version=1.0.0
Serilog.Sinks.SlackMessage
Writes Serilog events to the Slack using bot api
Geting started
Create a Slack Bot and give it at least two scopes: chat:write and files:write. Than add this bot to the channel where log events will be written.
Register the sink in your C# code
Log.Logger = new LoggerConfiguration()
.WriteTo.Slack(
"Enter your token here",
new SlackLogOptions
{
Channel = "Enter your slack channel here",
Mentions = new Dictionary<LogEventLevel, SlackLogMention>()
{
{
LogEventLevel.Error,
new SlackLogMention
{
Users = new[] { "slack_username" },
Channel = SlackMention.Here,
}
},
},
MaxMessageLineCount = 5,
SendExceptionAsFile = false,
},
LogEventLevel.Debug
)
.CreateLogger();
You can also configure the sink in appsettings.json.
{
"Serilog": {
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "Slack",
"Args": {
"Token": "Enter your token here",
"MinimumLevel": "Debug",
"Options": {
"Channel": "Enter your channel name here",
"SendExceptionAsFile": false,
"MaxMessageLineCount": 10,
"Mentions": {
"Debug": {
"Users": ["some_user_name"],
"Channel": "Here"
}
}
}
}
}
]
}
}
To do this, install the Serilog.Settings.Configuration package and register the sink in your C# code
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
It is important to register SelfLog before the sink registration. Otherwise you will not see errors occurred in the sink
For example:
SelfLog.Enable(Console.WriteLine);
Samples
Set the MaxMessageLineCount
option to null
if you want to receive all event information in one message.
Log.Logger = new LoggerConfiguration()
.WriteTo.Slack(
"<TOKEN>",
new SlackLogOptions { Channel = "<CHANNEL>", MaxMessageLineCount = null }
)
.CreateLogger();
Log.Information(longMessage);
But it's better to set a value between 5 to 10. Long message will be cutted and full message will be sent to the thread.
Log.Logger = new LoggerConfiguration()
.WriteTo.Slack(
"<TOKEN>",
new SlackLogOptions { Channel = "<CHANNEL>", MaxMessageLineCount = 5 }
)
.CreateLogger();
You can notify usergroup, channel or specific users. Just set the Mentions
option
Log.Logger = new LoggerConfiguration()
.WriteTo.Slack(
"<TOKEN>",
new SlackLogOptions
{
Channel = "<CHANNEL>",
Mentions = new Dictionary<LogEventLevel, SlackLogMention>()
{
{ LogEventLevel.Error, new SlackLogMention { Channel = SlackMention.Here } },
{ LogEventLevel.Fatal, new SlackLogMention { Channel = SlackMention.Channel } },
{ LogEventLevel.Debug, new SlackLogMention { Users = new[] { "<USERNAME>" }, UserGroups = new[] { "<USER_GROUP1>" }} },
{ LogEventLevel.Warning, new SlackLogMention { UserGroups = new[] { "<USER_GROUP2>" }} },
}
}
)
.CreateLogger();
If an event contains exception, the sink always send exception to a thread. Set the SendExceptionAsFile
option to false
to receive exception in message body.
If you set the SendExceptionAsFile
option to true
, an exception will be sent as a text file (which looks prettier in our opinion)
Log.Logger = new LoggerConfiguration()
.WriteTo.Slack(
"<TOKEN>",
new SlackLogOptions { Channel = "<CHANNEL>", SendExceptionAsFile = true }
)
.CreateLogger();
Product | Versions 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. |
-
.NETStandard 2.0
- Serilog (>= 2.0.0)
- Serilog.Sinks.PeriodicBatching (>= 2.2.0)
- SlackBot.Api (>= 1.0.0)
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.0 | 13,835 | 11/6/2020 |