Xamarin.iOS版本中的Console.WriteLine调用会发生什么情况? [英] What happens to Console.WriteLine calls in Xamarin.iOS release builds?

查看:147
本文介绍了Xamarin.iOS版本中的Console.WriteLine调用会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用XCode开发iOS应用时,可以使用NSLog调用将信息写入控制台以进行调试。在Xamarin.iOS中,也可以使用Console.WriteLine来完成此操作。

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:

#if DEBUG
Console.WriteLine("foo");
#endif

因此,发行版中甚至不存在这些调用。我很欣赏这可能是微优化-但我很好奇Xamairn是否已经这样做了,这使它不必要了吗?

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?

推荐答案

控制台的输出未在iOS版本中删除。

Console output is not removed in iOS release builds.

仅供参考:Xcode不会自动删除 NSLog print 来自Obj-C / Swift的发布代码。 (如果需要,通常定义一个全局预处理器宏以将其全部删除...)

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 预处理器,如您在问题中所示。

  • DEBUG preprocessor as you show in your question.

使用 System.Diagnostic.Debug.WriteLine


由于应用了[Conditional( DEBUG)]属性,编译器将删除对Debug。*的任何调用。

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




  • 创建自己的 Logger类作为 stdout 作家和 [Conditional( DEBUG)]

    使用 Fody 并重新编织程序集以删除/ NOP /重定向 WriteLine

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


    • 我这样做是为了将调用重定向到内部日志中,并在崩溃或用户状态请求后,将此日志转发到崩溃报告服务器。

    etc,.....

    etc, .....

    参考:Xamarin的 NSLogWriter 助手类

    Ref: Xamarin's NSLogWriter Helper Class

    这篇关于Xamarin.iOS版本中的Console.WriteLine调用会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