Skip to content

Commit 01501b4

Browse files
committed
Merge pull request #5 from merbla/feature-templaterendering
Add conditional rendering of message templates
2 parents 6d40a30 + 2e5b2ff commit 01501b4

15 files changed

+275
-53
lines changed

.nuget/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="serilog-generator" version="1.0.7" />
4+
</packages>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace Serilog.Sinks.Splunk.Sample
2+
{
3+
internal interface IConfigure
4+
{
5+
void Configure();
6+
}
7+
}
Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,93 @@
11
using System;
2-
using System.Collections.Concurrent;
3-
using System.Linq;
2+
using Splunk.Client;
3+
44

55
namespace Serilog.Sinks.Splunk.Sample
66
{
77
class Program
88
{
99
static void Main(string[] args)
1010
{
11-
Log.Logger = new LoggerConfiguration()
12-
.WriteTo.LiterateConsole()
13-
.WriteTo.SplunkViaTcp("127.0.0.1", 10001)
14-
.CreateLogger();
11+
var stub = new Stub();
12+
13+
var http = new ViaHttp();
14+
var tcp = new ViaTcp();
15+
var udp = new ViaHttp();
16+
17+
http.Configure();
18+
udp.Configure();
19+
tcp.Configure();
1520

16-
Log.Information("Just another test");
21+
Log.Information("Simulation running, press any key to exit.");
22+
23+
stub.Run();
1724

1825
Console.ReadLine();
1926
}
2027
}
28+
29+
class ViaTcp : IConfigure
30+
{
31+
public void Configure()
32+
{
33+
Log.Logger = new LoggerConfiguration()
34+
.WriteTo.LiterateConsole()
35+
.WriteTo.SplunkViaTcp("127.0.0.1", 10001, renderTemplate: false)
36+
.Enrich.WithThreadId()
37+
.Enrich.WithProperty("SplunkSample", "ViaTCP")
38+
.MinimumLevel.Debug()
39+
.CreateLogger();
40+
}
41+
}
42+
43+
class ViaHttp : IConfigure
44+
{
45+
public void Configure()
46+
{
47+
var generalSplunkContext = new global::Splunk.Client.Context(Scheme.Https, "127.0.0.1", 8089);
48+
49+
var transmitterArgs = new TransmitterArgs
50+
{
51+
Source = "Splunk.Sample",
52+
SourceType = "Splunk Sample Source"
53+
};
54+
55+
const string username = "my splunk user";
56+
const string password = "my splunk password";
57+
const string splunkIndex = "mysplunktest";
58+
59+
var serilogContext = new SplunkContext(
60+
generalSplunkContext,
61+
splunkIndex,
62+
username,
63+
password,
64+
null,
65+
transmitterArgs);
66+
67+
Log.Logger = new LoggerConfiguration()
68+
.WriteTo.LiterateConsole()
69+
.WriteTo.SplunkViaHttp(serilogContext, 100, TimeSpan.FromSeconds(10))
70+
.Enrich.WithThreadId()
71+
.Enrich.WithProperty("SplunkSample", "ViaHttp")
72+
.MinimumLevel.Debug()
73+
.CreateLogger();
74+
}
75+
}
76+
77+
78+
class ViaUdp : IConfigure
79+
{
80+
public void Configure()
81+
{
82+
Log.Logger = new LoggerConfiguration()
83+
.WriteTo.LiterateConsole()
84+
.WriteTo.SplunkViaUdp("127.0.0.1", 10002, renderTemplate: false)
85+
.Enrich.WithThreadId()
86+
.Enrich.WithProperty("SplunkSample", "ViaUDP")
87+
.MinimumLevel.Debug()
88+
.CreateLogger();
89+
}
90+
}
91+
92+
2193
}

sample/Serilog.Sinks.Splunk.Sample/Serilog.Sinks.Splunk.Sample.csproj

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,16 @@
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
3535
<ItemGroup>
36-
<Reference Include="Serilog">
37-
<HintPath>..\..\packages\Serilog.1.5.5\lib\net45\Serilog.dll</HintPath>
36+
<Reference Include="Serilog, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
37+
<HintPath>..\..\packages\Serilog.1.5.6\lib\net45\Serilog.dll</HintPath>
38+
<Private>True</Private>
39+
</Reference>
40+
<Reference Include="serilog-generator">
41+
<HintPath>..\..\packages\serilog-generator.1.0.7\tools\serilog-generator.exe</HintPath>
3842
</Reference>
39-
<Reference Include="Serilog.FullNetFx">
40-
<HintPath>..\..\packages\Serilog.1.5.5\lib\net45\Serilog.FullNetFx.dll</HintPath>
43+
<Reference Include="Serilog.FullNetFx, Version=1.5.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
44+
<HintPath>..\..\packages\Serilog.1.5.6\lib\net45\Serilog.FullNetFx.dll</HintPath>
45+
<Private>True</Private>
4146
</Reference>
4247
<Reference Include="Serilog.Sinks.Literate">
4348
<HintPath>..\..\packages\Serilog.Sinks.Literate.1.0.6\lib\net45\Serilog.Sinks.Literate.dll</HintPath>
@@ -49,13 +54,19 @@
4954
<HintPath>..\..\packages\Splunk.Logging.Common.1.1.0\lib\net45\Splunk.Logging.Common.dll</HintPath>
5055
<Private>True</Private>
5156
</Reference>
57+
<Reference Include="Sprache, Version=2.0.0.45, Culture=neutral, processorArchitecture=MSIL">
58+
<HintPath>..\..\packages\Sprache.2.0.0.45\lib\portable-net4+netcore45+win8+wp8+sl5+MonoAndroid1+MonoTouch1\Sprache.dll</HintPath>
59+
<Private>True</Private>
60+
</Reference>
5261
<Reference Include="System" />
5362
<Reference Include="System.Core" />
5463
<Reference Include="Microsoft.CSharp" />
5564
</ItemGroup>
5665
<ItemGroup>
66+
<Compile Include="IConfigure.cs" />
5767
<Compile Include="Program.cs" />
5868
<Compile Include="Properties\AssemblyInfo.cs" />
69+
<Compile Include="Stub.cs" />
5970
</ItemGroup>
6071
<ItemGroup>
6172
<None Include="App.config" />
@@ -66,10 +77,6 @@
6677
<Project>{17deed0f-f9cb-48fb-b4dc-53fb6aee64d7}</Project>
6778
<Name>Serilog.Sinks.Splunk.FullNetFx</Name>
6879
</ProjectReference>
69-
<ProjectReference Include="..\..\src\Serilog.Sinks.Splunk\Serilog.Sinks.Splunk.csproj">
70-
<Project>{1493abc3-811c-46c7-92ed-ceb7567fb588}</Project>
71-
<Name>Serilog.Sinks.Splunk</Name>
72-
</ProjectReference>
7380
</ItemGroup>
7481
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
7582
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Collections.Concurrent;
2+
using System.Linq;
3+
using Serilog.Generator.Actors;
4+
using Serilog.Generator.Model;
5+
6+
namespace Serilog.Sinks.Splunk.Sample
7+
{
8+
internal class Stub
9+
{
10+
public void Run()
11+
{
12+
const int initialCustomers = 1;
13+
14+
Log.Information("Simulation starting with {InitialCustomers} initial customers...", initialCustomers);
15+
16+
var catalog = new Catalog();
17+
18+
var customers = new ConcurrentBag<Customer>(Enumerable.Range(0, initialCustomers)
19+
.Select(_ => new Customer(catalog)));
20+
21+
var traffic = new TrafficReferral(customers, catalog);
22+
var admin = new Administrator(catalog);
23+
24+
foreach (var c in customers)
25+
c.Start();
26+
27+
admin.Start();
28+
traffic.Start();
29+
}
30+
}
31+
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="Serilog" version="1.5.5" targetFramework="net451" />
3+
<package id="Serilog" version="1.5.6" targetFramework="net451" />
44
<package id="Serilog.Sinks.Literate" version="1.0.6" targetFramework="net451" />
55
<package id="Splunk.Client" version="2.1.2" targetFramework="net451" />
66
<package id="Splunk.Logging.Common" version="1.1.0" targetFramework="net451" />
7-
7+
<package id="Sprache" version="2.0.0.45" targetFramework="net451" />
8+
<package id="serilog-generator" version="1.0.7" />
89
</packages>

serilog-sinks-splunk.sln

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{EBB6CE
2121
EndProject
2222
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Sinks.Splunk.Sample", "sample\Serilog.Sinks.Splunk.Sample\Serilog.Sinks.Splunk.Sample.csproj", "{F0B0E4EF-CF01-46FC-A64C-FE3528F14238}"
2323
EndProject
24+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{B16AD407-36C8-4286-A3E9-CACEBF359731}"
25+
ProjectSection(SolutionItems) = preProject
26+
.nuget\packages.config = .nuget\packages.config
27+
EndProjectSection
28+
EndProject
2429
Global
2530
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2631
Debug|Any CPU = Debug|Any CPU

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/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>

0 commit comments

Comments
 (0)