Saqib Saqib - 1 year ago 138
iOS Question

What happens to Console.WriteLine calls in Xamarin.iOS release builds?

When developing iOS apps using XCode, you can use NSLog calls to write information to the console for debugging. In Xamarin.iOS, the same can be done with Console.WriteLine.

When running a release build of an app, without the debugger attached, these calls serve no purpose. So I've thought of excluding them with pre-processor directives:


So that the calls don't even exist in the release build. I appreciate this may be micro-optimisation - but I'm curious if Xamairn already does this, making this unnecessary?

Answer Source

Console output is not removed in iOS release builds.

FYI: Xcode does not "automatically" remove NSLog or print from Obj-C/Swift "release" code either. (You typically define a single global preprocessor macro to remove them all, if desired...)

Your options are many... including:

  • DEBUG preprocessor as you show in your question.

  • Use System.Diagnostic.Debug.WriteLine:

Any calls to Debug.* will be removed by the compiler due to the [Conditional("DEBUG")] attribute being applied.

  • Create your own "Logger" class as a wrapper to the stdout writers and [Conditional("DEBUG")] it

  • Use Fody and re-weave the assemblies to remove/NOP/redirect the WriteLine

    • I do this to redirect the calls to in internal log and upon crash or user stat requests, forward this log to our crash reporting servers.
  • etc, .....

Ref: Xamarin's NSLogWriter Helper Class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download