Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature.customfields #59

Merged
merged 36 commits into from
Jun 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
021a0b6
Merge pull request #24 from serilog/dev
merbla Jul 1, 2016
6ed3774
Merge pull request #35 from serilog/dev
merbla Oct 7, 2016
cf4f649
Merge pull request #41 from serilog/dev
merbla Oct 25, 2016
95baa54
Merge pull request #44 from serilog/dev
merbla Jan 28, 2017
f3c8d59
Merge pull request #46 from serilog/dev
merbla Mar 3, 2017
0dc2fb5
Merge pull request #54 from serilog/dev
merbla May 28, 2017
c22eea8
Merge pull request #57 from serilog/dev
merbla May 28, 2017
80c5335
started with feature Extra splunk fields
patriklindstrom Jun 4, 2017
09db6be
Added CustomFieldsClass
patriklindstrom Jun 6, 2017
c1be7ec
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 6, 2017
5f2aef5
Merge branch 'serilog/dev' into feature.customfields
patriklindstrom Jun 6, 2017
a51c68f
Rename class to CustomField
patriklindstrom Jun 6, 2017
6805fc4
Added new CustomFields constructor and started on tests
patriklindstrom Jun 6, 2017
cd326c7
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 6, 2017
7d85d2a
setting up Arrange part of CustomFields_for_Splunk_Sink test
patriklindstrom Jun 6, 2017
f30d87c
Added new test project with NUnit Test for CustomField
patriklindstrom Jun 10, 2017
811a570
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 10, 2017
9aff614
Adding splunkjsonformatter new constructor
patriklindstrom Jun 10, 2017
028f0fa
Test_CustomFields_Jsonformatter_for_Splunk_Sink AA part done
patriklindstrom Jun 11, 2017
5734d89
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 11, 2017
3e72500
Test_CustomFields_Jsonformatter_for_Splunk_Sink starts to work
patriklindstrom Jun 11, 2017
fb61222
Merge branch 'refs/heads/feature.customfields' into feature.customfie…
patriklindstrom Jun 11, 2017
4da39f2
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 11, 2017
00dbb0d
new constructor for SplunkJsonFormatter with customfields
patriklindstrom Jun 12, 2017
eb92fa5
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 12, 2017
9d2075f
Merge remote-tracking branch 'refs/remotes/origin/feature.customfield…
patriklindstrom Jun 13, 2017
7f1450b
Added Fact for CustomFieldTest
patriklindstrom Jun 13, 2017
66047d3
Renam CustomField in Sample
patriklindstrom Jun 13, 2017
c8c03f5
Added Comments for params and Classes
patriklindstrom Jun 13, 2017
387d7c0
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 13, 2017
4ff9422
Merge remote-tracking branch 'refs/remotes/origin/feature.customfield…
patriklindstrom Jun 14, 2017
fd81bef
Changes for pullreques
patriklindstrom Jun 14, 2017
75ec6d1
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 14, 2017
09750db
Moved CustomFieldTest from NUnit to Xunit
patriklindstrom Jun 14, 2017
ab8c8f5
Removed NUnit test project
patriklindstrom Jun 14, 2017
6894f11
Merge branch 'refs/heads/feature.customfields.wot' into feature.custo…
patriklindstrom Jun 14, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 55 additions & 16 deletions sample/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using Serilog;
using Serilog.Sinks.Splunk;

