0
private static void print(StreamWriter sw, string mlog, bool screen)
    {
        DateTime ts = DateTime.Now;
        sw.WriteLine(ts + " " + mlog);
        if (screen == true)
        {
            Console.WriteLine(mlog);
        }
    }

I would use print (sw,"write here", false) to call. 90% chance I will use false. how to make it the default to be false that way I dont have to do the extra type when I do the call?

1
  • 1
    Sometimes its better to have explicit methods, eg PrintToStream() and PrintToStreamAndScreen() Commented Jul 2, 2012 at 19:06

7 Answers 7

7

If you're using C# 4, you can make screen an optional parameter:

// Note: changed method and parameter names to be nicer
private static void Print(StreamWriter writer, string log, bool screen = false)
{
    // Note: logs should almost always use UTC rather than the system local
    // time zone
    DateTime now = DateTime.UtcNow;

    // TODO: Determine what format you want to write your timestamps in.
    sw.WriteLine(CultureInfo.InvariantCulture,
                 "{0:yyyy-MM-dd'T'HH:mm:ss.fff}: {1}", now, log);
    if (screen)
    {
        Console.WriteLine(mlog);
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I've been converting more of my code to use DateTimeOffset rather than UTC DateTime .. I find it less confusing later to convert/know the "appropriate" local time. Not necessarily relevant here, but not many people seem to know about it.
@pst: I'd recommend you convert to using Noda Time where it's all clearer to start with :)
Haha, thanks. I didn't know there was such a library for .NET.
@JonSkeet thanks.why should I use UtcNow instead of Now? Pros and cons
@JohnRyann: In my experience, logging in UTC is simpler in all respects. It's friendlier for machine readability, and means you don't need to worry about what time zone the machine happened to be in at the time. Oh, and you don't get logs that go backwards around DST transitions...
1

Just use = false:

private static void print(StreamWriter sw, string mlog, bool screen = false)

Here's a little more info on Named and Optional Arguments in C#.

Note that this is new in C# 4.0. For older versions, use method overloads as others have suggested.

Comments

1
private static void print(StreamWriter sw, string mlog)
{
    print(sw, mlog, false);
}

Comments

1

For older versions you can simply provide 2 overrides:

private static void print(StreamWriter sw, string mlog)
{ 
 print(sw,mlog, false);
}

Comments

1

If you aren't using C# 4, create a function overload:

private static void Print(StreamWriter writer, string log) 
{ 
    Print(writer, log, false);
} 

Comments

1

The answers involving optional parameters will work, but some languages do not support optional parameters, so they could not call this method from a public-facing API.

I would go with method overloading..

private static void print(StreamWriter sw, string mlog) {
    print(sw, mlog, false);
}

private static void print(StreamWriter sw, string mlog, bool screen) { ... }

2 Comments

True, and a good principle to remember. This case, however, is a private method, so I don't think you really need to consider that.
@TimS. agreed in this instance. If it were to become a public method they may want to consider overloading.
1
private static void print(StreamWriter sw, string mlog = "Write here", bool screen = false)
    {
        DateTime ts = DateTime.Now;
        sw.WriteLine(ts + " " + mlog);
        if (screen == true)
        {
            Console.WriteLine(mlog);
        }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.