NLog / NLog/NLog.DiagnosticSource

Parsing TraceId to Guid

Open
#73 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
C#
Stars
16
Forks
4
Avg merge
7h 59m
Merged PRs (30d)
4

Description

We have a situation where I need to log the unique trace id to the database, but the column type is uniqueidentifier and the value of TraceId is what seems to be GUID without hyphens (ASP.NET Core 6) and saving it like that doesn't work. Unfortunately the database cannot be changed.

Is there a built-in way to Guid.TryParse a string or do I need my own custom layout renderer?

I managed to solve my issue by creating this layout wrapper:

[LayoutRenderer("parseguid")]
[ThreadAgnostic]
public sealed class GuidParserRenderer : WrapperLayoutRendererBase
{

    /// <inheritdoc />
    protected override string Transform(string text)
    {
        return Guid.TryParse(text, out var guid) ? guid.ToString() : Guid.Empty.ToString();
    }
}

Registration:

LogManager.Setup().SetupExtensions(s =>
    s.RegisterLayoutRenderer<GuidParserRenderer>("parseguid")
);

Usage:

<parameter name="@ActivityId" layout="${parseguid:inner=${activity:property=TraceId}}" />

If I do need this custom wrapper, is it implemented correctly? Is it OK to use ThreadAgnostic when wrapping ActivityTraceLayoutRenderer?

Last question: Is TraceId the same as activityId which seems to be deprecated?

Apologies if I missed some obvious piece of documentation somewhere, I tried looking but couldn't find an answer.

Thank you for all of your hard work!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's GuidParserRenderer wrapper, its RegisterLayoutRenderer registration, and the ${activity:property=TraceId} usage. Review the ActivityTraceLayoutRenderer behavior and the ThreadAgnostic attribute, then document or clarify whether built-in parsing exists, whether the wrapper is safe, and how TraceId relates to activityId.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
observability
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.