Skip to content

Commit 08a34bd

Browse files
committed
Added Dockerfile and setup for Sample to Docker Compose.
1 parent 8ef3028 commit 08a34bd

File tree

12 files changed

+118
-8
lines changed

12 files changed

+118
-8
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bin\
2+
obj\

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,6 @@ _Pvt_Extensions
235235
# FAKE - F# Make
236236
.fake/
237237

238-
.vscode/
238+
.vscode/
239+
240+
sample/Sample/out/

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FROM microsoft/dotnet:2.0-sdk AS build
2+
ADD . /
3+
WORKDIR /sample/Sample
4+
RUN dotnet restore
5+
RUN dotnet publish -c Release -o out -f netcoreapp2.0
6+
7+
FROM microsoft/dotnet:2.0-runtime AS runtime
8+
WORKDIR /sample/Sample
9+
COPY --from=build /sample/Sample/out ./
10+
ENTRYPOINT ["dotnet", "Sample.dll"]

docker-compose.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
version: '2'
2+
services:
3+
splunk:
4+
build: ./sample/splunk
5+
image: serilog-splunk
6+
ports:
7+
- 8000:8000
8+
- 8088:8088
9+
- 8089:8089
10+
environment:
11+
SPLUNK_START_ARGS: "--accept-license --answer-yes"
12+
SPLUNK_USER: "root"
13+
sampleconsoleapp:
14+
depends_on:
15+
- "splunk"
16+
build: .
17+
image: serilog-console-sample

sample/Sample/Program.cs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,75 @@
11
using System.Collections.Generic;
22
using System.Linq;
3+
using System.IO;
34
using Serilog;
45
using Serilog.Sinks.Splunk;
5-
6+
using Microsoft.Extensions.Configuration;
67
namespace Sample
78
{
89
public class Program
910
{
10-
const string SPLUNK_FULL_ENDPOINT = "http://localhost:8088/services/collector"; // Full splunk url
11-
const string SPLUNK_ENDPOINT = "http://localhost:8088"; // Your splunk url
12-
const string SPLUNK_HEC_TOKEN = "1AFAC088-BFC6-447F-A358-671FA7465342"; // Your HEC token. See http://docs.splunk.com/Documentation/Splunk/latest/Data/UsetheHTTPEventCollector
11+
const string SPLUNK_FULL_ENDPOINT = "http://splunk:8088/services/collector"; // Full splunk url
12+
const string SPLUNK_ENDPOINT = "http://splunk:8088"; // Your splunk url
13+
const string SPLUNK_HEC_TOKEN = "00112233-4455-6677-8899-AABBCCDDEEFF"; // Your HEC token. See http://docs.splunk.com/Documentation/Splunk/latest/Data/UsetheHTTPEventCollector
1314
public static string EventCollectorToken = SPLUNK_HEC_TOKEN;
1415

1516
public static void Main(string[] args)
1617
{
1718
var eventsToCreate = 100;
1819
var runSSL = false;
20+
var millisecsToWait = 30000;
1921

2022
if (args.Length > 0)
2123
eventsToCreate = int.Parse(args[0]);
2224

2325
if (args.Length == 2)
2426
runSSL = bool.Parse(args[1]);
2527

26-
Log.Information("Sample starting up");
28+
if (args.Length == 3)
29+
millisecsToWait = int.Parse(args[2]);
30+
2731
Serilog.Debugging.SelfLog.Enable(System.Console.Out);
32+
Log.Information("Sample app starting up...");
33+
34+
Log.Information("Waiting {} millisecs...", millisecsToWait);
2835

36+
System.Threading.Thread.Sleep(millisecsToWait);
37+
38+
UsingAppSettingsJson(eventsToCreate);
39+
UsingHostOnly(eventsToCreate);
2940
UsingHostOnly(eventsToCreate);
3041
UsingFullUri(eventsToCreate);
3142
OverridingSource(eventsToCreate);
3243
OverridingSourceType(eventsToCreate);
3344
OverridingHost(eventsToCreate);
3445
WithNoTemplate(eventsToCreate);
3546
WithCompactSplunkFormatter(eventsToCreate);
47+
3648
if (runSSL)
3749
{
3850
UsingSSL(eventsToCreate);
3951
}
4052
AddCustomFields(eventsToCreate);
4153

42-
Log.Debug("Done");
54+
Log.Information("Done....");
55+
}
56+
public static void UsingAppSettingsJson(int eventsToCreate)
57+
{
58+
var configuration = new ConfigurationBuilder()
59+
.SetBasePath(Directory.GetCurrentDirectory())
60+
.AddJsonFile("appsettings.json")
61+
.Build();
62+
63+
Log.Logger = new LoggerConfiguration()
64+
.ReadFrom.Configuration(configuration)
65+
.CreateLogger();
66+
67+
foreach (var i in Enumerable.Range(0, eventsToCreate))
68+
{
69+
Log.Information("Running via appsettings.json {Counter}", i);
70+
}
71+
72+
Log.CloseAndFlush();
4373
}
4474

4575
private static void WithCompactSplunkFormatter(int eventsToCreate)
@@ -59,7 +89,7 @@ private static void WithCompactSplunkFormatter(int eventsToCreate)
5989

6090
foreach (var i in Enumerable.Range(0, eventsToCreate))
6191
{
62-
Log.Information("{Counter}{Message}", i, "Running vanilla loop with CompactSplunkJsonFormatter");
92+
Log.Information("{Counter}{Message}", i, " Running vanilla loop with CompactSplunkJsonFormatter");
6393
}
6494

6595
Log.CloseAndFlush();

sample/Sample/Sample.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66
</PropertyGroup>
77

88
<ItemGroup>
9+
<PackageReference Include="Serilog" Version="2.6.0" />
910
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.0" />
1011
<PackageReference Include="System.Net.Http" Version="4.3.3" />
12+
<PackageReference Include="Serilog.Settings.Configuration" Version="2.6.1" />
13+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.1" />
1114
</ItemGroup>
1215

1316
<ItemGroup>
1417
<ProjectReference Include="..\..\src\Serilog.Sinks.Splunk\Serilog.Sinks.Splunk.csproj" />
1518
</ItemGroup>
19+
20+
<ItemGroup>
21+
<None Update="appsettings.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
24+
</ItemGroup>
1625
</Project>

sample/Sample/appsettings.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"Serilog": {
3+
"Using": ["Serilog.Sinks.Console", "Serilog.Sinks.Splunk"],
4+
"MinimumLevel": "Information",
5+
"WriteTo": [{
6+
"Name": "Console"
7+
},
8+
{
9+
"Name": "EventCollector",
10+
"Args": {
11+
"splunkHost": "http://splunk:8088",
12+
"eventCollectorToken": "00112233-4455-6677-8899-AABBCCDDEEFF"
13+
}
14+
}
15+
],
16+
"Properties": {
17+
"Application": "Serilog Splunk Console Sample",
18+
"Serilog.Sinks.Splunk.Sample": "ViaEventCollector",
19+
"Serilog.Sinks.Splunk.Sample.TestType": "AppSettings.json"
20+
}
21+
}
22+
}

sample/splunk/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM splunk/splunk:6.5.3
2+
ADD etc ${SPLUNK_HOME}/etc
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[http]
2+
disabled = 0
3+
enableSSL = 0
4+
5+
[http://serilog_sample]
6+
token = 00112233-4455-6677-8899-AABBCCDDEEFF
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[http]
2+
disabled = 0
3+
useDeploymentServer = 0

0 commit comments

Comments
 (0)