From 28d8810808df4487733498c87b9864befd0e2de0 Mon Sep 17 00:00:00 2001 From: EEParker Date: Fri, 5 Apr 2024 10:10:27 -0500 Subject: [PATCH 1/3] Attempt creating a reproduction for #183 --- sample/Sample/Program.cs | 28 ++++++++++++++++++++++++++++ sample/splunk/Dockerfile | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/sample/Sample/Program.cs b/sample/Sample/Program.cs index 459c3a7..2038e5b 100644 --- a/sample/Sample/Program.cs +++ b/sample/Sample/Program.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Configuration; using Serilog; +using Serilog.Events; using Serilog.Sinks.Splunk; using System; using System.Collections.Generic; @@ -54,6 +55,11 @@ public static async Task Main(string[] args) await Task.Delay(1000); } + logger.Information("Creating logger {MethodName}.", nameof(ReproduceGitHubIssue183)); + ReproduceGitHubIssue183(); + + return; + logger.Information("Creating logger {MethodName}.", nameof(OverridingSubsecondPrecisionMicroseconds)); OverridingSubsecondPrecisionMicroseconds(eventsToCreate); @@ -368,6 +374,28 @@ public static void AddCustomFields(int eventsToCreate) Log.CloseAndFlush(); } + + public static void ReproduceGitHubIssue183() + { + // Override Source + var loggerConfig = new LoggerConfiguration() + .MinimumLevel.Verbose() + .WriteTo.EventCollector( + SPLUNK_ENDPOINT, + Program.EventCollectorToken, + restrictedToMinimumLevel: LogEventLevel.Debug); + + using (var logger = loggerConfig.CreateLogger()) + { + Log.Logger = logger; + + logger.Information("Information message {@param}", new { Property1 = 1, Property2 = 2 }); + logger.Warning("Warning message {@param}", "Hello this is a string"); + logger.Error(new Exception("Bang"), "Error message"); + } + + Log.CloseAndFlush(); + } } diff --git a/sample/splunk/Dockerfile b/sample/splunk/Dockerfile index f006f84..5885de5 100644 --- a/sample/splunk/Dockerfile +++ b/sample/splunk/Dockerfile @@ -1,2 +1,2 @@ -FROM splunk/splunk:latest +FROM splunk/splunk:7.3.1 ADD sample/splunk/etc ${SPLUNK_HOME}/etc \ No newline at end of file From 5375dcf01947f67c23630bbeb1ea53cfa8973a5c Mon Sep 17 00:00:00 2001 From: EEParker Date: Fri, 5 Apr 2024 13:45:37 -0500 Subject: [PATCH 2/3] Add unit tests for local timezones --- .../Sinks/Splunk/Epoch.cs | 2 +- .../EpochExtensionsTests.cs | 41 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs b/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs index 1d85a2d..23a783b 100644 --- a/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs +++ b/src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs @@ -41,7 +41,7 @@ public static string ToEpoch(this DateTimeOffset value, SubSecondPrecision subSe // or Monday, June 1, 2015, at 7:50:55 PM GMT. // See: https://docs.splunk.com/Documentation/Splunk/9.2.0/SearchReference/Commontimeformatvariables - var totalSeconds = ToSeconds(value.Ticks - Epoch.Ticks); + var totalSeconds = ToSeconds(value.ToUniversalTime().Ticks - Epoch.Ticks); var format = subSecondPrecision switch { SubSecondPrecision.Nanoseconds => NanosecondFormat, SubSecondPrecision.Microseconds => MicrosecondFormat, diff --git a/test/Serilog.Sinks.Splunk.Tests/EpochExtensionsTests.cs b/test/Serilog.Sinks.Splunk.Tests/EpochExtensionsTests.cs index 949905d..8bbfca5 100644 --- a/test/Serilog.Sinks.Splunk.Tests/EpochExtensionsTests.cs +++ b/test/Serilog.Sinks.Splunk.Tests/EpochExtensionsTests.cs @@ -7,6 +7,47 @@ namespace Serilog.Sinks.Splunk.Tests { public class EpochExtensionsTests { + [Theory] + [InlineData(1)] + [InlineData(2)] + [InlineData(3)] + [InlineData(4)] + [InlineData(5)] + [InlineData(6)] + [InlineData(7)] + [InlineData(8)] + [InlineData(9)] + [InlineData(10)] + [InlineData(11)] + [InlineData(12)] + [InlineData(13)] + [InlineData(14)] + [InlineData(-1)] + [InlineData(-2)] + [InlineData(-3)] + [InlineData(-4)] + [InlineData(-5)] + [InlineData(-6)] + [InlineData(-7)] + [InlineData(-7.5)] + [InlineData(-8)] + [InlineData(-9)] + [InlineData(-10)] + [InlineData(-11)] + public void ToEpochLocalTime_ShouldReturnCorrectEpochTime(float timeZoneOffset) + { + // Arrange + var dateTimeOffset = new DateTimeOffset(2022, 1, 1, 0, 0, 0, TimeSpan.FromHours(timeZoneOffset)).AddHours(timeZoneOffset); + var expectedEpochTime = "1640995200.000"; // Epoch time for 2022-01-01 00:00:00 + + // Act + var result = dateTimeOffset.ToEpoch(); + + // Assert + Assert.Equal(expectedEpochTime, result); + } + + [Fact] public void ToEpoch_ShouldReturnCorrectEpochTime() { From eaecf9375ab87b58ef0999ed484e762ea5a9f053 Mon Sep 17 00:00:00 2001 From: EEParker Date: Fri, 5 Apr 2024 13:46:09 -0500 Subject: [PATCH 3/3] Reset docker version --- sample/splunk/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/splunk/Dockerfile b/sample/splunk/Dockerfile index 5885de5..f006f84 100644 --- a/sample/splunk/Dockerfile +++ b/sample/splunk/Dockerfile @@ -1,2 +1,2 @@ -FROM splunk/splunk:7.3.1 +FROM splunk/splunk:latest ADD sample/splunk/etc ${SPLUNK_HOME}/etc \ No newline at end of file