开发者

Alternative to Console.WriteLine for log/tracing?

In my app currently I'm using

#if DEBUG
Console.WriteLine(....)
#endif

In a number of places. It's been a temporary measure, and is f开发者_JAVA百科ine and dandy, but I'd like to know how I should be doing this properly. Someone mentioned NSLog briefly to me in passing, but I can't find any Monotouch and NSLog info, and I'm not sure how the information is retrievable so that I can see what's happened.

Lastly, if anyone is using any particular strategies for crash deduction and stacktrace grabbing, I'm all ears.


You should take advantage of C#'s [Conditional] support in the language.

You add this attribute to methods, like this:

class Util { 
     [Conditional ("DEBUG")]
     void Log (string msg)
     { 
         Console.WriteLine (msg);
     }
 }

This lets you write code like this:

 Util.Log ("Starting");
 DoSomething ();
 Util.Log ("Ending");

If the symbol DEBUG is defined, the code above compiles the three lines, but if the symbol is not defined, the compiler will only generate code for DoSomething.

This lets you keep the debugging code in your program, and switch it on/off by the use of a compiler define.

Then you add DEBUG as one of your defines to your DEBUG profiles. The same technique can be applied for other configuration builds, you could use the above for Simulator vs Device or Debug vs AppStore vs AdHoc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