-
Notifications
You must be signed in to change notification settings - Fork 47
/
SplunkLoggingConfigurationExtensions.cs
224 lines (208 loc) · 11.5 KB
/
SplunkLoggingConfigurationExtensions.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
// Copyright 2018 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 Serilog.Configuration;
using Serilog.Core;
using Serilog.Events;
using Serilog.Formatting;
using Serilog.Sinks.Splunk;
using System;
using System.Net.Http;
namespace Serilog
{
/// <summary>
/// Fluent configuration methods for Logger configuration
/// </summary>
public static class SplunkLoggingConfigurationExtensions
{
/// <summary>
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
/// </summary>
/// <param name="configuration">The logger config</param>
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If true, the message template will be rendered</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
/// <param name="batchSizeLimit">The size of the batch</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
/// <returns></returns>
public static LoggerConfiguration EventCollector(
this LoggerSinkConfiguration configuration,
string splunkHost,
string eventCollectorToken,
string uriPath = ConfigurationDefaults.DefaultEventCollectorPath,
string source = ConfigurationDefaults.DefaultSource,
string sourceType = ConfigurationDefaults.DefaultSourceType,
string host = ConfigurationDefaults.DefaultHost,
string index = ConfigurationDefaults.DefaultIndex,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
bool renderMessage = true,
int batchIntervalInSeconds = 2,
int batchSizeLimit = 100,
int? queueLimit = null,
HttpMessageHandler messageHandler = null,
LoggingLevelSwitch levelSwitch = null,
SubSecondPrecision subSecondPrecision = SubSecondPrecision.Milliseconds)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var batchingOptions = new BatchingOptions
{
BatchSizeLimit = batchSizeLimit,
BufferingTimeLimit = TimeSpan.FromSeconds(batchIntervalInSeconds),
EagerlyEmitFirstEvent = true,
QueueLimit = queueLimit
};
var eventCollectorSink = new EventCollectorSink(
splunkHost,
eventCollectorToken,
uriPath,
source,
sourceType,
host,
index,
formatProvider,
renderTemplate,
renderMessage,
messageHandler: messageHandler,
subSecondPrecision: subSecondPrecision);
return configuration.Sink(eventCollectorSink, batchingOptions, restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
/// </summary>
/// <param name="configuration">The logger config</param>
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
/// <param name="jsonFormatter">The text formatter used to render log events into a JSON format for consumption by Splunk</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
/// <param name="batchSizeLimit">The size of the batch</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <returns></returns>
public static LoggerConfiguration EventCollector(
this LoggerSinkConfiguration configuration,
string splunkHost,
string eventCollectorToken,
ITextFormatter jsonFormatter,
string uriPath = ConfigurationDefaults.DefaultEventCollectorPath,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
int batchIntervalInSeconds = 2,
int batchSizeLimit = 100,
int? queueLimit = null,
HttpMessageHandler messageHandler = null,
LoggingLevelSwitch levelSwitch = null)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
if (jsonFormatter == null) throw new ArgumentNullException(nameof(jsonFormatter));
var batchingOptions = new BatchingOptions
{
BatchSizeLimit = batchSizeLimit,
BufferingTimeLimit = TimeSpan.FromSeconds(batchIntervalInSeconds),
EagerlyEmitFirstEvent = true,
QueueLimit = queueLimit
};
var eventCollectorSink = new EventCollectorSink(
splunkHost,
eventCollectorToken,
uriPath,
jsonFormatter,
messageHandler);
return configuration.Sink(eventCollectorSink, batchingOptions, restrictedToMinimumLevel, levelSwitch);
}
/// <summary>
/// Adds a sink that writes log events as to a Splunk instance via the HTTP Event Collector.
/// </summary>
/// <param name="configuration">The logger config</param>
/// <param name="splunkHost">The Splunk host that is configured with an Event Collector</param>
/// <param name="eventCollectorToken">The token provided to authenticate to the Splunk Event Collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="source">The source of the event</param>
/// <param name="sourceType">The source type of the event</param>
/// <param name="host">The host of the event</param>
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that the queue should be instpected for batching</param>
/// <param name="batchSizeLimit">The size of the batch</param>
/// <param name="queueLimit">Maximum number of events in the queue</param>
/// <param name="messageHandler">The handler used to send HTTP requests</param>
/// <param name="levelSwitch">A switch allowing the pass-through minimum level to be changed at runtime.</param>
/// <param name="fields">Customfields that will be indexed in splunk with this event</param>
/// <param name="renderMessage">Include "RenderedMessage" parameter in output JSON message.</param>
/// <param name="subSecondPrecision">Timestamp sub-second precision. Splunk props.conf setup is required.</param>
/// <returns></returns>
public static LoggerConfiguration EventCollector(
this LoggerSinkConfiguration configuration,
string splunkHost,
string eventCollectorToken,
CustomFields fields,
string uriPath = ConfigurationDefaults.DefaultEventCollectorPath,
string source = ConfigurationDefaults.DefaultSource,
string sourceType = ConfigurationDefaults.DefaultSourceType,
string host = ConfigurationDefaults.DefaultHost,
string index = ConfigurationDefaults.DefaultIndex,
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
bool renderMessage = true,
int batchIntervalInSeconds = 2,
int batchSizeLimit = 100,
int? queueLimit = null,
HttpMessageHandler messageHandler = null,
LoggingLevelSwitch levelSwitch = null,
SubSecondPrecision subSecondPrecision = SubSecondPrecision.Milliseconds)
{
if (configuration == null) throw new ArgumentNullException(nameof(configuration));
var batchingOptions = new BatchingOptions
{
BatchSizeLimit = batchSizeLimit,
BufferingTimeLimit = TimeSpan.FromSeconds(batchIntervalInSeconds),
EagerlyEmitFirstEvent = true,
QueueLimit = queueLimit
};
var eventCollectorSink = new EventCollectorSink(
splunkHost,
eventCollectorToken,
uriPath,
source,
sourceType,
host,
index,
fields,
formatProvider,
renderTemplate,
renderMessage,
messageHandler,
subSecondPrecision: subSecondPrecision
);
return configuration.Sink(eventCollectorSink, batchingOptions, restrictedToMinimumLevel, levelSwitch);
}
}
}