diff --git a/appveyor.yml b/appveyor.yml index ba62cb76..423bb86b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -31,7 +31,7 @@ for: - provider: NuGet api_key: - secure: +WuB7C/ehQ955aj7fus2FFZWhtJ8/SSxViv+xmtlV2GkYyMl3B7rhhavBgNORxCt + secure: V+MlStLrFQQKjtbJQQ9RDTGD2VPlPJaQhkXayCO3AM4dWBKf5/fQWJgStZyK6Tx3 skip_symbols: true artifact: /seqcli\..*\.nupkg/ on: diff --git a/global.json b/global.json index f2068dc8..1ee82c37 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "8.0.101" + "version": "8.0.203" } -} \ No newline at end of file +} diff --git a/src/SeqCli/Cli/Features/OutputFormatFeature.cs b/src/SeqCli/Cli/Features/OutputFormatFeature.cs index 3f16eb7b..8ae28432 100644 --- a/src/SeqCli/Cli/Features/OutputFormatFeature.cs +++ b/src/SeqCli/Cli/Features/OutputFormatFeature.cs @@ -14,7 +14,6 @@ using System; using System.Collections.Generic; -using Destructurama; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using Newtonsoft.Json.Linq; @@ -22,6 +21,7 @@ using SeqCli.Config; using SeqCli.Csv; using SeqCli.Output; +using SeqCli.Util; using Serilog; using Serilog.Core; using Serilog.Events; @@ -122,7 +122,7 @@ public void WriteEntity(Entity entity) // way to write colorized JSON ;) var writer = new LoggerConfiguration() - .Destructure.JsonNetTypes() + .Destructure.With() .Enrich.With() .WriteTo.Console( outputTemplate: "{@Message:j}{NewLine}", diff --git a/src/SeqCli/SeqCli.csproj b/src/SeqCli/SeqCli.csproj index 36234e7b..69b925f7 100644 --- a/src/SeqCli/SeqCli.csproj +++ b/src/SeqCli/SeqCli.csproj @@ -27,7 +27,6 @@ - @@ -39,7 +38,7 @@ - + diff --git a/src/SeqCli/Util/JsonNetDestructuringPolicy.cs b/src/SeqCli/Util/JsonNetDestructuringPolicy.cs new file mode 100644 index 00000000..8d9bf7bc --- /dev/null +++ b/src/SeqCli/Util/JsonNetDestructuringPolicy.cs @@ -0,0 +1,91 @@ +// Copyright 2015 Destructurama Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using Newtonsoft.Json.Linq; +using Serilog.Core; +using Serilog.Events; + +namespace SeqCli.Util; + +sealed class JsonNetDestructuringPolicy : IDestructuringPolicy +{ + public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, [NotNullWhen(true)] 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; + } + + static LogEventPropertyValue Destructure(JValue jv, ILogEventPropertyValueFactory propertyValueFactory) + { + return propertyValueFactory.CreatePropertyValue(jv.Value!, destructureObjects: true); + } + + static SequenceValue Destructure(JArray ja, ILogEventPropertyValueFactory propertyValueFactory) + { + var elems = ja.Select(t => propertyValueFactory.CreatePropertyValue(t, destructureObjects: true)); + return new SequenceValue(elems); + } + + static LogEventPropertyValue Destructure(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) + { + string? typeTag = null; + var props = new List(jo.Count); + + foreach (var prop in jo.Properties()) + { + if (prop.Name == "$type") + { + if (prop.Value is JValue typeVal && typeVal.Value is string v) + { + typeTag = v; + continue; + } + } + else if (!LogEventProperty.IsValidName(prop.Name)) + { + return DestructureToDictionaryValue(jo, propertyValueFactory); + } + + props.Add(new LogEventProperty(prop.Name, propertyValueFactory.CreatePropertyValue(prop.Value, destructureObjects: true))); + } + + return new StructureValue(props, typeTag); + } + + static DictionaryValue DestructureToDictionaryValue(JObject jo, ILogEventPropertyValueFactory propertyValueFactory) + { + var elements = jo.Properties().Select( + prop => new KeyValuePair( + new ScalarValue(prop.Name), + propertyValueFactory.CreatePropertyValue(prop.Value, destructureObjects: true)) + ); + return new DictionaryValue(elements); + } +} \ No newline at end of file