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

Conversation

patriklindstrom
Copy link
Contributor

Add CustomField splunk feature in serilog-sink log constructor

Splunk has a feature for adding customfields that you want to be associated with the event and can be used in filters but not shown as part of the event. See http://dev.splunk.com/view/event-collector/SP-CAAAFB6 .

These index for CustomFields can be used in splunk filters like eg: role::backend role::rest channel::verification .

These CustomFields can be string key value pairs or key array of values.
This feature (afaik) was missing in the current splunk sink.

I have made these two new classes: CustomField and CustomFieldsList also added constructor that takes fields as parameter and logic for adding this to _suffix

I added test to SplunkJsonFormatterTests. However I am not that good with Xunit test soo also I added project with NUnit test in style of Arrange,Act, Assert style.
I think these sort of filter capabilites is great at larger organisations that will make an abstract log layer around serilog and set these fields at deploy time with a config file and a tool like Octopus Deply or TFS Release Manager. That is nothing that the developer will care about. They will just focus on the what will be in de event. I think better big corporate Splunk Dasboard can be build with this feature where you want eg to search for all event for the last release on all dotnet core rest backendservices deployed to Linux machines to the Acceptance Test Channel.

merbla and others added 30 commits July 1, 2016 14:25
Test_CustomFields_Jsonformatter_for_Splunk_Sink test works for new
constructor
Copy link
Contributor

@merbla merbla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @patriklindstrom,
I think this is a great addition. Thanks so much for taking the time to create a PR!

I have noted a few items, also we are eager to stay with XUnit due to DotNet core concerns and consistency across the Serilog Organisation.

@@ -0,0 +1,7 @@
Getting error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove this file

Notes.md Outdated
@@ -0,0 +1,4 @@
Getting error.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Remove this file

result.json Outdated
@@ -0,0 +1,19 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this an example event? Not sure of its purpose

const string SPLUNK_FULL_ENDPOINT = "http://ws2012-devops:8088/services/collector";
// Patrik local vm machine

const string SPLUNK_ENDPOINT = "http://ws2012-devops:8088"; // Patrik local vm machine
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patriklindstrom FYI your VM info is here. Would the Sample work better with some args? e.g. HEC token and host?

string sourceType,
string host,
string index,
CustomFields customFields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should CustomFields customFields be an optional param. That seems to represent the HEC implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Should CustomFields customFields be an optional param?..."

Yes
But have I not done so by have two constructors one with customfields and one without? Or am I missing something? Do you want me to solve it differently

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @patriklindstrom I missed the other constructor. 👍

@@ -49,7 +49,7 @@ public static class SplunkLoggingConfigurationExtensions
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param>
/// <param name="outputTemplate">The output template to be used when logging</param>
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param>
/// <param name="renderTemplate">If ture, the message template will be rendered</param>
/// <param name="renderTemplate">If true, the message template will be rendered</param>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice pickup!

public TestCustomFields Fields { get; set; }
}
[TestFixture]
class SplunkCustomFieldsTests
Copy link
Contributor

@merbla merbla Jun 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There area some great use cases here, can we move to the existing test project. We are keen to stay with XUnit due to a number of issues with NUnit & DotNet core.

As for the Arrange/Act/Assert. We should be able to achieve a similar pattern in XUnit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"We are keen to stay with XUnit "

Will do

"As for the Arrange/Act/Assert. We should be able to achieve a similar pattern in XUnit"

Hope so or I am lost. I'll give it a try.

@patriklindstrom
Copy link
Contributor Author

patriklindstrom commented Jun 14, 2017 via email

@patriklindstrom
Copy link
Contributor Author

Hi @merbla
Have I fixed everything that you wanted now? I am a bit new to the github pullrequest.
I belive only issue left is about wether the fields parameter is optional or not.

@merbla merbla merged commit c251109 into serilog-contrib:dev Jun 16, 2017
@merbla merbla mentioned this pull request Jun 16, 2017
@merbla
Copy link
Contributor

