Search code examples
c#.netstring.net-2.0

Alternative to adding spaces in strings in NET 2.0?


I am creating a server-client model and am forced to use .NET 2.0 as my framework due to the nature of my development environment on the client side.

Introduction aside, let's say I wanted the console to output a new connection was made from a certain ip address at a certain time. I would write:

Console.Writeline("New connection: " +
                  client.endpoint.ToString() + " " + DateTime.Now.ToString());

I was wondering if there is an alternative to this line. Some of these lines are becoming very long and feels a bit untidy to me.


Solution

  • You can use one of the Console.WriteLine() overloads. It doesn't save much, but perhaps it more naturally splits the statement into multiple lines, for example:

    Console.WriteLine("New connection: {0} {1}", 
                      client.endpoint, 
                      DateTime.Now);
    

    You can also explore the Trace class. You can configure the Trace options to always output the DateTime so you'd be able to skip that parameter. Then, you can add a ConsoleTraceListener to write those Trace messages to the Console. Using this approach, you'd do something like:

    Trace.TraceInformation("New connection: {0}", client.endpoint);