forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlackLogFactory.cs
More file actions
149 lines (130 loc) · 6.13 KB
/
SlackLogFactory.cs
File metadata and controls
149 lines (130 loc) · 6.13 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
using System;
using ServiceStack.Configuration;
namespace ServiceStack.Logging.Slack
{
public class SlackLogFactory : ILogFactory
{
public bool DebugEnabled { get; set; }
private readonly string incomingWebHookUrl;
/// <summary>
/// Slack Incoming Webhook URL.
/// </summary>
public string Url
{
get
{
return incomingWebHookUrl;
}
}
/// <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="DebugEnabled" /> 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 ConfigKeyFmt = "ServiceStack.Logging.Slack.{0}";
/// <summary>
/// Initializes a new instance of the <see cref="SlackLogFactory"/> class.
/// </summary>
/// <param name="incomingWebHookUrl">Private URL create by Slack when setting up an Incoming WebHook integration.</param>
/// <param name="debugEnabled">By default, Debug logs are not posted to Slack.</param>
public SlackLogFactory(string incomingWebHookUrl, bool debugEnabled = false)
{
DebugEnabled = debugEnabled;
this.incomingWebHookUrl = incomingWebHookUrl;
}
/// <summary>
/// Initializes a new instance of the <see cref="SlackLogFactory"/> class.
/// Configuration from IAppSettings looking at keys with prefix "ServiceStack.Logging.Slack.".
/// Eg, <add key="ServiceStack.Logging.Slack.DefaultChannel" value="LoggingChannel" />
/// </summary>
/// <param name="incomingWebHookUrl"></param>
/// <param name="appSettings">Further config provided by an IAppSettings instance.</param>
public SlackLogFactory(string incomingWebHookUrl, IAppSettings appSettings)
: this(appSettings)
{
this.incomingWebHookUrl = incomingWebHookUrl;
}
/// <summary>
/// Configuration from IAppSettings looking at keys with prefix "ServiceStack.Logging.Slack.".
/// Eg, <add key="ServiceStack.Logging.Slack.Url" value="{SlackIncomingWebhookUrl}" />
/// </summary>
/// <param name="appSettings"></param>
public SlackLogFactory(IAppSettings appSettings)
{
if (appSettings == null)
throw new ArgumentNullException("appSettings");
if(incomingWebHookUrl == null)
incomingWebHookUrl = appSettings.GetString(ConfigKeyFmt.Fmt("Url"));
DebugEnabled = appSettings.Get<bool>(ConfigKeyFmt.Fmt("DebugEnabled"));
DefaultChannel = appSettings.GetString(ConfigKeyFmt.Fmt("DefaultChannel"));
IconEmoji = appSettings.GetString(ConfigKeyFmt.Fmt("IconEmoji"));
ErrorChannel = appSettings.GetString(ConfigKeyFmt.Fmt("ErrorChannel"));
DebugChannel = appSettings.GetString(ConfigKeyFmt.Fmt("DebugChannel"));
InfoChannel = appSettings.GetString(ConfigKeyFmt.Fmt("InfoChannel"));
FatalChannel = appSettings.GetString(ConfigKeyFmt.Fmt("FatalChannel"));
WarnChannel = appSettings.GetString(ConfigKeyFmt.Fmt("WarnChannel"));
BotUsername = appSettings.GetString(ConfigKeyFmt.Fmt("BotUsername"));
ChannelPrefix = appSettings.GetString(ConfigKeyFmt.Fmt("ChannelPrefix"));
}
public ILog GetLogger(Type type)
{
return GetLogger(type.ToString());
}
public ILog GetLogger(string typeName)
{
return new SlackLog(incomingWebHookUrl, DebugEnabled)
{
DebugChannel = DebugChannel,
DefaultChannel = DefaultChannel,
IconEmoji = IconEmoji,
InfoChannel = InfoChannel,
FatalChannel = FatalChannel,
WarnChannel = WarnChannel,
ErrorChannel = ErrorChannel,
BotUsername = BotUsername,
ChannelPrefix = ChannelPrefix
};
}
}
}