diff --git a/src/Squidex.Infrastructure/Log/JsonLogWriter.cs b/src/Squidex.Infrastructure/Log/JsonLogWriter.cs index df5d950c8..13b0c1e91 100644 --- a/src/Squidex.Infrastructure/Log/JsonLogWriter.cs +++ b/src/Squidex.Infrastructure/Log/JsonLogWriter.cs @@ -7,137 +7,136 @@ using System; using System.IO; -using Newtonsoft.Json; +using System.Text; +using System.Text.Json; using NodaTime; namespace Squidex.Infrastructure.Log { public sealed class JsonLogWriter : IObjectWriter, IArrayWriter { - private readonly Formatting formatting; + private readonly JsonWriterOptions formatting; private readonly bool formatLine; - private readonly StringWriter textWriter = new StringWriter(); - private JsonWriter jsonWriter; + private readonly MemoryStream stream = new MemoryStream(); + private readonly StreamReader streamReader; + private Utf8JsonWriter jsonWriter; - public int BufferSize + public long BufferSize { - get { return textWriter.GetStringBuilder().Capacity; } + get { return stream.Length; } } - internal JsonLogWriter(Formatting formatting, bool formatLine) + internal JsonLogWriter(JsonWriterOptions formatting, bool formatLine) { this.formatLine = formatLine; this.formatting = formatting; + streamReader = new StreamReader(stream, Encoding.UTF8); + Start(); } private void Start() { - jsonWriter = new JsonTextWriter(textWriter) { Formatting = formatting }; + jsonWriter = new Utf8JsonWriter(stream, formatting); jsonWriter.WriteStartObject(); } internal void Reset() { - textWriter.GetStringBuilder().Clear(); + stream.Position = 0; + stream.SetLength(0); Start(); } IArrayWriter IArrayWriter.WriteValue(string value) { - jsonWriter.WriteValue(value); + jsonWriter.WriteStringValue(value); return this; } IArrayWriter IArrayWriter.WriteValue(double value) { - jsonWriter.WriteValue(value); + jsonWriter.WriteNumberValue(value); return this; } IArrayWriter IArrayWriter.WriteValue(long value) { - jsonWriter.WriteValue(value); + jsonWriter.WriteNumberValue(value); return this; } IArrayWriter IArrayWriter.WriteValue(bool value) { - jsonWriter.WriteValue(value); + jsonWriter.WriteBooleanValue(value); return this; } IArrayWriter IArrayWriter.WriteValue(Instant value) { - jsonWriter.WriteValue(value.ToString()); + jsonWriter.WriteStringValue(value.ToString()); return this; } IArrayWriter IArrayWriter.WriteValue(TimeSpan value) { - jsonWriter.WriteValue(value.ToString()); + jsonWriter.WriteStringValue(value.ToString()); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, string value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value); + jsonWriter.WriteString(property, value); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, double value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value); + jsonWriter.WriteNumber(property, value); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, long value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value); + jsonWriter.WriteNumber(property, value); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, bool value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value); + jsonWriter.WriteBoolean(property, value); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, Instant value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value.ToString()); + jsonWriter.WriteString(property, value.ToString()); return this; } IObjectWriter IObjectWriter.WriteProperty(string property, TimeSpan value) { - jsonWriter.WritePropertyName(Format(property)); - jsonWriter.WriteValue(value.ToString()); + jsonWriter.WriteString(property, value.ToString()); return this; } IObjectWriter IObjectWriter.WriteObject(string property, Action objectWriter) { - jsonWriter.WritePropertyName(Format(property)); + jsonWriter.WritePropertyName(property); jsonWriter.WriteStartObject(); objectWriter?.Invoke(this); @@ -149,7 +148,7 @@ namespace Squidex.Infrastructure.Log IObjectWriter IObjectWriter.WriteObject(string property, T context, Action objectWriter) { - jsonWriter.WritePropertyName(Format(property)); + jsonWriter.WritePropertyName(property); jsonWriter.WriteStartObject(); objectWriter?.Invoke(context, this); @@ -161,7 +160,7 @@ namespace Squidex.Infrastructure.Log IObjectWriter IObjectWriter.WriteArray(string property, Action arrayWriter) { - jsonWriter.WritePropertyName(Format(property)); + jsonWriter.WritePropertyName(property); jsonWriter.WriteStartArray(); arrayWriter?.Invoke(this); @@ -173,7 +172,7 @@ namespace Squidex.Infrastructure.Log IObjectWriter IObjectWriter.WriteArray(string property, T context, Action arrayWriter) { - jsonWriter.WritePropertyName(Format(property)); + jsonWriter.WritePropertyName(property); jsonWriter.WriteStartArray(); arrayWriter?.Invoke(context, this); @@ -205,21 +204,22 @@ namespace Squidex.Infrastructure.Log return this; } - private static string Format(string property) - { - return property; - } - public override string ToString() { jsonWriter.WriteEndObject(); + jsonWriter.Flush(); + + stream.Position = 0; + streamReader.DiscardBufferedData(); + + var json = streamReader.ReadToEnd(); if (formatLine) { - textWriter.WriteLine(); + json += Environment.NewLine; } - return textWriter.ToString(); + return json; } } } diff --git a/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs b/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs index 7ff85b6ea..f499d0b01 100644 --- a/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs +++ b/src/Squidex.Infrastructure/Log/JsonLogWriterFactory.cs @@ -6,7 +6,7 @@ // ========================================================================== using System.Collections.Concurrent; -using Newtonsoft.Json; +using System.Text.Json; namespace Squidex.Infrastructure.Log { @@ -15,12 +15,13 @@ namespace Squidex.Infrastructure.Log private const int MaxPoolSize = 10; private const int MaxCapacity = 5000; private readonly ConcurrentStack pool = new ConcurrentStack(); - private readonly Formatting formatting; + private readonly JsonWriterOptions formatting; private readonly bool formatLine; - public JsonLogWriterFactory(Formatting formatting = Formatting.None, bool formatLine = false) + public JsonLogWriterFactory(bool indended = false, bool formatLine = false) { - this.formatting = formatting; + formatting.Indented = indended; + this.formatLine = formatLine; } @@ -31,7 +32,7 @@ namespace Squidex.Infrastructure.Log public static JsonLogWriterFactory Readable() { - return new JsonLogWriterFactory(Formatting.Indented, true); + return new JsonLogWriterFactory(true, true); } public IObjectWriter Create() diff --git a/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj b/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj index d83357913..aa339402d 100644 --- a/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj +++ b/src/Squidex.Infrastructure/Squidex.Infrastructure.csproj @@ -32,6 +32,7 @@ + diff --git a/src/Squidex/Config/Logging.cs b/src/Squidex/Config/Logging.cs index 51c6a4ce9..df98f9edc 100644 --- a/src/Squidex/Config/Logging.cs +++ b/src/Squidex/Config/Logging.cs @@ -18,6 +18,11 @@ namespace Squidex.Config { builder.AddFilter((category, level) => { + if (level < LogLevel.Information) + { + return false; + } + if (category.StartsWith("Orleans.", StringComparison.OrdinalIgnoreCase)) { var subCategory = category.AsSpan().Slice(8); @@ -55,7 +60,7 @@ namespace Squidex.Config return true; } #endif - return level >= LogLevel.Information; + return true; }); } } diff --git a/tests/Squidex.Infrastructure.Tests/Log/JsonLogWriterTests.cs b/tests/Squidex.Infrastructure.Tests/Log/JsonLogWriterTests.cs index c8ff22a92..364a6d3a5 100644 --- a/tests/Squidex.Infrastructure.Tests/Log/JsonLogWriterTests.cs +++ b/tests/Squidex.Infrastructure.Tests/Log/JsonLogWriterTests.cs @@ -159,7 +159,7 @@ namespace Squidex.Infrastructure.Log [Fact] public void Should_write_pretty_json() { - var prettySut = new JsonLogWriterFactory(Formatting.Indented).Create(); + var prettySut = new JsonLogWriterFactory(true).Create(); var result = prettySut.WriteProperty("property", 1.5).ToString(); @@ -169,7 +169,7 @@ namespace Squidex.Infrastructure.Log [Fact] public void Should_write_extra_line_after_object() { - var prettySut = new JsonLogWriterFactory(Formatting.None, true).Create(); + var prettySut = new JsonLogWriterFactory(false, true).Create(); var result = prettySut.WriteProperty("property", 1.5).ToString();