namespace Sample
{
public class Program
{
public static string EventCollectorToken = "2B94855F-1184-46F7-BFF1-56A3112F627E";
const string SPLUNK_FULL_ENDPOINT = "http://localhost:8088/services/collector"; // Full splunk url
const string SPLUNK_ENDPOINT = "http://localhost:8088"; // Your splunk url
const string SPLUNK_HEC_TOKEN = "1AFAC088-BFC6-447F-A358-671FA7465342"; // Your HEC token. See http://docs.splunk.com/Documentation/Splunk/latest/Data/UsetheHTTPEventCollector
public static string EventCollectorToken = SPLUNK_HEC_TOKEN;

public static void Main(string[] args)
{
Expand All @@ -30,7 +34,10 @@ public static void Main(string[] args)
WithNoTemplate(eventsToCreate);
WithCompactSplunkFormatter(eventsToCreate);
if (runSSL)
{
UsingSSL(eventsToCreate);
}
AddCustomFields(eventsToCreate);

Log.Debug("Done");
}
Expand All @@ -42,10 +49,11 @@ private static void WithCompactSplunkFormatter(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088/services/collector",
Program.EventCollectorToken,new CompactSplunkJsonFormatter())
SPLUNK_FULL_ENDPOINT,
Program.EventCollectorToken, new CompactSplunkJsonFormatter())
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla with CompactSplunkJsonFormatter specified")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType",
"Vanilla with CompactSplunkJsonFormatter specified")
.CreateLogger();


Expand All @@ -64,7 +72,7 @@ public static void OverridingSource(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken,
source: "Serilog.Sinks.Splunk.Sample.TestSource")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -77,7 +85,6 @@ public static void OverridingSource(int eventsToCreate)
}

Log.CloseAndFlush();

}

public static void OverridingSourceType(int eventsToCreate)
Expand All @@ -87,9 +94,9 @@ public static void OverridingSourceType(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken,
sourceType: "Serilog.Sinks.Splunk.Sample.TestSourceType")
sourceType: "_json")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Source Type Override")
.CreateLogger();
Expand All @@ -100,7 +107,6 @@ public static void OverridingSourceType(int eventsToCreate)
}

Log.CloseAndFlush();

}

public static void OverridingHost(int eventsToCreate)
Expand All @@ -110,7 +116,7 @@ public static void OverridingHost(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken,
host: "myamazingmachine")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -123,7 +129,6 @@ public static void OverridingHost(int eventsToCreate)
}

Log.CloseAndFlush();

}

public static void UsingFullUri(int eventsToCreate)
Expand All @@ -133,7 +138,7 @@ public static void UsingFullUri(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088/services/collector",
SPLUNK_FULL_ENDPOINT,
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla with full uri specified")
Expand All @@ -155,7 +160,7 @@ public static void UsingHostOnly(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "Vanilla No services/collector in uri")
Expand All @@ -176,7 +181,7 @@ public static void WithNoTemplate(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"http://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken,
renderTemplate: false)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
Expand All @@ -198,7 +203,7 @@ public static void UsingSSL(int eventsToCreate)
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
"https://localhost:8088",
SPLUNK_ENDPOINT,
Program.EventCollectorToken)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "HTTPS")
Expand All @@ -210,5 +215,39 @@ public static void UsingSSL(int eventsToCreate)
}
Log.CloseAndFlush();
}

