forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlackLog.cs
More file actions
248 lines (207 loc) · 8.16 KB
/
Copy pathSlackLog.cs
File metadata and controls
248 lines (207 loc) · 8.16 KB
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
247
248
using System;
using System.Text;
using ServiceStack.Text;
namespace ServiceStack.Logging.Slack
{
public class SlackLog : ILogWithException
{
private readonly string incomingWebHookUrl;
private readonly bool debugEnabled;
/// <summary>
/// Default channel override.
/// This will override the channel name specified when the Incoming Webhook was created.
/// </summary>
public string DefaultChannel { get; set; }
/// <summary>
/// Icon emoji override for the bot. Eg, ":ghost:".
/// </summary>
public string IconEmoji { get; set; }
/// <summary>
/// Channel override specifically for logging errors.
/// Falls back to <see cref="DefaultChannel"/> if not specified.
/// If ErrorChannel and DefaultChannel null, falls back to Slack configued default channel.
/// </summary>
public string ErrorChannel { get; set; }
/// <summary>
/// Channel override specifically for debug logging.
/// Debug logging is only posted in <see cref="IsDebugEnabled" /> is true.
/// Falls back to <see cref="DefaultChannel"/> if not specified.
/// If DebugChannel and DefaultChannel null, falls back to Slack configued default channel.
/// </summary>
public string DebugChannel { get; set; }
/// <summary>
/// Channel override specifically for info logging.
/// Falls back to <see cref="DefaultChannel"/> if not specified.
/// If InfoChannel and DefaultChannel null, falls back to Slack configued default channel.
/// </summary>
public string InfoChannel { get; set; }
/// <summary>
/// Channel override specifically for fatal error logging.
/// Falls back to <see cref="DefaultChannel"/> if not specified.
/// If FatalChannel and DefaultChannel null, falls back to Slack configued default channel.
/// </summary>
public string FatalChannel { get; set; }
/// <summary>
/// Channel override specifically for fatal warning logging.
/// Falls back to <see cref="DefaultChannel"/> if not specified.
/// If WarnChannel and DefaultChannel null, falls back to Slack configued default channel.
/// </summary>
public string WarnChannel { get; set; }
/// <summary>
/// Bot username override.
/// Falls back to Slack configured default if not specified.
/// </summary>
public string BotUsername { get; set; }
public string ChannelPrefix { get; set; }
private const string NewLine = "\n";
public SlackLog(string incomingWebHookUrl, bool debugEnabled = false)
{
this.incomingWebHookUrl = incomingWebHookUrl;
this.debugEnabled = debugEnabled;
// Init from DefaultChannel
ErrorChannel = DefaultChannel;
DebugChannel = DefaultChannel;
InfoChannel = DefaultChannel;
FatalChannel = DefaultChannel;
WarnChannel = DefaultChannel;
}
private void LogMessage(SlackLoggingData message)
{
using (JsConfig.With(propertyConvention: PropertyConvention.Lenient,
emitLowercaseUnderscoreNames: true,
emitCamelCaseNames: false))
{
incomingWebHookUrl.PostJsonToUrlAsync(message);
}
}
private SlackLoggingData BuildMessage(string text, string channel = null)
{
string finalChannel;
if (!string.IsNullOrEmpty(ChannelPrefix) && (channel != null || DefaultChannel != null))
finalChannel = ChannelPrefix + (channel ?? DefaultChannel);
else
finalChannel = channel ?? DefaultChannel;
return new SlackLoggingData
{
Channel = finalChannel,
IconEmoji = IconEmoji,
Text = text,
Username = BotUsername
};
}
private void Write(object message, Exception execption, string channel = null)
{
StringBuilder sb = new StringBuilder();
sb.Append(message);
if (execption != null)
sb.Append(NewLine);
while (execption != null)
{
sb.Append("Message: ").Append(execption.Message).Append(NewLine)
.Append("Source: ").Append(execption.Source).Append(NewLine)
.Append("Target site: ").Append(execption.TargetSite).Append(NewLine)
.Append("Stack trace: ").Append(execption.StackTrace).Append(NewLine);
// Walk the InnerException tree
execption = execption.InnerException;
}
var slackMessage = BuildMessage(sb.ToString(), channel);
LogMessage(slackMessage);
}
public void Debug(object message)
{
if (!debugEnabled)
return;
Write(message, null, DebugChannel);
}
public void Debug(object message, Exception exception)
{
if (!debugEnabled)
return;
Write(message, exception, DebugChannel);
}
public void DebugFormat(string format, params object[] args)
{
if (!debugEnabled)
return;
Write(string.Format(format, args), null, DebugChannel);
}
public void Debug(Exception exception, string format, params object[] args)
{
if (!debugEnabled)
return;
Write(string.Format(format, args), exception, DebugChannel);
}
public void Error(object message)
{
Write(message, null, ErrorChannel);
}
public void Error(object message, Exception exception)
{
Write(message, exception, ErrorChannel);
}
public void ErrorFormat(string format, params object[] args)
{
Write(string.Format(format, args), null, ErrorChannel);
}
public void Error(Exception exception, string format, params object[] args)
{
Write(string.Format(format, args), exception, ErrorChannel);
}
public void Fatal(object message)
{
Write(message, null, FatalChannel);
}
public void Fatal(object message, Exception exception)
{
Write(message, exception, FatalChannel);
}
public void FatalFormat(string format, params object[] args)
{
Write(string.Format(format, args), null, FatalChannel);
}
public void Fatal(Exception exception, string format, params object[] args)
{
Write(string.Format(format, args), exception, FatalChannel);
}
public void Info(object message)
{
Write(message, null, InfoChannel);
}
public void Info(object message, Exception exception)
{
Write(message, exception, InfoChannel);
}
public void InfoFormat(string format, params object[] args)
{
Write(string.Format(format, args), null, InfoChannel);
}
public void Info(Exception exception, string format, params object[] args)
{
Write(string.Format(format, args), exception, InfoChannel);
}
public void Warn(object message)
{
Write(message, null, WarnChannel);
}
public void Warn(object message, Exception exception)
{
Write(message, exception, WarnChannel);
}
public void WarnFormat(string format, params object[] args)
{
Write(string.Format(format, args), null, WarnChannel);
}
public void Warn(Exception exception, string format, params object[] args)
{
Write(string.Format(format, args), exception, WarnChannel);
}
public bool IsDebugEnabled => debugEnabled;
}
class SlackLoggingData
{
public string Channel { get; set; }
public string Text { get; set; }
public string Username { get; set; }
public string IconEmoji { get; set; }
}
}