{"id":15017945,"url":"https://github.com/nlog/nlog.diagnosticsource","last_synced_at":"2025-07-25T11:33:51.741Z","repository":{"id":37957549,"uuid":"230812543","full_name":"NLog/NLog.DiagnosticSource","owner":"NLog","description":"NLog integration with Microsoft Activity TraceId and SpanId","archived":false,"fork":false,"pushed_at":"2025-07-20T16:08:20.000Z","size":154,"stargazers_count":14,"open_issues_count":1,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-20T16:42:03.007Z","etag":null,"topics":["csharp","dotnet","nlog","nlog-target","open-telemetry","open-telemetry-csharp","opentelemetry"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NLog.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-12-29T22:52:21.000Z","updated_at":"2025-07-20T16:08:24.000Z","dependencies_parsed_at":"2023-01-31T12:15:17.361Z","dependency_job_id":"22963828-b8cf-4ff5-b876-ef06eab40856","html_url":"https://github.com/NLog/NLog.DiagnosticSource","commit_stats":{"total_commits":70,"total_committers":4,"mean_commits":17.5,"dds":0.6285714285714286,"last_synced_commit":"1d9f235cecd0877f048835287381185c66220969"},"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"purl":"pkg:github/NLog/NLog.DiagnosticSource","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NLog%2FNLog.DiagnosticSource","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NLog%2FNLog.DiagnosticSource/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NLog%2FNLog.DiagnosticSource/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NLog%2FNLog.DiagnosticSource/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NLog","download_url":"https://codeload.github.com/NLog/NLog.DiagnosticSource/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NLog%2FNLog.DiagnosticSource/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266998045,"owners_count":24018950,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["csharp","dotnet","nlog","nlog-target","open-telemetry","open-telemetry-csharp","opentelemetry"],"created_at":"2024-09-24T19:51:14.082Z","updated_at":"2025-07-25T11:33:51.732Z","avatar_url":"https://github.com/NLog.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# NLog.DiagnosticSource\r\nNLog ActivityTraceLayoutRenderer for [Microsoft Activity Trace](https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md)\r\n\r\nNLog DiagnosticListenerTarget for [Microsoft DiagnosticSource](https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/DiagnosticSourceUsersGuide.md)\r\n\r\n[![Version](https://badge.fury.io/nu/NLog.DiagnosticSource.svg)](https://www.nuget.org/packages/NLog.DiagnosticSource)\r\n[![AppVeyor](https://img.shields.io/appveyor/ci/nlog/NLog-DiagnosticSource/master.svg)](https://ci.appveyor.com/project/nlog/NLog-DiagnosticSource/branch/master)\r\n\r\n### How to install\r\n\r\n1) Install the package\r\n\r\n    `Install-Package NLog.DiagnosticSource` or in your csproj:\r\n\r\n    ```xml\r\n    \u003cPackageReference Include=\"NLog.DiagnosticSource\" Version=\"6.*\" /\u003e\r\n    ```\r\n\r\n2) Add to your nlog.config:\r\n\r\n    ```xml\r\n    \u003cextensions\u003e\r\n        \u003cadd assembly=\"NLog.DiagnosticSource\"/\u003e\r\n    \u003c/extensions\u003e\r\n    ```\r\n\r\n   Alternative register from code using fluent configuration API:\r\n   ```csharp\r\n   LogManager.Setup().SetupExtensions(ext =\u003e {\r\n      ext.RegisterTarget\u003cNLog.Targets.DiagnosticListenerTarget\u003e();\r\n      ext.RegisterLayoutRenderer\u003cNLog.LayoutRenderers.ActivityTraceLayoutRenderer\u003e();\r\n   });\r\n   ```\r\n\r\n### How to use ActivityTraceLayoutRenderer\r\nThe `System.Diagnostics.Activity.Current` from Microsoft allows one to create OpenTelemetry spans. \r\n\r\nExample of `NLog.config` file that outputs span-details together with LogEvent by using `${activity}`:\r\n\r\n```xml\r\n\u003cnlog\u003e\r\n\u003cextensions\u003e\r\n    \u003cadd assembly=\"NLog.DiagnosticSource\"/\u003e\r\n\u003c/extensions\u003e\r\n\u003ctargets\u003e\r\n    \u003ctarget name=\"console\" xsi:type=\"console\" layout=\"${message}|ActivityId=${activity:property=TraceId}\" /\u003e\r\n\u003c/targets\u003e\r\n\u003crules\u003e\r\n    \u003clogger minLevel=\"Info\" writeTo=\"console\" /\u003e\r\n\u003c/rules\u003e\r\n\u003c/nlog\u003e\r\n```\r\n\r\n**Property Enum Values**\r\n- Id : Hierarchical structure identifier that is concatenation of ParentIds\r\n- SpanId : Identifier for the current activity (Ex. database activity within current request)\r\n- ParentId : Identifier for the parent activity\r\n- TraceId : Identifier for the root activity (Request Trace Identifier)\r\n- OperationName : Operation name of the current activity\r\n- DisplayName : Explicit assigned Activity DisplayName (with fallback to OperationName)\r\n- StartTimeUtc : Time when the operation started\r\n- Duration : Duration of the operation (formatted as TimeSpan)\r\n- DurationMs : Duration of the operation (formatted as TimeSpan.TotalMilliseconds)\r\n- Baggage : Collection of key/value pairs that are passed to children of this Activity (Use `Format=\"@\"` for json-dictionary)\r\n- Tags : Collection of key/value pairs that are NOT passed to children of this Activity (Use `Format=\"@\"` for json-dictionary)\r\n- CustomProperty : Custom property assigned to this activity. Must be used together with Item-option\r\n- Events : Events attached to this activity (Use `Format=\"@\"` for json-array)\r\n- TraceState : W3C tracestate header\r\n- TraceFlags : See System.Diagnostics.ActivityTraceFlags for activity (defined by the W3C ID specification). For int-value combine with `format=\"d\"`\r\n- SourceName : Name of the activity source associated with this activity\r\n- SourceVersion : Version of the activity source associated with this activity\r\n- ActivityKind : Relationship kind between the activity, its parents, and its children. For int-value combine with `format=\"d\"`\r\n- TraceStateString : W3C 'tracestate' header as a string\r\n- Status : Status code of the current activity (`Ok` or `Error`). For int-value combine with `format=\"d\"` \r\n- StatusDescription : Status description of the current activity\r\n- IsAllDataRequested : Activity span has been marked to be sampled / recorded (`0` or `1`)\r\n\r\n**Formatting**\r\n- Format: Format for rendering the property.\r\n- Culture: CultureInfo for rendering the property (Default Invariant Culture)\r\n- Item: Lookup a single item from property-collection (Baggage, Tags, CustomProperty)\r\n  - `${activity:property=Baggage:item=BaggageKey}`\r\n  - `${activity:property=Tags:item=TagKey}`\r\n  - `${activity:property=CustomProperty:item=PropertyKey}`\r\n\r\n**Extract property values from parent or root**\r\n\r\nIt is possible to specify that the above property should be extracted from either root- or parent-activity.\r\n\r\n```\r\n${activity:property=OperationName:parent=true}\r\n```\r\n\r\n```\r\n${activity:property=OperationName:root=true}\r\n```\r\n\r\n**Manually configure ActivityTrackingOptions**\r\n\r\nWhen using the default HostBuilder then it will automatically setup the following [ActivityTrackingOptions](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.loggerfactoryoptions.activitytrackingoptions):\r\n```c#\r\nbuilder.ConfigureLogging((hostingContext, loggingBuilder) =\u003e\r\n{\r\n      loggingBuilder.Configure(options =\u003e\r\n      {\r\n            options.ActivityTrackingOptions = ActivityTrackingOptions.SpanId\r\n                                            | ActivityTrackingOptions.TraceId\r\n                                            | ActivityTrackingOptions.ParentId;\r\n      });\r\n}).\r\n```\r\nIf creating a custom HostBuilder, then one have to manually setup the ActivityTrackingOptions like shown above.\r\n\r\n### How to use DiagnosticListenerTarget\r\n\r\nExample of `NLog.config` file that uses the `diagnosticListener` target:\r\n\r\n```xml\r\n\u003cnlog\u003e\r\n\u003cextensions\u003e\r\n    \u003cadd assembly=\"NLog.DiagnosticSource\"/\u003e\r\n\u003c/extensions\u003e\r\n\u003ctargets\u003e\r\n    \u003ctarget name=\"diagSource\" xsi:type=\"diagnosticListener\" layout=\"${message}\" sourceName=\"nlog\" eventName=\"${logger}\" /\u003e\r\n\u003c/targets\u003e\r\n\u003crules\u003e\r\n    \u003clogger minLevel=\"Info\" writeTo=\"diagSource\" /\u003e\r\n\u003c/rules\u003e\r\n\u003c/nlog\u003e\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlog%2Fnlog.diagnosticsource","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnlog%2Fnlog.diagnosticsource","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnlog%2Fnlog.diagnosticsource/lists"}