3
\$\begingroup\$

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;
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ This site is for requesting reviews of your code, not for publishing it. \$\endgroup\$ – svick Oct 22 '13 at 13:35
3
\$\begingroup\$
(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.

\$\endgroup\$
  • \$\begingroup\$ Thanks for review! 1. What do you mean, "(null)" ? 3. Ok, seems simplier 4. Yep, deltatime between calls is looking quite nice in the second raw. For example, it shows that reading json with 30 items (like 5-7 fields per item) took 0.3 seconds. I'd never think that it can take that much amount of time, so i'd, probably, didnt even look there. Instead, having second raw as deltatime highlights this issues. Anyway, that's minor change everybody can fit themselves. \$\endgroup\$ – Vitalii Vasylenko Oct 22 '13 at 14:01
  • 1
    \$\begingroup\$ @VitaliiVasylenko Ad 1. I meant that when the value is null, the logger would make it clear; something like value == null ? "(null)" : value.ToString(). \$\endgroup\$ – svick Oct 22 '13 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.