Skip to content

Commit 417f9de

Browse files
committed
New Json formatter
1 parent 6d40a30 commit 417f9de

File tree

6 files changed

+89
-11
lines changed

6 files changed

+89
-11
lines changed

src/Serilog.Sinks.Splunk.FullNetFx/Serilog.Sinks.Splunk.FullNetFx.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@
6565
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\SplunkContext.cs">
6666
<Link>Sinks\Splunk\SplunkContext.cs</Link>
6767
</Compile>
68+
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\SplunkJsonFormatter.cs">
69+
<Link>Sinks\Splunk\SplunkJsonFormatter.cs</Link>
70+
</Compile>
6871
<Compile Include="..\Serilog.Sinks.Splunk\Sinks\Splunk\SplunkViaHttpSink.cs">
6972
<Link>Sinks\Splunk\SplunkViaHttpSink.cs</Link>
7073
</Compile>

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ public class SplunkViaTcpSink : ILogEventSink, IDisposable
3838
/// <param name="hostAddress">The Splunk Host</param>
3939
/// <param name="port">The UDP port configured in Splunk</param>
4040
/// <param name="formatProvider">Optional format provider</param>
41+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
42+
4143
public SplunkViaTcpSink(
4244
IPAddress hostAddress,
4345
int port,
44-
IFormatProvider formatProvider = null)
46+
IFormatProvider formatProvider = null,
47+
bool renderTemplate = true)
4548
{
4649
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
4750

4851
_writer = new TcpSocketWriter(hostAddress, port, reconnectionPolicy, 10000);
4952

50-
_jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
53+
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
5154
}
5255

5356
/// <summary>
@@ -56,17 +59,19 @@ public SplunkViaTcpSink(
5659
/// <param name="host">The Splunk Host</param>
5760
/// <param name="port">The UDP port configured in Splunk</param>
5861
/// <param name="formatProvider">Optional format provider</param>
62+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
5963
public SplunkViaTcpSink(
6064
string host,
6165
int port,
62-
IFormatProvider formatProvider = null)
66+
IFormatProvider formatProvider = null,
67+
bool renderTemplate = true)
6368
{
6469
var reconnectionPolicy = new ExponentialBackoffTcpReconnectionPolicy();
6570
var ipAddress = IPAddress.Parse(host);
6671

6772
_writer = new TcpSocketWriter(ipAddress, port, reconnectionPolicy, 10000);
6873

69-
_jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
74+
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
7075
}
7176

7277
/// <inheritdoc/>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public class SplunkViaUdpSink : ILogEventSink, IDisposable
3737
/// <param name="hostAddress">The Splunk Host</param>
3838
/// <param name="port">The UDP port configured in Splunk</param>
3939
/// <param name="formatProvider">Optional format provider</param>
40-
public SplunkViaUdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null)
40+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
41+
public SplunkViaUdpSink(IPAddress hostAddress, int port, IFormatProvider formatProvider = null, bool renderTemplate = true)
4142
{
4243
_socket = new Socket(SocketType.Dgram, ProtocolType.Udp);
4344
_socket.Connect(hostAddress, port);
4445

45-
_jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
46+
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
4647
}
4748

4849
/// <summary>

