Skip to content

Commit

Permalink
Bugfix/183 no logs (#185)
Browse files Browse the repository at this point in the history
* Attempt creating a reproduction for #183

* Add unit tests for local timezones

* Reset docker version
  • Loading branch information
EEParker authored Apr 5, 2024
1 parent aaa9e96 commit ae00946
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
28 changes: 28 additions & 0 deletions sample/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Splunk;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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));

Check warning on line 63 in sample/Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Unreachable code detected
OverridingSubsecondPrecisionMicroseconds(eventsToCreate);

Expand Down Expand Up @@ -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();
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.Sinks.Splunk/Sinks/Splunk/Epoch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 41 additions & 0 deletions test/Serilog.Sinks.Splunk.Tests/EpochExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit ae00946

Please sign in to comment.