0

UPDATE: This turned out to be a discrepancy that exists in Serilog (which I tunnel to), but does not exist in NLog, which treats a single argument as a verbatim string (as one (and specifically, I) might expect)


Using NLog, if I want to log something potentially containing embedded double braces: {{ without having them collapsed, what's the most efficient way?

e.g.:

NLog.LogManager.GetLogger("x").Warn("{{\"aaa}}")

emits in my custom serilog-target:

{"aaa}

And I want:

{{"aaa}}

Is

NLog.LogManager.GetLogger("x").Warn("{0}","{{\"aaa}}")

the best way or is there a more efficient way ?

UPDATE: no such tricks required:

NLog.LogManager.GetLogger("x").Warn("{{\"aaa}}")

... does indeed just work!

(This is not (just) me being pedantic, its a follow-on from a question regarding a high perf/throughput requirement)

Rolf Kristensen
  • 17,785
  • 1
  • 51
  • 70
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • Not sure I understand. NLog doesn't do any parsing of the string if not providing any parameters. So your first example doesn't make sense, unless you have a broken target. – Rolf Kristensen Apr 05 '18 at 19:04
  • @RolfKristensen Yes, 100% correct (see my declaration below). It does mean the answer in the linked question by Julian turns out to be wrong though:- the forwarder needs to materialize using NLog's formatting – Ruben Bartelink Apr 05 '18 at 19:14
  • I guess your work-around will do then, though I would probably do it in the custom target. If no parameters then inject the string as parameter to Serilog using the message template `{0}` (Serilog caches message templates so it should be inexpensive) – Rolf Kristensen Apr 05 '18 at 19:33
  • Guess you should rephrase your question to "Logging a literal message in Serilog" :) – Rolf Kristensen Apr 05 '18 at 19:35
  • @RolfKristensen LOL. I met you half way on the title ;) (And had already done the transition in the obvious place:- the `SerilogTarget`). Thanks for the thinking, checking and validation. – Ruben Bartelink Apr 05 '18 at 21:46

1 Answers1

0

Turns out the question is wrong. The code above works in NLog - a single string is not treated as a message template and is rendered verbatim.

I was tunneling it thru to Serilog per the linked question, and Serilog was doing the collapsing I show the effects of in the question.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249