|
3 | 3 | using DocoptNet; |
4 | 4 | using Seq.Api; |
5 | 5 | using Serilog; |
6 | | -using System.Reactive.Linq; |
7 | 6 | using Serilog.Formatting.Compact.Reader; |
8 | 7 | using System.Threading; |
9 | | -using Newtonsoft.Json.Linq; |
| 8 | + |
| 9 | +// ReSharper disable AccessToDisposedClosure |
10 | 10 |
|
11 | 11 | const string usage = @"seq-tail: watch a Seq query from your console. |
12 | 12 |
|
|
29 | 29 |
|
30 | 30 | try |
31 | 31 | { |
32 | | - var arguments = new Docopt().Apply(usage, args, version: "Seq Tail 0.2", exit: true)!; |
| 32 | + var arguments = new Docopt().Apply(usage, args, version: "Seq Tail 0.3", exit: true)!; |
33 | 33 |
|
34 | 34 | var server = arguments["<server>"].ToString(); |
35 | 35 | var apiKey = Normalize(arguments["--apikey"]); |
36 | 36 | var filter = Normalize(arguments["--filter"]); |
37 | 37 |
|
38 | | - var cancel = new CancellationTokenSource(); |
| 38 | + using var cts = new CancellationTokenSource(); |
39 | 39 | Console.WriteLine("Tailing, press Ctrl+C to exit."); |
40 | | - Console.CancelKeyPress += (_,_) => cancel.Cancel(); |
| 40 | + Console.CancelKeyPress += (_,_) => cts.Cancel(); |
41 | 41 |
|
42 | | - var run = Task.Run(() => Run(server, apiKey, filter, cancel), cancel.Token); |
| 42 | + var run = Task.Run(() => Run(server, apiKey, filter, cts.Token), cts.Token); |
43 | 43 |
|
44 | 44 | run.GetAwaiter().GetResult(); |
45 | 45 | } |
|
56 | 56 | return string.IsNullOrWhiteSpace(s) ? null : s; |
57 | 57 | } |
58 | 58 |
|
59 | | -static async Task Run(string server, string? apiKey, string? filter, CancellationTokenSource cancel) |
| 59 | +static async Task Run(string server, string? apiKey, string? filter, CancellationToken cancel) |
60 | 60 | { |
61 | 61 | var connection = new SeqConnection(server, apiKey); |
62 | 62 |
|
63 | | - string? strict = null; |
| 63 | + string? strictFilter = null; |
64 | 64 | if (filter != null) |
65 | 65 | { |
66 | 66 | var converted = await connection.Expressions.ToStrictAsync(filter); |
67 | | - strict = converted.StrictExpression; |
| 67 | + strictFilter = converted.StrictExpression; |
68 | 68 | } |
69 | 69 |
|
70 | | - using var stream = await connection.Events.StreamAsync<JObject>(filter: strict); |
71 | | - var subscription = stream |
72 | | - .Select(LogEventReader.ReadFromJObject) |
73 | | - .Subscribe(Log.Write, cancel.Cancel); |
74 | | - |
75 | | - cancel.Token.WaitHandle.WaitOne(); |
76 | | - subscription.Dispose(); |
| 70 | + await foreach (var evt in connection.Events.StreamDocumentsAsync(filter: strictFilter, clef: true, cancellationToken: cancel)) |
| 71 | + { |
| 72 | + var logEvent = LogEventReader.ReadFromString(evt); |
| 73 | + Log.Write(logEvent); |
| 74 | + } |
77 | 75 | } |
0 commit comments