1+ using System . Runtime . CompilerServices ;
12using JsonApiDotNetCore . Resources . Annotations ;
23using JsonApiDotNetCore . Serialization . Objects ;
34using Microsoft . Extensions . Logging ;
@@ -87,7 +88,7 @@ private static string GetSchemaTypeName(Type type)
8788
8889 private sealed partial class SchemaGenerationTraceScope : ISchemaGenerationTraceScope
8990 {
90- private static readonly AsyncLocal < int > RecursionDepthAsyncLocal = new ( ) ;
91+ private static readonly AsyncLocal < StrongBox < int > > RecursionDepthAsyncLocal = new ( ) ;
9192
9293 private readonly ILogger _logger ;
9394 private readonly string _schemaTypeName ;
@@ -101,8 +102,10 @@ public SchemaGenerationTraceScope(ILogger logger, string schemaTypeName)
101102 _logger = logger ;
102103 _schemaTypeName = schemaTypeName ;
103104
104- RecursionDepthAsyncLocal . Value ++ ;
105- LogStarted ( RecursionDepthAsyncLocal . Value , _schemaTypeName ) ;
105+ RecursionDepthAsyncLocal . Value ??= new StrongBox < int > ( 0 ) ;
106+ int depth = Interlocked . Increment ( ref RecursionDepthAsyncLocal . Value . Value ) ;
107+
108+ LogStarted ( depth , _schemaTypeName ) ;
106109 }
107110
108111 public void TraceSucceeded ( string schemaId )
@@ -112,16 +115,18 @@ public void TraceSucceeded(string schemaId)
112115
113116 public void Dispose ( )
114117 {
118+ int depth = RecursionDepthAsyncLocal . Value ! . Value ;
119+
115120 if ( _schemaId != null )
116121 {
117- LogSucceeded ( RecursionDepthAsyncLocal . Value , _schemaTypeName , _schemaId ) ;
122+ LogSucceeded ( depth , _schemaTypeName , _schemaId ) ;
118123 }
119124 else
120125 {
121- LogFailed ( RecursionDepthAsyncLocal . Value , _schemaTypeName ) ;
126+ LogFailed ( depth , _schemaTypeName ) ;
122127 }
123128
124- RecursionDepthAsyncLocal . Value -- ;
129+ Interlocked . Decrement ( ref RecursionDepthAsyncLocal . Value . Value ) ;
125130 }
126131
127132 [ LoggerMessage ( Level = LogLevel . Trace , SkipEnabledCheck = true , Message = "({Depth:D2}) Started for {SchemaTypeName}." ) ]
0 commit comments