Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8294617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:16:57+00:00 2026-06-08T14:16:57+00:00

I would like to change the default Environment.NewLine character for my current environment. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:17:00+00:00Added an answer on June 8, 2026 at 2:17 pm

    I’m not convinced by this:

    When I move the core framework into a Windows service the Environment.NewLine changes to “\n”

    I’ve checked the IL for get_NewLine(), and it is hard-coded to:

    ldstr "\r\n"
    ret
    

    So basically; the problem isn’t what you think it is; changing Environment.NewLine isn’t going to do anything (and: isn’t possible).

    So: how are you redirecting? I tend to just do something like:

    if (!Environment.UserInteractive)
    {
        var tw = new SlowWriter(Path.Combine(logPath,"{0}.log"));
        Console.SetError(tw);
        Console.SetOut(tw);
    }
    

    where SlowWriter is a custom type (subclasses TextWriter) that makes sure the file doesn’t stay open; slower, but pretty robust:

    class SlowWriter : TextWriter
    { // this opens and closs each time; slower, but doesn't lock the file
        private readonly string path;
        public SlowWriter(string path)
        {
            this.path = path;
        }
        public override System.Text.Encoding Encoding
        {
            get { return System.Text.Encoding.UTF8; }
        }
        private TextWriter Append()
        {
            var finalPath = string.Format(path, DateTime.UtcNow.ToString("yyyyMMdd"));
            return File.AppendText(finalPath);
        }
        public override void Write(string value)
        {
            lock (this)
            {
                using (var file = Append())
                {
                    file.Write(value);
                }
            }
        }
        public override void Write(char[] buffer, int index, int count)
        {
            lock(this)
            {
                using (var file = Append())
                {
                    file.Write(buffer, index, count);
                }
            }
        }
        public override void Write(char[] buffer)
        {
            lock (this)
            {
                using (var file = Append())
                {
                    file.Write(buffer);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like to change the default message that is displayed when an application
I would like to change my default route values. Right now, I have: routes.MapRoute(
I would like to change visual studio's default configurations (Debug,Release). That is, change the
I am writing a small editor. I would like to change default behaviour of
I would like to change the default style for vector features in a map
I have a ListView and each item have a TextView. I would like change
I would like to change all captions from one type to another. While not
I would like to know how to change default Azure SDK local deploy directory?
I would like to change the wordpress default query to orderby title when viewing
I would like to change the default behaviour of the expandable list view from:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.