How can I output "console.write"-style output (no newline) with NLog?
Asked Answered
A

5

7

How can I output log as console.write() i.e. no newline with NLog?

${message} defaults to inserting a newline.

Armenia answered 18/4, 2011 at 6:30 Comment(3)
could you please explain more?Kanaka
I think he means when using nlogImmobilize
yes i mean With NLog , i've read the document , but i can't find the layout like 'console.write'-like output !Armenia
P
1

The filetarget has the property LineEnding which could be set to the following values:

  • Default - Insert platform-dependent end-of-line sequence after each line.
  • CR - Insert CR character (ASCII 13) after each line.
  • CRLF - Insert CR LF sequence (ASCII 13, ASCII 10) after each line.
  • LF - Insert LF character (ASCII 10) after each line.
  • None - Don't insert any line ending.

In your case you need None, so:

 <target xsi:type="File"
          name="file1"
          lineEnding="None"

If you need this in another target, e.g. ConsoleTarget, then that isn't implemented. Please open an issue on GitHub.

Protrusive answered 2/9, 2016 at 18:20 Comment(0)
C
1

You could use the replace new lines layout renderer in your nlog config e.g.:

<target name="HealthLog" 
    xsi:type="File" 
    fileName="$C:\Logs\logfile.log"
    layout="${date:format=yyyy-MM-dd HH\:mm\:ss.fff} ${level:uppercase=true} ${replace-newlines:${message}}"
/>

Documentation on the replace new lines layout renderer can be found here: https://github.com/NLog/NLog/wiki/Replace-NewLines-Layout-Renderer

Considerate answered 7/12, 2022 at 15:19 Comment(0)
E
0

I hope I understand your meaning, it seems that you have new line characters in the string you want to write which causes it to go to the new line in the console, if so replace the newline characters with blank string like below.

string Message = "testMessage\r\n";
Console.Write(Message.Replace("\r\n",""));
Enliven answered 18/4, 2011 at 6:36 Comment(3)
+1, this was what I was going to answer with when I read the question.Ferree
A nice guess, but s/he wants to output with something like "myNlogLogger.Info("This is a log message");". I don't know that NLog provides the capability to omit the newline.Andrej
This was actually what I was searching for, but as mentioned does not really answer the question. ThxMorrell
N
0

It seems there is a property named LineEndingMode / LineEnding, which you could try to modify.

Nonsuch answered 7/7, 2016 at 5:25 Comment(0)
Z
-1

To log items in the console, use console.log(item).

Zusman answered 18/4, 2011 at 6:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.