I would like to change the default Environment.NewLine character for my current environment.
I have one standalone application that is writing messages to a console. When the core framework works in the console environment the new line is \r\n. When I move the core framework into a Windows service the Environment.NewLine changes to “\n”.
I would like to be able to change the Console to always use “\r\n” because when I redirect the output of the console to a file the output has no “windows new lines” when read from example Notepad.
I would like Console.WriteLine to use \r\n whenever I want.
EDIT:
I am redirecting Console.out:
ConsoleOut = File.AppendText(fileName);
ConsoleOut.AutoFlush = true;
Console.SetOut(ConsoleOut);
Console.SetError(ConsoleOut);
Every Console.WriteLine or Console.Write is sent to a file but as I said I am experiencing different behaviours in Windows Service and in standalone Windows environment.
I’m not convinced by this:
I’ve checked the IL for
get_NewLine(), and it is hard-coded to:So basically; the problem isn’t what you think it is; changing
Environment.NewLineisn’t going to do anything (and: isn’t possible).So: how are you redirecting? I tend to just do something like:
where
SlowWriteris a custom type (subclassesTextWriter) that makes sure the file doesn’t stay open; slower, but pretty robust: