IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
DiagnosticChannelFormatter.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3
5
7{
8
9 abstract class DiagnosticChannelFormatter<TOptions> : IDiagnosticFormatter, IDisposable
11 {
12
13 readonly TOptions _options;
14
19 public DiagnosticChannelFormatter(TOptions options)
20 {
21 _options = options ?? throw new ArgumentNullException(nameof(options));
22 }
23
25 public void Write(in DiagnosticEvent @event)
26 {
27 // find the writer for the event's level
28 var channel = @event.Diagnostic.Level switch
29 {
30 DiagnosticLevel.Trace => _options.TraceChannel,
31 DiagnosticLevel.Info => _options.InfoChannel,
32 DiagnosticLevel.Warning => _options.WarningChannel,
33 DiagnosticLevel.Error => _options.ErrorChannel,
34 DiagnosticLevel.Fatal => _options.FatalChannel,
35 _ => throw new InvalidOperationException(),
36 };
37
38 // no channel found for level
39 if (channel == null)
40 return;
41
42 WriteImpl(@event, channel);
43 }
44
50 protected abstract void WriteImpl(in DiagnosticEvent @event, IDiagnosticChannel channel);
51
55 public virtual void Dispose()
56 {
57 var hs = new HashSet<IDisposable>();
58
59 if (_options.TraceChannel is IDisposable trace && hs.Add(trace))
60 trace.Dispose();
61 if (_options.InfoChannel is IDisposable info && hs.Add(info))
62 info.Dispose();
63 if (_options.WarningChannel is IDisposable warning && hs.Add(warning))
64 warning.Dispose();
65 if (_options.ErrorChannel is IDisposable error && hs.Add(error))
66 error.Dispose();
67 if (_options.FatalChannel is IDisposable fatal && hs.Add(fatal))
68 fatal.Dispose();
69 }
70
71 }
72
73}
DiagnosticChannelFormatter(TOptions options)
Initializes a new instance.
void WriteImpl(in DiagnosticEvent @event, IDiagnosticChannel channel)
Implement this method to write the diagnostic event to the channel.
void Write(in DiagnosticEvent @event)
Writes the event.
Handles consuming and outputting diagnostic events.