DiagnosticSourceLogging 1.1.0
dotnet add package DiagnosticSourceLogging --version 1.1.0
NuGet\Install-Package DiagnosticSourceLogging -Version 1.1.0
<PackageReference Include="DiagnosticSourceLogging" Version="1.1.0" />
paket add DiagnosticSourceLogging --version 1.1.0
#r "nuget: DiagnosticSourceLogging, 1.1.0"
// 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
DiagnosticSource output helper to ILogger
DiagnosticSourceLogging is helper package for outputting DiagnosticSource to ILogger. This uses generic host for listening thread.
Usage
Package installation
add following nuget packages to your project
- DiagnosticSourceLogging
- Microsoft.Extensions.Hosting
- any package of ILogger destination
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
.
Example:
// Generally, CreateDefaultBuilder configures ConsoleLogger.
await Host.CreateDefaultBuilder()
// configuring logging...
.AddDiagnosticSourceLoggingService<MyDiagnosticSourceLoggingServiceOptions>()
// add your tasks...
.RunConsoleAsync()
;
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
DiagnosticSourceLogging.EventObserver
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
DiagnosticSourceLogging.DiagnosticListenerObserver
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 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 was computed. 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. |
-
.NETStandard 2.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 5.0.0)
- Microsoft.Extensions.Options (>= 5.0.0)
- System.Diagnostics.DiagnosticSource (>= 5.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.