File tree 3 files changed +31
-0
lines changed
src/Serilog.Formatting.Compact.Reader
test/Serilog.Formatting.Compact.Reader.Tests
3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -102,6 +102,27 @@ public bool TryRead([NotNullWhen(true)] out LogEvent? evt)
102
102
return ParseLine ( line ) ;
103
103
}
104
104
105
+ #if FEATURE_READ_LINE_ASYNC_CANCELLATION
106
+ /// <inheritdoc cref="TryReadAsync()" />
107
+ /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
108
+ public async Task < LogEvent ? > TryReadAsync ( CancellationToken cancellationToken )
109
+ {
110
+ var line = await _text . ReadLineAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
111
+ _lineNumber ++ ;
112
+ while ( string . IsNullOrWhiteSpace ( line ) )
113
+ {
114
+ if ( line == null )
115
+ {
116
+ return null ;
117
+ }
118
+ line = await _text . ReadLineAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
119
+ _lineNumber ++ ;
120
+ }
121
+
122
+ return ParseLine ( line ) ;
123
+ }
124
+ #endif
125
+
105
126
/// <summary>
106
127
/// Read a single log event from a JSON-encoded document.
107
128
/// </summary>
Original file line number Diff line number Diff line change 28
28
<ImplicitUsings >enable</ImplicitUsings >
29
29
</PropertyGroup >
30
30
31
+ <PropertyGroup Condition =" '$(TargetFramework)' == 'net8.0' " >
32
+ <DefineConstants >$(DefineConstants);FEATURE_READ_LINE_ASYNC_CANCELLATION</DefineConstants >
33
+ </PropertyGroup >
34
+
31
35
<ItemGroup >
32
36
<PackageReference Include =" Newtonsoft.Json" Version =" 13.0.3" />
33
37
<PackageReference Include =" Serilog" Version =" 4.0.0" />
Original file line number Diff line number Diff line change 4
4
using System ;
5
5
using System . Collections . Generic ;
6
6
using System . IO ;
7
+ using System . Threading ;
7
8
using System . Threading . Tasks ;
8
9
using Xunit ;
9
10
@@ -157,6 +158,11 @@ public async Task InvalidDataThrowsInvalidDataException(string document)
157
158
using var asyncReader = new LogEventReader ( new StringReader ( document ) ) ;
158
159
await Assert . ThrowsAsync < InvalidDataException > ( asyncReader . TryReadAsync ) ;
159
160
161
+ #if NET7_0_OR_GREATER
162
+ using var asyncReader2 = new LogEventReader ( new StringReader ( document ) ) ;
163
+ await Assert . ThrowsAsync < InvalidDataException > ( async ( ) => await asyncReader2 . TryReadAsync ( CancellationToken . None ) ) ;
164
+ #endif
165
+
160
166
Assert . Throws < InvalidDataException > ( ( ) => LogEventReader . ReadFromString ( document ) ) ;
161
167
}
162
168
}
You can’t perform that action at this time.
0 commit comments