Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SeqCli/Cli/Features/OutputFormatFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@

using System;
using System.Collections.Generic;
using Destructurama;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Seq.Api.Model;
using SeqCli.Config;
using SeqCli.Csv;
using SeqCli.Output;
using SeqCli.Util;
using Serilog;
using Serilog.Core;
using Serilog.Events;
Expand Down Expand Up @@ -122,7 +122,7 @@ public void WriteEntity(Entity entity)
// way to write colorized JSON ;)

var writer = new LoggerConfiguration()
.Destructure.JsonNetTypes()
.Destructure.With<JsonNetDestructuringPolicy>()
.Enrich.With<StripStructureTypeEnricher>()
.WriteTo.Console(
outputTemplate: "{@Message:j}{NewLine}",
Expand Down
3 changes: 1 addition & 2 deletions src/SeqCli/SeqCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Destructurama.JsonNet" Version="2.0.1" />
<PackageReference Include="newtonsoft.json" Version="13.0.3" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Expressions" Version="4.0.0" />
Expand All @@ -39,7 +38,7 @@
<PackageReference Include="Superpower" Version="3.0.0" />
<PackageReference Include="System.Reactive" Version="6.0.0" />
<PackageReference Include="System.Security.Cryptography.ProtectedData" Version="8.0.0" />
<PackageReference Include="Seq.Api" Version="2024.1.0" />
<PackageReference Include="Seq.Api" Version="2024.2.0" />
<PackageReference Include="Seq.Apps" Version="2023.4.0" />
<PackageReference Include="Seq.Syntax" Version="1.0.0" />
<PackageReference Include="Tavis.UriTemplates" Version="2.0.0" />
Expand Down
91 changes: 91 additions & 0 deletions src/SeqCli/Util/JsonNetDestructuringPolicy.cs
Original file line number Diff line number Diff line change
@@ -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<LogEventProperty>(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<ScalarValue, LogEventPropertyValue>(
new ScalarValue(prop.Name),
propertyValueFactory.CreatePropertyValue(prop.Value, destructureObjects: true))
);
return new DictionaryValue(elements);
}
}