Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print a multi-line message with NLog

Tags:

nlog

Is it possible with NLog to emit a multi-line message such that each line is formatted according to the current layout? E.g.

2015-12-17 11:37:38.0845 | 64 | INFO | -----------------------------------
2015-12-17 11:37:38.0845 | 64 | INFO | Statistics:
2015-12-17 11:37:38.0845 | 64 | INFO |   Crawling Requests   46887 /min
2015-12-17 11:37:38.0845 | 64 | INFO |   Stored Documents    9910 /min
2015-12-17 11:37:38.0845 | 64 | INFO | -----------------------------------

Alternatively, is it possible with NLog to emit multiple messages as a single, non-interrupted block in a multithreaded environment?

like image 460
François Beaune Avatar asked Dec 17 '15 11:12

François Beaune


1 Answers

You can do all this from your config.

<variable name="statData" value="${longdate} | 64 | ${level} | "/>
<variable name="line" value="-----------------------------------"/>
<targets>
    <target xsi:type="Console"
        name="Console"
        layout="
        ${statData}${line}${newline}
        ${statData}Statistics:${newline}
        ${statData}  Crawling Requests   46887 /min ${newline}
        ${statData}  Stored Documents    9910 /min ${newline}
        ${statData}${line}${newline}"/>

Wasn't exactly sure what your 64 was or where you were getting your per minute data. Probably a variable or something your inserting.This should also work if you are logging to a file not the console.

As for your second question, if you are wanting a single log message from multiple threads I think you would have to do that on the code side. You would have to collect your threads, get your log data you want and send it 1 time to nLog. I might be misunderstanding though

like image 106
Alex Bezek Avatar answered Nov 09 '22 20:11

Alex Bezek