Nlog: JsonLayout - escape "${value}"

Created on 18 Feb 2017  路  9Comments  路  Source: NLog/NLog

Type (choose one):

  • Bug

NLog version: 4.4.2

Platform: .Net 4.5

Current NLog config: API

        var fileTarget1 = new FileTarget
            {
                Name ="FileTargetName",
                Layout = logObject,
                //so'on.....
            };

        logObject= new JsonLayout
        {
            Attributes =
            {
                new JsonAttribute("timestamp", DateTime.Now.ToString("O")),
                new JsonAttribute("htmltbody", "<tbody>${summaryTable.rowData}</tbody>")
            },
            RenderEmptyObject = false,
            SuppressSpaces = true
        };

BUG case:

  • What is the current result?
    NLog failed to assign JsonAttribute value with simple text "${simpletext}". Example of the JsonAttribute value: "{"html": "Client ${client.name}"}"

  • What is the expected result?
    Expected NLog can assign value in JsonAttribute "Client ${client.name}" is normal text usage instead of automatic recognize as configuration syntax.

  • Are there any work arrounds? yes/no
    No

  • Is there a version in which it did worked?
    No

json / json-layout question

Most helpful comment

You need to escape only ${

There's a helper for that in c#, SimpleLayout.Escape(...)

All 9 comments

NLog failed to assign JsonAttribute value with simple text "${simpletext}". Example of the JsonAttribute value: "{"html": "Client ${client.name}"}"

Please show me how you doing this.

I using JsonLayout to customize my log.

``c# var fileTarget1 = new FileTarget { Name ="FileTargetName", Layout = logObject, //so'on..... };

`
logObject= new JsonLayout
{
Attributes =
{
new JsonAttribute("timestamp", DateTime.Now.ToString("O")),
new JsonAttribute("htmltbody", "${summaryTable.rowData}")
},
RenderEmptyObject = false,
SuppressSpaces = true
};
```

Which mean "${summaryTable.rowData}" actually it is not declare as a variable in NLog config (Configuration Syntax?) but I'm just want the value simply show exactly same word "${summaryTable.rowData}" in log.

Anyway can make NLog ignore it because it is same syntax way.

You could escape it with the ${literal}, see https://github.com/NLog/NLog/wiki/Literal-Layout-Renderer

That's we're you are looking for, right?

Yes! I didn't know got Literal Layout Renderer! It was solved my problem! Thank You!

Sorry...once test with this value with JsonAttribute decode way.
It will not work anymore and got this error message: LayoutRenderer cannot be found: 'test'.

Example of my JSON Serialized to string value:

var value = "{\"Headers\": {\"logname\": \"Test\"},\"dollarSign\": \"${test}\"}"

new JsonAttribute("body", "${literal:text=" + value + "}", false)

You need to escape only ${

There's a helper for that in c#, SimpleLayout.Escape(...)

my problem solved. Thanks again!!

馃憤

Was this page helpful?
0 / 5 - 0 ratings