-
Notifications
You must be signed in to change notification settings - Fork 15
/
SlackSink.cs
246 lines (213 loc) · 10.1 KB
/
SlackSink.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2013 Serilog Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
namespace Serilog.Sinks.Slack
{
/// <summary>
/// Writes log events as messages to Slack Channels.
/// </summary>
public class SlackSink : ILogEventSink
{
/// <summary>
/// The Slack channels collection.
/// </summary>
protected readonly SlackChannelCollection Channels = new SlackChannelCollection();
/// <summary>
/// The <see cref="IFormatProvider"/> used to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.
/// </summary>
protected readonly IFormatProvider FormatProvider;
/// <summary>
/// The Slack bot name.
/// </summary>
private readonly string _username;
/// <summary>
/// URL to an image to use as the icon for this message.
/// </summary>
private readonly string _iconUrl;
/// <summary>
/// Construct a sink posting to the specified Slack Channel.
/// </summary>
/// <param name="channelId">Slack Channel Id.</param>
/// <param name="token">Token that allows Slack authentication. To manage tokens go to https://api.slack.com/tokens.</param>
/// <param name="formatProvider">FormatProvider to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.</param>
/// <param name="username">Optional bot name</param>
/// <param name="iconUrl">Optional URL to an image to use as the icon for this message.</param>
public SlackSink(string channelId, string token,
IFormatProvider formatProvider, string username = null, string iconUrl = null)
{
if (string.IsNullOrWhiteSpace(channelId))
throw new ArgumentNullException(nameof(channelId));
if (string.IsNullOrWhiteSpace(token))
throw new ArgumentNullException(nameof(token));
FormatProvider = formatProvider;
Channels.Add(new SlackChannel(channelId, token));
_username = username;
_iconUrl = iconUrl;
}
/// <summary>
/// Construct a sink posting to the specified Slack Channel.
/// </summary>
/// <param name="channels">Slack Channel list.</param>
/// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook. By default uses <see cref="RenderMessage"/>.</param>
/// <param name="formatProvider">FormatProvider to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.</param>
/// <param name="username">Optional bot name</param>
/// <param name="iconUrl">Optional URL to an image to use as the icon for this message.</param>
public SlackSink(SlackChannelCollection channels,
SlackSink.RenderMessageMethod renderMessageImplementation,
IFormatProvider formatProvider, string username = null, string iconUrl = null)
{
if (channels == null)
throw new ArgumentNullException(nameof(channels));
FormatProvider = formatProvider;
Channels = channels;
RenderMessageImplementation = renderMessageImplementation ?? RenderMessage;
_username = username;
_iconUrl = iconUrl;
if (Channels.Count == 0)
SelfLog.WriteLine("There are 0 Slack channels defined. Slack sink will not send messages.");
}
/// <summary>
/// Construct a sink posting to the specified Slack Channel.
/// </summary>
/// <param name="webhookUri">WebHook Uri that allows Slack Incoming Webhooks (https://api.slack.com/incoming-webhooks).</param>
/// <param name="renderMessageImplementation">Optional delegate to build json to send to slack webhook. By default uses <see cref="RenderMessage"/>.</param>
/// <param name="formatProvider">FormatProvider to apply to <see cref="LogEvent.RenderMessage(IFormatProvider)"/>.</param>
public SlackSink(string webhookUri,
SlackSink.RenderMessageMethod renderMessageImplementation,
IFormatProvider formatProvider)
{
if (string.IsNullOrWhiteSpace(webhookUri))
throw new ArgumentNullException("webhookUri");
FormatProvider = formatProvider;
Channels.Add(new SlackChannel(webhookUri));
RenderMessageImplementation = renderMessageImplementation ?? RenderMessage;
if (Channels.Count == 0)
SelfLog.WriteLine("There are 0 Slack channels defined. Slack sink will not send messages.");
}
/// <summary>
/// Delegate to allow overriding of the RenderMessage method.
/// </summary>
public delegate string RenderMessageMethod(LogEvent input);
/// <summary>
/// RenderMessage method that will transform LogEvent into a Slack message.
/// </summary>
protected RenderMessageMethod RenderMessageImplementation = RenderMessage;
#region ILogEventSink implementation
public void Emit(LogEvent logEvent)
{
foreach (var item in Channels)
{
// FormatProvider overrides default behaviour
var message = (FormatProvider != null) ? logEvent.RenderMessage(FormatProvider) : RenderMessageImplementation(logEvent);
if (item.UsesWebhooks)
{
SendMessageWithWebHooks(item.WebHookUri, message);
}
else
{
SendMessageWithChannelIdAndToken(item.Token, item.ChannelId, message);
}
}
}
#endregion
protected static string RenderMessage(LogEvent logEvent)
{
dynamic body = new ExpandoObject();
body.text = logEvent.RenderMessage();
body.attachments = WrapInAttachment(logEvent).ToArray();
return Newtonsoft.Json.JsonConvert.SerializeObject(body);
}
protected void SendMessageWithChannelIdAndToken(string token, string channelId, string message)
{
SelfLog.WriteLine("Trying to send message to channelId '{0}' with token '{1}': '{2}'.", channelId, token, message);
var sendMessageResult = SlackClient.SlackClient.SendMessage(token, channelId, message, _username, _iconUrl);
if (sendMessageResult != null)
{
SelfLog.WriteLine("Message sent to channelId '{0}' with token '{1}': '{2}'.", channelId, token, sendMessageResult.JsonValue.ToString());
}
}
protected void SendMessageWithWebHooks(string webhookUri, string message)
{
SelfLog.WriteLine("Trying to send message to webhook '{0}': '{1}'.", webhookUri, message);
if (message != null)
{
var sendMessageResult = SlackClient.SlackClient.SendMessageViaWebhooks(webhookUri, message);
if (sendMessageResult != null)
{
SelfLog.WriteLine("Message sent to webhook '{0}': '{1}'.", webhookUri, sendMessageResult);
}
}
}
protected static string GetAttachmentColor(LogEventLevel level)
{
switch (level)
{
case LogEventLevel.Information:
return "#5bc0de";
case LogEventLevel.Warning:
return "#f0ad4e";
case LogEventLevel.Error:
case LogEventLevel.Fatal:
return "#d9534f";
default:
return "#777";
}
}
protected static object CreateAttachmentField(string title, string value, bool @short = true)
{
return new { title, value, @short };
}
protected static object WrapInAttachment(Exception ex)
{
return new
{
title = "Exception",
fallback = string.Format("Exception: {0} \n {1}", ex.Message, ex.StackTrace),
color = GetAttachmentColor(LogEventLevel.Fatal),
fields = new[]
{
CreateAttachmentField("Message", ex.Message),
CreateAttachmentField("Type", "`"+ex.GetType().Name+"`"),
CreateAttachmentField("Stack Trace", "```"+ex.StackTrace+"```", false)
},
mrkdwn_in = new[] { "fields" }
};
}
protected static IEnumerable<dynamic> WrapInAttachment(LogEvent log)
{
var result = new List<dynamic>
{
new
{
fallback = string.Format("[{0}]{1}", log.Level, log.RenderMessage()),
color = GetAttachmentColor(log.Level),
fields = new[]
{
CreateAttachmentField("Level", log.Level.ToString()),
CreateAttachmentField("Timestamp", log.Timestamp.ToString())
}
}
};
if (log.Exception != null)
result.Add(WrapInAttachment(log.Exception));
return result;
}
}
}