JSON-related helper classes, for use by the other MetaBrainz.*
packages.
The JsonUtils
class provides a TraceSource
that can be used to
configure debug output; its name is MetaBrainz.Common.JsonUtils
.
In code, you can enable tracing like follows:
// Use the default switch, turning it on.
JsonUtils.TraceSource.Switch.Level = SourceLevels.All;
// Alternatively, use your own switch so multiple things can be
// enabled/disabled at the same time.
var mySwitch = new TraceSwitch("MyAppDebugSwitch", "All");
JsonUtils.TraceSource.Switch = mySwitch;
// By default, there is a single listener that writes trace events to
// the debug output (typically only seen in an IDE's debugger). You can
// add (and remove) listeners as desired.
var listener = new ConsoleTraceListener {
Name = "MyAppConsole",
TraceOutputOptions = TraceOptions.DateTime | TraceOptions.ProcessId,
};
JsonUtils.TraceSource.Listeners.Clear();
JsonUtils.TraceSource.Listeners.Add(listener);
Starting from .NET 7 your application can also be set up to read tracing configuration from the application configuration file. To do so, the application needs to add the following to its startup code:
System.Diagnostics.TraceConfiguration.Register();
(Provided by the System.Configuration.ConfigurationManager
package.)
The application config file can then have a system.diagnostics
section
where sources, switches and listeners can be configured.
<configuration>
<system.diagnostics>
<sharedListeners>
<add name="console" type="System.Diagnostics.ConsoleTraceListener" traceOutputOptions="DateTime,ProcessId" />
</sharedListeners>
<sources>
<source name="MetaBrainz.Common.JsonUtils" switchName="MetaBrainz.Common.JsonUtils">
<listeners>
<add name="console" />
<add name="json-log" type="System.Diagnostics.TextWriterTraceListener" initializeData="json-utils.log" />
</listeners>
</source>
</sources>
<switches>
<add name="MetaBrainz.Common.JsonUtils" value="All" />
</switches>
</system.diagnostics>
</configuration>
These are available on GitHub.