DiagnosticSourceLogging 1.1.0

.NET Standard 2.0
dotnet add package DiagnosticSourceLogging --version 1.1.0
NuGet\Install-Package DiagnosticSourceLogging -Version 1.1.0
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="DiagnosticSourceLogging" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DiagnosticSourceLogging --version 1.1.0
#r "nuget: DiagnosticSourceLogging, 1.1.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install DiagnosticSourceLogging as a Cake Addin
#addin nuget:?package=DiagnosticSourceLogging&version=1.1.0

// Install DiagnosticSourceLogging as a Cake Tool
#tool nuget:?package=DiagnosticSourceLogging&version=1.1.0

NuGet Link

DiagnosticSource output helper to ILogger

DiagnosticSourceLogging is helper package for outputting DiagnosticSource to ILogger. This uses generic host for listening thread.


Package installation

add following nuget packages to your project

Implement IDiagnosticSourceLoggingServiceOptions

After your nuget package setting,implement DiagnosticSourceLogging.IDiagnosticSourceLoggingServiceOptions for listener setting. Here is the IDiagnosticSourceLoggingServiceOptions's members

  • bool ShouldListen(DiagnosticListener listener)
    • It should return whether the DiagnosticListener should be watched
    • If it returns true, Logging will be started
    • It may be called multiple times in same listener instance
    • It is not called frequently, almost once per one DiagnosticListener
  • bool IsEnabled(string sourceName, string eventName)
    • It should return whether events should be processed
    • If it returns true, GetEventProcessor will be called and invoking returned callback
    • It may be call many times, so heavy process should not run in it.
  • Action<ILogger, string, object> GetEventProcessor(string sourceName, string eventName)
    • It should return callback for DiagnosticSource events
    • This will be called every events occured, so return value should be cached for almost cases

Here is the example implementation:

class MyDiagnosticSourceLoggingServiceOptions : IDiagnosticSourceLoggingServiceOptions
    ConcurrentDictionary<(string sourceName, string eventName), Action<ILogger, string, object>> Processors = new();
    public Action<ILogger, string, object> GetEventProcessor(string sourceName, string eventName)
        return Processors.GetOrAdd((sourceName, eventName), n =>
            var f = LoggerMessage.Define<string, object>(LogLevel.Information, new EventId(1, $"{n.sourceName}"), "{0}: {1}");
            return (ILogger logger, string msg, object arg) => f(logger, msg, arg, null);

    public bool IsEnabled(string sourceName, string eventName, object arg1, object arg2)
        return true;

    public bool ShouldListen(DiagnosticListener listener)
        return listener.Name.StartsWith("DiagnosticSourceLogging.Test");

Adding task

DiagnosticSourceLogging needs background task which holds subscription for DiagnosticListener. Background task can be added by AddDiagnosticSourceLoggingService<T>(this IHostBuilder builder, Func<T> optionsFactory = null) where T: IDiagnosticSourceLoggingServiceOptions.


// Generally, CreateDefaultBuilder configures ConsoleLogger.
await Host.CreateDefaultBuilder()
    // configuring logging...
    // add your tasks...

Running without GenericHost

you can use IDisposable DiagnosticSourceLogging.Observable.Subscribe(ILoggerFactory loggerFacotry, IDiagnosticSourceLoggingServiceOptions options) too.

var loggerFactory = new LoggerFactory();
using var subscription = DiagnosticSourceLogging.Observable.Subscribe(loggerFactory, new MyDiagnosticSourceLoggingServiceOptions());
// observe until subscription disposed

or you can use the way which does not implement option interface.

var loggerFactory = new LoggerFactory();
using var subscription = DiagnosticSourceLogging.Observable.Subscribe(loggerFactory, (listener) => listener.Name == "DiagnosticSourceName",
                    (src, ev) => (logger, ev, arg) => Console.WriteLine($"event: {ev}, {arg}"), (src, ev, arg1, arg2) => true);
// observe until subscription disposed

DiagnosticSource exported by this package

DiagnosticSourceLogging.DiagnosticSourceLoggingService_[typename of T]

This is related on DiagnosticSourceLogging's background task.

  • Start
    • argument is always null
  • Stop
    • argument is TimeSpan, which means background task's running time
  • Error
    • argument is Exception, which means error in background task


This is related on personal DiagnosticSource's observers.

  • Completed
    • triggered when observer is completed
    • argument is DiagnosticSourceLogging.EventObserverCompletedArg
  • ProcessError
    • triggered when exception is raised in subscribe
    • argument is DiagnosticSourceLogging.EventObserverProcessErrorArg
  • Error
    • triggered when observer is interrupted by unexpected exception
    • argument is DiagnosticSourceLogging.EventObserverErrorArg


This is related on observing DiagnosticListener.AllListeners.

  • Starting
    • triggered by starting observing DiagnosticListener
    • argument is DiagnosticSourceLogging.ObserverStartingArg
  • Completed
    • triggered by completing observing DiagnosticListener.AllListeners
    • argument is DiagnosticSourceLogging.ObserverCompletedArg
  • Error
    • triggered by unexpected error in observing DiagnosticListener.AllListeners
    • argument is DiagnosticSourceLogging.ObserverErrorArg
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 247 9/2/2021
1.0.0 194 8/31/2021