src/Serilog.Sinks.Splunk/Serilog.Sinks.Splunk.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
<Link>Properties\CommonAssemblyInfo.cs</Link>
5656
</Compile>
5757
<Compile Include="Sinks\Splunk\SplunkContext.cs" />
58+
<Compile Include="Sinks\Splunk\SplunkJsonFormatter.cs" />
5859
<Compile Include="Sinks\Splunk\SplunkViaHttpSink.cs" />
5960
</ItemGroup>
6061
<ItemGroup>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2014 Serilog Contriutors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.using System;
14+
15+
using System;
16+
using System.IO;
17+
using Serilog.Formatting.Json;
18+
19+
namespace Serilog.Sinks.Splunk
20+
{
21+
/// <summary>
22+
/// A json formatter to allow conditional rendering of properties
23+
/// </summary>
24+
public class SplunkJsonFormatter : JsonFormatter
25+
{
26+
private readonly bool _renderTemplate;
27+
28+
/// <summary>
29+
/// Construct a <see cref="JsonFormatter"/>.
30+
/// </summary>
31+
/// <param name="omitEnclosingObject">If true, the properties of the event will be written to
32+
/// the output without enclosing braces. Otherwise, if false, each event will be written as a well-formed
33+
/// JSON object.</param>
34+
/// <param name="closingDelimiter">A string that will be written after each log event is formatted.
35+
/// If null, <see cref="Environment.NewLine"/> will be used. Ignored if <paramref name="omitEnclosingObject"/>
36+
/// is true.</param>
37+
/// <param name="renderMessage">If true, the message will be rendered and written to the output as a
38+
/// property named RenderedMessage.</param>
39+
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
40+
/// <param name="renderTemplate">If true, the template used will be rendered and written to the output as a property named MessageTemplate</param>
41+
public SplunkJsonFormatter(
42+
bool omitEnclosingObject = false,
43+
string closingDelimiter = null,
44+
bool renderMessage = false,
45+
IFormatProvider formatProvider = null,
46+
bool renderTemplate = true)
47+
:base(omitEnclosingObject,closingDelimiter,renderMessage,formatProvider)
48+
{
49+
_renderTemplate = renderTemplate;
50+
}
51+
52+
53+
/// <summary>
54+
///
55+
/// </summary>
56+
/// <param name="template"></param>
57+
/// <param name="delim"></param>
58+
/// <param name="output"></param>
59+
protected override void WriteMessageTemplate(string template, ref string delim, TextWriter output)
60+
{
61+
if(_renderTemplate)
62+
base.WriteMessageTemplate(template, ref delim, output);
63+
}
64+
}
65+
}

src/Serilog.Sinks.Splunk/Sinks/Splunk/SplunkViaHttpSink.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public class SplunkViaHttpSink : ILogEventSink, IDisposable
4949
/// <param name="batchSizeLimit">The size of the batch prior to logging</param>
5050
/// <param name="batchInterval">The interval on which to log via http</param>
5151
/// <param name="formatProvider">The format provider to use when rendering the message</param>
52+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
5253
public SplunkViaHttpSink(
5354
SplunkContext context,
5455
int batchSizeLimit,
5556
TimeSpan batchInterval,
56-
IFormatProvider formatProvider = null)
57-
: this(context, context.Index, context.Username, context.Password, batchSizeLimit, batchInterval, context.ResourceNamespace, context.TransmitterArgs, formatProvider)
57+
IFormatProvider formatProvider = null,
58+
bool renderTemplate = true)
59+
: this(context, context.Index, context.Username, context.Password, batchSizeLimit, batchInterval, context.ResourceNamespace, context.TransmitterArgs, formatProvider, renderTemplate: renderTemplate)
5860
{
5961
}
6062

@@ -70,6 +72,7 @@ public SplunkViaHttpSink(
7072
/// <param name="resourceNamespace">The resource namespaces</param>
7173
/// <param name="transmitterArgs">The </param>
7274
/// <param name="formatProvider">The format provider to be used when rendering the message</param>
75+
/// <param name="renderTemplate">If true, the message template will be rendered</param>
7376
public SplunkViaHttpSink(
7477
SplunkClient.Context context,
7578
string index,
@@ -79,7 +82,8 @@ public SplunkViaHttpSink(
7982
TimeSpan batchInterval,
8083
SplunkClient.Namespace resourceNamespace = null,
8184
SplunkClient.TransmitterArgs transmitterArgs = null,
82-
IFormatProvider formatProvider = null
85+
IFormatProvider formatProvider = null,
86+
bool renderTemplate = true
8387
)
8488
{
8589
_index = index;
@@ -90,12 +94,11 @@ public SplunkViaHttpSink(
9094

9195
_queue = new ConcurrentQueue<LogEvent>();
9296

93-
_jsonFormatter = new JsonFormatter(renderMessage: true, formatProvider: formatProvider);
97+
_jsonFormatter = new SplunkJsonFormatter(renderMessage: true, formatProvider: formatProvider, renderTemplate: renderTemplate);
9498

9599
_service = resourceNamespace == null
96100
? new SplunkClient.Service(context, new SplunkClient.Namespace("nobody", "search"))
97101
: new SplunkClient.Service(context, resourceNamespace);
98-
99102

100103
RepeatAction.OnInterval(batchInterval, () => ProcessQueue().Wait(), new CancellationToken());
101104
}

0 commit comments

Comments
 (0)