kara
kara

Reputation: 3455

How to get the ThreadId in a custom target without using layout?

I did create a custom-db-target to log to my database. It's a very simple target which only overrides the Write method to create a log-entry in my db.

This works fine and all informations are delivered by the LogEventInfo.

But now i want to log the threadId. As far as I know, NLog has the layout-parameter ${threadid} to do so. But i don't want to use a layout-renderer to write into my db.

My Question

Is it possible to ask the LogEventInfo for the threadId? If yes: How? If no: Where does NLog get this id?

Upvotes: 1

Views: 251

Answers (1)

Julian
Julian

Reputation: 36740

You could create property (preferred) or variable with the threadId layout and render that one.

e.g.

Layout threadIdLayout = "${threadid}"; //for performance, as static property on the custom class is recommend

var threadId = threadIdLayout.Render(logEvent);

Upvotes: 3

Related Questions