merbla commented Jun 16, 2017

@patriklindstrom a dev package should be available for this feature. Serilog.Sinks.Splunk.2.3.0-dev-00175.nupkg. https://www.nuget.org/packages/Serilog.Sinks.Splunk/2.3.0-dev-00176

It will go out with #61

@patriklindstrom
Copy link
Contributor Author

patriklindstrom commented Jun 16, 2017 via email

@patriklindstrom
Copy link
Contributor Author

@merbla Is it ok if I update the wiki with CustomFields examples? https://github.com/serilog/serilog-sinks-splunk/wiki/Configuring-the-sink ?

@merbla
Copy link
Contributor

merbla commented Jun 20, 2017

Yep! I have not had a chance to test the dev package.

@merbla
Copy link
Contributor

merbla commented Jun 20, 2017

Let me know if you do not have access.

@dhaval-mistry
Copy link

I have tried to add customfield into splunk using below code. But I am failed.

           //convert RPA JSON log into object 
         var SplunkLog = ConvertJsonToObject(LogJson);
        // add custom field
        var metaData = new CustomFields(new List<CustomField>
        {
            new CustomField("logger", SplunkLog.logger),
            new CustomField("timestamp",  SplunkLog.timestamp.ToString())
        });

        Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .WriteTo.Console()
            .WriteTo.EventCollector(
                splunkHost: SPLUNK_ENDPOINT
                , eventCollectorToken: SPLUNK_HEC_TOKEN
                , fields: metaData  //// adding customField into logger object.
                , host: "RPA BOT Runner"
                , source: "BOT"
                , sourceType: "BOT")
            //SPLUNK_FULL_ENDPOINT,EventCollectorToken)
            .Enrich.WithProperty("CorrelationId", SplunkLog.BOTProperties.CorrelationId)
            .Enrich.WithProperty("BusinessUnit", SplunkLog.BOTProperties.BusinessUnit)
            .Enrich.WithProperty("ApplicationName", SplunkLog.BOTProperties.ApplicationName)
            .Enrich.WithProperty("ApplicationFeatureName", SplunkLog.BOTProperties.ApplicationFeatureName)
            .Enrich.WithProperty("EnvironmentName", SplunkLog.BOTProperties.EnvironmentName)
            .Enrich.WithProperty("BOTMachine", SplunkLog.BOTProperties.BOTMachine)
            .Enrich.WithProperty("CallType", SplunkLog.BOTProperties.CallType)
            .CreateLogger();


        //Log.Information("Running vanilla loop with full uri {Counter}", i);
        Log.Information("logger: {logger} timestamp: {timestamp}", SplunkLog.logger, SplunkLog.timestamp.ToString() );

I could able to create log in splunk but custom fields are not visible in splunk. below is my Splunk JSON we design.

{
"severity": "INFO",
"logger": "RPA",
"timestamp": "09/17/2021",
"RPAJobName": "<>",
"BOTProperties": {
"CorrelationId": "<>",
"BusinessUnit": "<>",
"ApplicationName": "<>",
"ApplicationFeatureName": "<>",
"EnvironmentName": "<>",
"BOTMachine": "<<BOT Machine Name/IP>>",
"CallType": "BOT",
"RequestBody": [
{ "companyId": "gtstest" },
{ "userName": "qaestatement" },
{ "RquestData1": "RquestData1" },
{ "RquestData2": "RquestData2" }
],
"BOT Step": "<< BOT Step Detail >>",
"Status": "PASSED",
"ResponseBody": [
{ "companyId": "gtstest" },
{ "userName": "qaestatement" },
{ "executionStartTime": "09/16/2021 16:25:36.466" },
{ "executionEndTime": "09/16/2021 16:30:27.312" },
{ "Response Sample": "<< Response Sample >>" },
{ "Response Sample 2": "<< Response Sample 2 >>" }
]
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants