-
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.
Added serilog destructring for newtonsoft, sjt and nettolpologysuite (#…
- Loading branch information
1 parent
b7b7ed7
commit 2b52094
Showing
49 changed files
with
1,045 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -207,4 +207,5 @@ src/Cake.Scripts/tools/tools/ | |
codealike.json | ||
|
||
# Rider | ||
.idea/ | ||
.idea/ | ||
*.received.* |
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
76 changes: 76 additions & 0 deletions
76
src/Foundation.NewtonsoftJson/NewtonsoftJsonDestructuringPolicy.cs
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,76 @@ | ||
using Newtonsoft.Json.Linq; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
internal class NewtonsoftJsonDestructuringPolicy : IDestructuringPolicy | ||
{ | ||
private static LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory) | ||
{ | ||
return propertyValueFactory.CreatePropertyValue(jv.Value, true); | ||
} | ||
|
||
private static LogEventPropertyValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory) | ||
{ | ||
var elems = ja.Select(t => propertyValueFactory.CreatePropertyValue(t, true)); | ||
return new SequenceValue(elems); | ||
} | ||
|
||
private static LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) | ||
{ | ||
string typeTag = null; | ||
var props = new List<LogEventProperty>(jo.Count); | ||
|
||
foreach (var prop in jo.Properties()) | ||
{ | ||
if (prop.Name == "$type") | ||
{ | ||
if (prop.Value is JValue typeVal && typeVal.Value is string) | ||
{ | ||
typeTag = (string)typeVal.Value; | ||
continue; | ||
} | ||
} | ||
else if (!LogEventProperty.IsValidName(prop.Name)) | ||
{ | ||
return DestructureToDictionaryValue(jo, propertyValueFactory); | ||
} | ||
|
||
props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, true))); | ||
} | ||
|
||
return new StructureValue(props, typeTag); | ||
} | ||
|
||
private static LogEventPropertyValue DestructureToDictionaryValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) | ||
{ | ||
var elements = jo.Properties().Select( | ||
prop => | ||
new KeyValuePair<ScalarValue, LogEventPropertyValue>( | ||
new ScalarValue(prop.Name), | ||
propertyValueFactory.CreatePropertyValue(prop.Value, true) | ||
) | ||
); | ||
return new DictionaryValue(elements); | ||
} | ||
|
||
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result) | ||
{ | ||
switch (value) | ||
{ | ||
case JObject jo: | ||
result = Destructure(jo, propertyValueFactory); | ||
return true; | ||
case JArray ja: | ||
result = Destructure(ja, propertyValueFactory); | ||
return true; | ||
case JValue jv: | ||
result = Destructure(jv, propertyValueFactory); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Foundation.NewtonsoftJson/NewtonsoftJsonLoggerConfigurationExtensions.cs
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,20 @@ | ||
using Serilog; | ||
using Serilog.Configuration; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
/// <summary> | ||
/// Adds the Destructure.NewtonsoftJsonTypes() extension to <see cref="LoggerConfiguration" />. | ||
/// </summary> | ||
public static class NewtonsoftJsonLoggerConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Enable destructuring of JSON.NET dynamic objects. | ||
/// </summary> | ||
/// <param name="configuration">The logger configuration to apply configuration to.</param> | ||
/// <returns>An object allowing configuration to continue.</returns> | ||
public static LoggerConfiguration NewtonsoftJsonTypes(this LoggerDestructuringConfiguration configuration) | ||
{ | ||
return configuration.With<NewtonsoftJsonDestructuringPolicy>(); | ||
} | ||
} |
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,40 @@ | ||
using System.Text.Json; | ||
using Serilog.Core; | ||
using Serilog.Events; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
internal class SystemTextJsonDestructuringPolicy : IDestructuringPolicy | ||
{ | ||
private static LogEventPropertyValue Destructure(in JsonElement jel) | ||
{ | ||
return jel.ValueKind switch | ||
{ | ||
JsonValueKind.Array => new SequenceValue(jel.EnumerateArray().Select(ae => Destructure(in ae))), | ||
JsonValueKind.False => new ScalarValue(false), | ||
JsonValueKind.True => new ScalarValue(true), | ||
JsonValueKind.Null => new ScalarValue(null), | ||
JsonValueKind.Undefined => new ScalarValue(null), | ||
JsonValueKind.Number => new ScalarValue(jel.GetDecimal()), | ||
JsonValueKind.String => new ScalarValue(jel.GetString()), | ||
JsonValueKind.Object => new StructureValue(jel.EnumerateObject().Select(jp => new LogEventProperty(jp.Name, Destructure(jp.Value)))), | ||
_ => throw new ArgumentException("Unrecognized value kind " + jel.ValueKind + ".") | ||
}; | ||
} | ||
|
||
public bool TryDestructure(object value, ILogEventPropertyValueFactory _, out LogEventPropertyValue? result) | ||
{ | ||
switch (value) | ||
{ | ||
case JsonDocument jdoc: | ||
result = Destructure(jdoc.RootElement); | ||
return true; | ||
case JsonElement jel: | ||
result = Destructure(jel); | ||
return true; | ||
} | ||
|
||
result = null; | ||
return false; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/Foundation/SystemTextJsonLoggerConfigurationExtensions.cs
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,20 @@ | ||
using Serilog; | ||
using Serilog.Configuration; | ||
|
||
namespace Rocket.Surgery.LaunchPad.Foundation; | ||
|
||
/// <summary> | ||
/// Adds the Destructure.NewtonsoftJsonTypes() extension to <see cref="LoggerConfiguration" />. | ||
/// </summary> | ||
public static class SystemTextJsonLoggerConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Enable destructuring of JSON.NET dynamic objects. | ||
/// </summary> | ||
/// <param name="configuration">The logger configuration to apply configuration to.</param> | ||
/// <returns>An object allowing configuration to continue.</returns> | ||
public static LoggerConfiguration SystemTextJsonTypes(this LoggerDestructuringConfiguration configuration) | ||
{ | ||
return configuration.With<SystemTextJsonDestructuringPolicy>(); | ||
} | ||
} |
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
Oops, something went wrong.