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

1 Answer 1

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.

answered Oct 22, 2013 at 13:46
\$\endgroup\$
2
  • \$\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\$ Commented Oct 22, 2013 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\$ Commented Oct 22, 2013 at 14:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.