Skip to content

Commit 8cc2046

Browse files
committed
Simple test
1 parent 417f9de commit 8cc2046

File tree

4 files changed

+55
-27
lines changed

4 files changed

+55
-27
lines changed

sample/Serilog.Sinks.Splunk.Sample/Program.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ static void Main(string[] args)
1010
{
1111
Log.Logger = new LoggerConfiguration()
1212
.WriteTo.LiterateConsole()
13-
.WriteTo.SplunkViaTcp("127.0.0.1", 10001)
13+
.WriteTo.SplunkViaTcp("127.0.0.1", 10001, renderTemplate:true)
1414
.CreateLogger();
1515

16-
Log.Information("Just another test");
16+
var person = new Person() {DateOfBirth = DateTime.Now.AddYears(-30), FirstName = "Joe", Surname = "Bloggs"};
17+
18+
Log.Information("Just another test {@person}", person);
1719

1820
Console.ReadLine();
1921
}
2022
}
23+
24+
internal class Person
25+
{
26+
public string FirstName { get; set; }
27+
public string Surname { get; set; }
28+
public DateTime DateOfBirth { get; set; }
29+
30+
}
31+
2132
}

src/Serilog.Sinks.Splunk.FullNetFx/LoggerConfigurationSplunkExtensions.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public static class LoggerConfigurationSplunkExtensions
3434
/// <param name="batchInterval"></param>
3535
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
3636
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
37-
/// <param name="batchSizeLimit"></param>
37+
/// <param name="batchSizeLimit">The size of the batch prior to writing</param>
38+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
3839
/// <returns>Logger configuration, allowing configuration to continue.</returns>
3940
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
4041
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
@@ -44,9 +45,10 @@ public static LoggerConfiguration SplunkViaHttp(
4445
int batchSizeLimit,
4546
TimeSpan batchInterval,
4647
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
47-
IFormatProvider formatProvider = null)
48+
IFormatProvider formatProvider = null,
49+
bool renderTemplate = true)
4850
{
49-
var sink = new SplunkViaHttpSink(context, batchSizeLimit, batchInterval, formatProvider);
51+
var sink = new SplunkViaHttpSink(context, batchSizeLimit, batchInterval, formatProvider, renderTemplate);
5052

5153
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
5254
}
@@ -56,15 +58,16 @@ public static LoggerConfiguration SplunkViaHttp(
5658
/// </summary>
5759
/// <param name="loggerConfiguration">The logger configuration.</param>
5860
/// <param name="context">The Splunk context to log to</param>
59-
/// <param name="password"></param>
61+
/// <param name="password">The password of the Splunk user</param>
6062
/// <param name="resourceNameSpace"></param>
6163
/// <param name="transmitterArgs"></param>
6264
/// <param name="batchSizeLimit">The size of the batch prior to logging</param>
6365
/// <param name="batchInterval">The interval on which to log via http</param>
6466
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
6567
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
66-
/// <param name="index"></param>
67-
/// <param name="userName"></param>
68+
/// <param name="index">The name of the Splunk index</param>
69+
/// <param name="userName">The name of the Splunk user</param>
70+
/// <param name="renderTemplate">If ture, the message template is rendered</param>
6871
/// <returns>Logger configuration, allowing configuration to continue.</returns>
6972
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
7073
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
@@ -79,7 +82,8 @@ public static LoggerConfiguration SplunkViaHttp(
7982
Namespace resourceNameSpace,
8083
TransmitterArgs transmitterArgs,
8184
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
82-
IFormatProvider formatProvider = null)
85+
IFormatProvider formatProvider = null,
86+
bool renderTemplate = true)
8387
{
8488
var sink = new SplunkViaHttpSink(new SplunkContext(context, index, userName, password, resourceNameSpace, transmitterArgs), batchSizeLimit,batchInterval, formatProvider);
8589

@@ -94,16 +98,18 @@ public static LoggerConfiguration SplunkViaHttp(
9498
/// <param name="port">The UDP port</param>
9599
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
96100
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
101+
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
97102
/// <returns></returns>
98103
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
99104
public static LoggerConfiguration SplunkViaUdp(
100105
this LoggerSinkConfiguration loggerConfiguration,
101106
string host,
102107
int port,
103108
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
104-
IFormatProvider formatProvider = null)
109+
IFormatProvider formatProvider = null,
110+
bool renderTemplate = true)
105111
{
106-
var sink = new SplunkViaUdpSink(host, port, formatProvider);
112+
var sink = new SplunkViaUdpSink(host, port, formatProvider, renderTemplate);
107113

108114
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
109115
}
@@ -117,16 +123,18 @@ public static LoggerConfiguration SplunkViaUdp(
117123
/// <param name="port">The UDP port</param>
118124
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
119125
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
120-
/// <returns></returns>
126+
/// <param name="renderTemplate">If ture, the message template is rendered</param>
127+
/// <returns>The logger configuration</returns>
121128
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
122129
public static LoggerConfiguration SplunkViaUdp(
123130
this LoggerSinkConfiguration loggerConfiguration,
124131
IPAddress hostAddresss,
125132
int port,
126133
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
127-
IFormatProvider formatProvider = null)
134+
IFormatProvider formatProvider = null,
135+
bool renderTemplate = true)
128136
{
129-
var sink = new SplunkViaUdpSink(hostAddresss, port, formatProvider);
137+
var sink = new SplunkViaUdpSink(hostAddresss, port, formatProvider, renderTemplate);
130138

131139
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
132140
}
@@ -139,16 +147,18 @@ public static LoggerConfiguration SplunkViaUdp(
139147
/// <param name="port">The TCP port</param>
140148
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
141149
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
150+
/// <param name="renderTemplate">If true, the message template is rendered</param>
142151
/// <returns></returns>
143152
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
144153
public static LoggerConfiguration SplunkViaTcp(
145154
this LoggerSinkConfiguration loggerConfiguration,
146155
IPAddress hostAddresss,
147156
int port,
148157
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
149-
IFormatProvider formatProvider = null)
158+
IFormatProvider formatProvider = null,
159+
bool renderTemplate = true)
150160
{
151-
var sink = new SplunkViaTcpSink(hostAddresss, port, formatProvider);
161+
var sink = new SplunkViaTcpSink(hostAddresss, port, formatProvider, renderTemplate);
152162

153163
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
154164
}
@@ -161,16 +171,18 @@ public static LoggerConfiguration SplunkViaTcp(
161171
/// <param name="port">The TCP port</param>
162172
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
163173
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
174+
/// <param name="renderTemplate">If ture, the message template is rendered</param>
164175
/// <returns></returns>
165176
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
166177
public static LoggerConfiguration SplunkViaTcp(
167178
this LoggerSinkConfiguration loggerConfiguration,
168179
string host,
169180
int port,
170181
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum,
171-
IFormatProvider formatProvider = null)
182+
IFormatProvider formatProvider = null,
183+
bool renderTemplate = true)
172184
{
173-
var sink = new SplunkViaTcpSink(host, port, formatProvider);
185+
var sink = new SplunkViaTcpSink(host, port, formatProvider, renderTemplate);
174186

175187
return loggerConfiguration.Sink(sink, restrictedToMinimumLevel);
176188
}

src/Serilog.Sinks.Splunk.FullNetFx/Sinks/Splunk/SplunkViaUdpSink.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ namespace Serilog.Sinks.Splunk
2828
/// </summary>
2929
public class SplunkViaUdpSink : ILogEventSink, IDisposable
3030
{
31-
Socket _socket;
32-
JsonFormatter _jsonFormatter;
31+
readonly Socket _socket;
32+
readonly JsonFormatter _jsonFormatter;
3333

3434
/// <summary>
3535
/// Creates an instance of the Splunk UDP Sink
@@ -52,12 +52,14 @@ public SplunkViaUdpSink(IPAddress hostAddress, int port, IFormatProvider formatP
5252
/// <param name="host">The Splunk Host</param>
5353
/// <param name="port">The UDP port configured in Splunk</param>
5454
/// <param name="formatProvider">Optional format provider</param>
55-
public SplunkViaUdpSink(string host, int port, IFormatProvider formatProvider = null)
55+
/// <param name="renderTemplate">If true, the message template is rendered</param>
56+
public SplunkViaUdpSink(string host, int port, IFormatProvider formatProvider = null,
57+
bool renderTemplate = true)
5658
{
5759
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
5860
_socket.Connect(host, port);
5961

60-
_jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
62+
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate:renderTemplate);
6163
}
6264

6365
/// <inheritdoc/>

src/Serilog.Sinks.Splunk/LoggerConfigurationSplunkExtensions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static class LoggerConfigurationSplunkExtensions
3434
/// <param name="batchInterval">The interval on which to log via http</param>
3535
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
3636
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
37+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
3738
/// <returns>Logger configuration, allowing configuration to continue.</returns>
3839
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
3940
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
@@ -43,9 +44,10 @@ public static LoggerConfiguration SplunkViaHttp(
4344
int batchSizeLimit,
4445
TimeSpan batchInterval,
4546
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Debug,
46-
IFormatProvider formatProvider = null)
47+
IFormatProvider formatProvider = null,
48+
bool renderTemplate = true)
4749
{
48-
var sink = new SplunkViaHttpSink(context, batchSizeLimit, batchInterval, formatProvider);
50+
var sink = new SplunkViaHttpSink(context, batchSizeLimit, batchInterval, formatProvider, renderTemplate);
4951

5052
return loggerConfiguration.Sink(sink);
5153
}
@@ -64,13 +66,13 @@ public static LoggerConfiguration SplunkViaHttp(
6466
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
6567
/// <param name="index"></param>
6668
/// <param name="userName"></param>
69+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
6770
/// <returns>Logger configuration, allowing configuration to continue.</returns>
6871
/// <exception cref="ArgumentNullException">A required parameter is null.</exception>
6972
/// <remarks>TODO: Add link to splunk configuration and wiki</remarks>
7073
public static LoggerConfiguration SplunkViaHttp(
7174
this LoggerSinkConfiguration loggerConfiguration,
7275
Context context,
73-
7476
string index,
7577
string userName,
7678
string password,
@@ -79,9 +81,10 @@ public static LoggerConfiguration SplunkViaHttp(
7981
int batchSizeLimit,
8082
TimeSpan batchInterval,
8183
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Debug,
82-
IFormatProvider formatProvider = null)
84+
IFormatProvider formatProvider = null,
85+
bool renderTemplate = true)
8386
{
84-
var sink = new SplunkViaHttpSink(new SplunkContext(context, index, userName, password, resourceNameSpace, transmitterArgs), batchSizeLimit, batchInterval, formatProvider);
87+
var sink = new SplunkViaHttpSink(new SplunkContext(context, index, userName, password, resourceNameSpace, transmitterArgs), batchSizeLimit, batchInterval, formatProvider,renderTemplate);
8588

8689
return loggerConfiguration.Sink(sink);
8790
}

0 commit comments

Comments
 (0)