System.Diagnostics.Tracer 2.1.0-alpha

This is a prerelease version of System.Diagnostics.Tracer.
dotnet add package System.Diagnostics.Tracer --version 2.1.0-alpha
NuGet\Install-Package System.Diagnostics.Tracer -Version 2.1.0-alpha
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="System.Diagnostics.Tracer" Version="2.1.0-alpha" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add System.Diagnostics.Tracer --version 2.1.0-alpha
#r "nuget: System.Diagnostics.Tracer, 2.1.0-alpha"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install System.Diagnostics.Tracer as a Cake Addin
#addin nuget:?package=System.Diagnostics.Tracer&version=2.1.0-alpha&prerelease

// Install System.Diagnostics.Tracer as a Cake Tool
#tool nuget:?package=System.Diagnostics.Tracer&version=2.1.0-alpha&prerelease

System.Diagnostics.Tracer

An improved API on top of System.Diagnostics

Adds support for dynamic runtime configuration of trace sources, hierarchical trace sources and testability.

Usage

Just like for TraceSource, the recommended way to retrieve a tracer for your code is to do so via a static readonly field:

public class MyClass {
    static readonly ITracer tracer = Tracer.Get<MyClass>();        

    public MyClass() {
        tracer.Info("Initialized my class!");
    }
}

Built-in support for using Service Trace Viewer, including activity tracing:

using (var activity = tracer.StartActivity("Uploading")) {
    tracer.Info("10% done");
    ...
}

When exceptions are traced, any additional data in the Exception.Data dictionary is also exposed to the viewer.

Configuration

One of the key benefits of leveraging the Tracer is that configuration can happen dynamically at run-time, via the Tracer.Configuration, such as setting trace levels and adding/removing trace listeners:

// Add a manually configured trace listener to the entire 'Contoso' system
Tracer.Configuration.AddListener ("Contoso", new XmlWriterTraceListener(xml));

// Tune up logging for an entire sub-system
Tracer.Configuration.SetTracingLevel ("Contoso.Payments", SourceLevels.All);

// Get the underlying TraceSource if needed
TraceSource source = Tracer.Configuration.GetSource("Contoso");

// Turn on logging for all sources retrieved via Tracer
Tracer.Configuration.SetTracingLevel ("*", SourceLevels.Warning);

The wildcard trace source allows system-wide configuration of sources retrieved via the Tracer, since all hierarchical sources inherit from it automatically.

Why

The System.Diagnostics.TraceSource is almost enough for trivial tracing. It's what .NET itself uses all over the place even. There are a few shortcomings though:

  • Run-time configuration: can't change trace level and add/remove listeners unless the specific TraceSource instance used by the code is exposed directly.
  • Inheriting configuration: can't easily turn on tracing for an entire application module or namespace unless every class in it is using the same trace source. So you have to think about tracing scopes up-front, and can't fine-tune later.
  • Testing is s harder because of both points above.
  • Correlating activities is non-trivial and error prone (issuing Start/Stop/Transfer calls appropriately)

This project (the Tracer from now on) provides a solution to all three issues, while still leveraging the built-in TraceSource class. This means you're not opting out of .NET core tracing for something totally new and unproven. Essentially, Tracer provides just a slightly improved API that ultimately simply extends the built-in TraceSource.

This means that everything down to configuring sources via config files works as usual, yet you gain run-time configuration of even built-in WCF/WPF/other sources (you can try for example the "System.Windows.Markup" in a WPF app). In addition, integration with the Service Trace Viewer is vastly improved for activity tracing as well as exception reporting.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 is compatible.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.7.2

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages (5)

Showing the top 5 NuGet packages that depend on System.Diagnostics.Tracer:

Package Downloads
System.Net.Mqtt The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A lightweight and simple MQTT client implementation written entirely in C#.

OpcLabs.Mqtt The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Provides MQTT communication features, using the System.Net.Mqtt library. Use with OpcLabs.QuickOpc for OPC UA PubSub transport protocol mappings based on MQTT.

System.Net.Mqtt.Server The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

A lightweight and simple MQTT Server implementation written entirely in C#.

System.Resources.Models

Provides tools to parse .resx and .strings files to a model classes

dr1rrb.System.Net.Mqtt

A lightweight and simple MQTT client implementation written entirely in C#.

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on System.Diagnostics.Tracer:

Repository Stars
xamarin/xamarin-macios
Bridges the worlds of .NET with the native APIs of macOS, iOS, tvOS, and watchOS.
xamarin/mqtt
Minimalist and intuitive MQTT broker and client written entirely in C#
Version Downloads Last updated
2.1.0-alpha 672 2/1/2023
2.0.8 251,137 3/14/2017
2.0.7 1,600 3/10/2017
2.0.6 91,452 11/1/2016
2.0.4 7,598 9/8/2016
2.0.3 1,804 9/7/2016
2.0.1 4,117 8/31/2016
2.0.0 1,806 8/30/2016
1.1.0 1,828 8/12/2016