public static void AddCustomFields(int eventsToCreate)
{
var metaData = new CustomFields(new List<CustomField>
{
new CustomField("relChan", "Test"),
new CustomField("version", "17.8.9.beta"),
new CustomField("rel", "REL1706"),
new CustomField("role", new List<string>() { "service", "rest", "ESB" })
});
// Override Source
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.LiterateConsole()
.WriteTo.EventCollector(
splunkHost: SPLUNK_ENDPOINT
, eventCollectorToken: SPLUNK_HEC_TOKEN
, host: System.Environment.MachineName
, source: "BackPackTestServerChannel"
, sourceType: "_json"
,fields: metaData)
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample", "ViaEventCollector")
.Enrich.WithProperty("Serilog.Sinks.Splunk.Sample.TestType", "AddCustomFields")
.CreateLogger();

foreach (var i in Enumerable.Range(0, eventsToCreate))
{
Log.Information("AddCustomFields {Counter}", i);
}

Log.CloseAndFlush();
}
}


}
2 changes: 1 addition & 1 deletion serilog-sinks-splunk.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.6
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7A774CBB-A6E9-4854-B4DB-4CF860B0C1C5}"
EndProject
Expand Down
41 changes: 41 additions & 0 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/CustomField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serilog.Sinks.Splunk
{ /// <summary>
/// A Class for storing CustomField. They are sort of key,value pair. In simpler form key as string and value as single string, but could also be key and list of strings.
/// </summary>
public class CustomField
{ /// <summary>
/// the fieldsname eg: role, version,
/// </summary>
public string Name { get; set; }
/// <summary>
/// All values even simple string are stored as a list
/// </summary>
public List<string> ValueList { get; set; }
/// <summary>
/// constructor for a simple fieldname and a value both are strings
/// </summary>
/// <param name="name">Name of filed to be indexed by Splunk. Eg Role,Version,Channel</param>
/// <param name="value">Value of keypair. Eg. Test,1.08, RestService</param>
public CustomField(string name, string value)
{
Name = name;
ValueList = new List<string> { value };
}
/// <summary>
/// Constructor for Name and array of values
/// </summary>
/// <param name="name">Name of field eg TypeOfResource</param>
/// <param name="value">Array of values that should be connected with field.Eg Backend,Service,Rest</param>
public CustomField(string name, List<string> value)
{
Name = name;
ValueList = value;
}
}
}
41 changes: 41 additions & 0 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/CustomFields.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serilog.Sinks.Splunk
{
/// <summary>
/// Class for storing CustomFields that splunk will index for the event but will not be displayed in the event.
/// They are the same for all events. Could could contain type of server or releasecode see: http://dev.splunk.com/view/event-collector/SP-CAAAFB6
/// </summary>
public class CustomFields
{/// <summary>
/// The List of all CustomFields of type CustomField
/// </summary>
public List<CustomField> CustomFieldList { get; set; }
/// <summary>
/// Constructor with no inital data
/// </summary>
public CustomFields()
{
CustomFieldList = new List<CustomField>();
}
/// <summary>
/// Constructor with simple CustomField
/// </summary>
/// <param name="customField"></param>
public CustomFields(CustomField customField)
{
CustomFieldList = new List<CustomField>{customField};
}
/// <summary>
/// Constructor with a list of CustomFields
/// </summary>
public CustomFields(List<CustomField> customFields)
{
CustomFieldList = customFields ;
}
}
}
41 changes: 41 additions & 0 deletions src/Serilog.Sinks.Splunk/Sinks/Splunk/EventCollectorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,47 @@ public EventCollectorSink(
messageHandler)
{
}
/// <summary>
/// Creates a new instance of the sink with Customfields
/// </summary>
/// <param name="splunkHost">The host of the Splunk instance with the Event collector configured</param>
/// <param name="eventCollectorToken">The token to use when authenticating with the event collector</param>
/// <param name="uriPath">Change the default endpoint of the Event Collector e.g. services/collector/event</param>
/// <param name="batchSizeLimit">The size of the batch when sending to the event collector</param>
/// <param name="formatProvider">The format provider used when rendering the message</param>
/// <param name="renderTemplate">Whether to render the message template</param>
/// <param name="batchIntervalInSeconds">The interval in seconds that batching should occur</param>
/// <param name="index">The Splunk index to log to</param>
/// <param name="fields">Add extra CustomExtraFields for Splunk to index</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="messageHandler">The handler used to send HTTP requests</param>
public EventCollectorSink(
string splunkHost,
string eventCollectorToken,
string uriPath,
string source,
string sourceType,
string host,
string index,
CustomFields fields,
int batchIntervalInSeconds,
int batchSizeLimit,
IFormatProvider formatProvider = null,
bool renderTemplate = true,
HttpMessageHandler messageHandler = null)
// TODO here is the jsonformatter creation. We must make way to test output of jsonformatter.
: this(
splunkHost,
eventCollectorToken,
uriPath,
batchIntervalInSeconds,
batchSizeLimit,
new SplunkJsonFormatter(renderTemplate, formatProvider, source, sourceType, host, index,fields),
messageHandler)
{
}


/// <summary>
Expand Down
Loading