首页 > 解决方案 > 诊断源的 NLog 诊断侦听器

问题描述

是否有任何 NLog 包提供与日志 .NET DiagnosticSource 模型的本机集成?有点像 NLogTraceListener ...

乍一看,在谷歌上看不到任何东西。

标签: .netnlogsystem.diagnostics

解决方案


也许您可以只执行 DiagnosticListener.AllListeners.Subscribe 然后转发到 NLog-Logger:

            DiagnosticListener.AllListeners.Subscribe(delegate(DiagnosticListener listener)
            {
                // subscribe to the Service Bus DiagnosticSource
                if (listener.Name == "Microsoft.Azure.ServiceBus")
                {
                    // receive event from Service Bus DiagnosticSource
                    listener.Subscribe(delegate(KeyValuePair<string, object> @event)
                    {
                        // Log operation details once it's done
                        if (!@event.Key.EndsWith("Stop"))
                            return;
                        var currentActivity = Activity.Current;
                        NLogLogger.Debug($"{currentActivity.OperationName} Duration: {currentActivity.Duration}\n\t{string.Join("\n\t", currentActivity.Tags)}");
                    });
                }
            });

https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/DiagnosticSourceUsersGuide.md#discovery-of-diagnosticlisteners

https://github.com/Microsoft/ApplicationInsights-dotnet-server/blob/develop/Src/Web/Web/AspNetDiagnosticTelemetryModule.cs

设置观察者(你想要的监听器)的例子:

https://github.com/NLog/NLog.DiagnosticSource/blob/master/test/NLog.DiagnosticSource.Tests/DiagnosticListenerTargetTests.cs

监听来自目标的输出(相反的方式):

https://github.com/NLog/NLog.DiagnosticSource/blob/master/src/NLog.DiagnosticSource/Targets/DiagnosticListenerTarget.cs


推荐阅读