-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed support for nodatime time with destructuring (#1175)
- Loading branch information
1 parent
3347bd8
commit 681fd47
Showing
18 changed files
with
308 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System.Text.Json; | ||
using NodaTime; | ||
using NodaTime.Text; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
internal class NodaTimeDestructuringPolicy : IDestructuringPolicy | ||
{ | ||
private readonly ZonedDateTimePattern _zonedDateTimePattern; | ||
|
||
public NodaTimeDestructuringPolicy(IDateTimeZoneProvider provider) | ||
{ | ||
_zonedDateTimePattern = ZonedDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm':'ss;FFFFFFFFFo<G> z", provider); | ||
} | ||
|
||
public bool TryDestructure(object value, ILogEventPropertyValueFactory _, out LogEventPropertyValue? result) | ||
{ | ||
ScalarValue a; | ||
if (value is Instant instant1) | ||
{ | ||
result = new ScalarValue(InstantPattern.ExtendedIso.Format(instant1)); | ||
return true; | ||
} | ||
|
||
if (value is LocalDateTime localDateTime) | ||
{ | ||
result = new ScalarValue(LocalDateTimePattern.ExtendedIso.Format(localDateTime)); | ||
return true; | ||
} | ||
|
||
if (value is LocalDate localDate) | ||
{ | ||
result = new ScalarValue(LocalDatePattern.Iso.Format(localDate)); | ||
return true; | ||
} | ||
|
||
if (value is LocalTime localTime) | ||
{ | ||
result = new ScalarValue(LocalTimePattern.ExtendedIso.Format(localTime)); | ||
return true; | ||
} | ||
|
||
if (value is OffsetDateTime offsetDateTime) | ||
{ | ||
result = new ScalarValue(OffsetDateTimePattern.ExtendedIso.Format(offsetDateTime)); | ||
return true; | ||
} | ||
|
||
if (value is OffsetDate offsetDate) | ||
{ | ||
result = new ScalarValue(OffsetDatePattern.GeneralIso.Format(offsetDate)); | ||
return true; | ||
} | ||
|
||
if (value is OffsetTime offsetTime) | ||
{ | ||
result = new ScalarValue(OffsetTimePattern.ExtendedIso.Format(offsetTime)); | ||
return true; | ||
} | ||
|
||
if (value is ZonedDateTime zonedDateTime) | ||
{ | ||
result = new ScalarValue(_zonedDateTimePattern.Format(zonedDateTime)); | ||
return true; | ||
} | ||
|
||
if (value is DateTimeZone dateTimeZone) | ||
{ | ||
result = new ScalarValue(dateTimeZone.Id); | ||
return true; | ||
} | ||
|
||
if (value is Duration duration) | ||
{ | ||
result = new ScalarValue(DurationPattern.Roundtrip.Format(duration)); | ||
return true; | ||
} | ||
|
||
if (value is Period period) | ||
{ | ||
result = new ScalarValue(PeriodPattern.NormalizingIso.Format(period)); | ||
return true; | ||
} | ||
|
||
if (value is Interval interval) | ||
{ | ||
var values = new List<LogEventProperty>(); | ||
if (interval.HasStart) | ||
{ | ||
values.Add(new LogEventProperty("Start", _.CreatePropertyValue(interval.Start))); | ||
} | ||
|
||
if (interval.HasEnd) | ||
{ | ||
values.Add(new LogEventProperty("End", _.CreatePropertyValue(interval.End))); | ||
} | ||
|
||
result = new StructureValue(values); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using NodaTime; | ||
using Serilog; | ||
using Serilog.Configuration; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
/// <summary> | ||
/// Adds the Destructure.NewtonsoftJsonTypes() extension to <see cref="LoggerConfiguration" />. | ||
/// </summary> | ||
public static class NodaTimeLoggerConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Enable destructuring of JSON.NET dynamic objects. | ||
/// </summary> | ||
/// <param name="configuration">The logger configuration to apply configuration to.</param> | ||
/// <param name="provider"></param> | ||
/// <returns>An object allowing configuration to continue.</returns> | ||
public static LoggerConfiguration NodaTimeTypes(this LoggerDestructuringConfiguration configuration, IDateTimeZoneProvider provider) | ||
{ | ||
return configuration | ||
.AsScalar<Offset>().Destructure | ||
.AsScalar<CalendarSystem>().Destructure | ||
.With(new NodaTimeDestructuringPolicy(provider)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
...snapshots/SerilogDestructuringTests.Should_Destructure_NodaTime_DateTimeZone.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test "America/New_York" | ||
] |
3 changes: 3 additions & 0 deletions
3
...sts/snapshots/SerilogDestructuringTests.Should_Destructure_NodaTime_Duration.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test "1:00:00:00" | ||
] |
3 changes: 3 additions & 0 deletions
3
...ests/snapshots/SerilogDestructuringTests.Should_Destructure_NodaTime_Instant.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test "2022-01-01T04:04:04Z" | ||
] |
3 changes: 3 additions & 0 deletions
3
...sts/snapshots/SerilogDestructuringTests.Should_Destructure_NodaTime_Interval.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test { Start: "2022-01-01T04:04:04Z", End: "2022-01-01T04:04:04Z" } | ||
] |
3 changes: 3 additions & 0 deletions
3
...ts/snapshots/SerilogDestructuringTests.Should_Destructure_NodaTime_LocalDate.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test "2022-01-01" | ||
] |
3 changes: 3 additions & 0 deletions
3
...napshots/SerilogDestructuringTests.Should_Destructure_NodaTime_LocalDateTime.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[ | ||
This is just a test "2022-01-01T04:04:04" | ||
] |
Oops, something went wrong.