Manushin Igor
Manushin Igor

Reputation: 3689

How to print threadid instead of threadname by using NLog when .Net Thread does not have name?

I have NLog configuration like below.

It prints name of thread if it exists. Otherwise I get empty string instead of thread name.

Question: how can I archive behavior below?

Current configuration example:

<variable name="defaultLayout" value="${date} ${level} [${threadname}] ${logger} - ${message} ${exception:format=ToString}"/>

<targets async="true">
  <target name="ConsoleAppender" 
          type="ColoredConsole" 
          layout="${var:defaultLayout}" />
</targets>

Please note: I'd like to avoid printing both variables, e.g. this layout of not useful: ... [${threadname}-${threadid}] ...

Upvotes: 8

Views: 5677

Answers (1)

Manushin Igor
Manushin Igor

Reputation: 3689

TLDR: use this - ${threadname:whenEmpty=${threadid}}

So right configuration:

<variable name="defaultLayout" value="${date} ${level} [${threadname:whenEmpty=${threadid}}] ${logger} - ${message} ${exception:format=ToString}"/>

<targets async="true">
   <target name="ConsoleAppender" 
      type="ColoredConsole" 
      layout="${var:defaultLayout}" />
</targets>

Upvotes: 11

Related Questions