File tree 1 file changed +16
-7
lines changed
src/JsonApiDotNetCore/Middleware
1 file changed +16
-7
lines changed Original file line number Diff line number Diff line change 2
2
using System . Reflection ;
3
3
using System . Runtime . CompilerServices ;
4
4
using System . Text ;
5
+ using System . Text . Encodings . Web ;
6
+ using System . Text . Json ;
7
+ using System . Text . Json . Serialization ;
5
8
using Microsoft . Extensions . Logging ;
6
- using Newtonsoft . Json ;
7
9
8
10
namespace JsonApiDotNetCore . Middleware
9
11
{
10
- internal sealed class TraceLogWriter < T >
12
+ internal abstract class TraceLogWriter
13
+ {
14
+ protected static readonly JsonSerializerOptions SerializerOptions = new ( )
15
+ {
16
+ WriteIndented = true ,
17
+ Encoder = JavaScriptEncoder . UnsafeRelaxedJsonEscaping ,
18
+ ReferenceHandler = ReferenceHandler . Preserve
19
+ } ;
20
+ }
21
+
22
+ internal sealed class TraceLogWriter < T > : TraceLogWriter
11
23
{
12
24
private readonly ILogger _logger ;
13
25
@@ -126,12 +138,9 @@ private static string SerializeObject(object value)
126
138
{
127
139
try
128
140
{
129
- // It turns out setting ReferenceLoopHandling to something other than Error only takes longer to fail.
130
- // This is because Newtonsoft.Json always tries to serialize the first element in a graph. And with
131
- // EF Core models, that one is often recursive, resulting in either StackOverflowException or OutOfMemoryException.
132
- return JsonConvert . SerializeObject ( value , Formatting . Indented ) ;
141
+ return JsonSerializer . Serialize ( value , SerializerOptions ) ;
133
142
}
134
- catch ( JsonSerializationException )
143
+ catch ( JsonException )
135
144
{
136
145
// Never crash as a result of logging, this is best-effort only.
137
146
return "object" ;
You can’t perform that action at this time.
0 commit comments