HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Simplest WP7 logger

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simplestwp7logger

Problem

Just ended up with this approach for wp7 (no tag yet), in case someone would find it useful. Also, improvement considerations are welcome.

It works with

  • SimpleLogger.WriteLine("JustLine");



  • SimpleLogger.WriteLine(ObjectToBeCastedToString);



  • SimpleLogger.WriteLine("Price is {0} {1}", price, currency);



public class SimpleLogger
{
    private static DateTime lastLog;

    [Conditional("DEBUG")]
    public static void WriteLine(object value)
    {
        WriteLine((value == null) ? "(null)" : value.ToString());
    }

    [Conditional("DEBUG")]
    public static void WriteLine(string format)
    {
        WriteLine("{0}", format);
    }

    [Conditional("DEBUG")]
    public static void WriteLine(string format, params object[] values)
    {
        var formatted = String.Format(null, format, values);

        Debug.WriteLine("{0:hh:mm:ss.fff} [{1:hh:mm:ss.fff}] {2}", DateTime.UtcNow, DateTime.UtcNow - lastLog, formatted);
        lastLog = DateTime.UtcNow;
    }
}

Solution

(value == null) ? string.Empty : value.ToString()


Consider whether something more descriptive would be suitable for null. Maybe something like "(null)".

public static void WriteLine(string format)


The name format is confusing here, because it's not actually format string, it's the value to write. So calling it value (like in the object overload) would make more sense. And maybe you don't need string overload at all.

DateTime.Now.ToUniversalTime()


You can write just DateTime.UtcNow. And you should do the same for lastLog too (assuming you keep it, see next item).

DateTime.Now - lastLog


I'm not sure what is this for. I don't think the time between two consecutive log entries is that interesting, it just clutters the log. And when it is interesting, you can compute the approximate time difference just by looking at it.

Code Snippets

(value == null) ? string.Empty : value.ToString()
public static void WriteLine(string format)
DateTime.Now.ToUniversalTime()
DateTime.Now - lastLog

Context

StackExchange Code Review Q#33050, answer score: 3

Revisions (0)

No revisions yet.