本文介绍了在.NET / Visual Studio中定义TRACE常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Visual Studio 2010中,如果您转到项目的属性并转到构建选项卡,则会显示定义TRACE常量复选框。相当于做一个#define TRACE。



System.Diagnostics.Trace的所有方法都有一个 [Conditional(TRACE)]



我的问题是为什么你会把它关闭?我的意思是,如果您没有定义任何跟踪监听器,那么就像您要填写日志或其他东西一样。对我来说感觉很奇怪如果您正在努力调用Trace,为什么您不想通过App / Web.config来控制它,而是通过编译器开关来控制它,这样就排除了可以在没有重新编译我/我缺少一些东西?



当然,对于表演来说,这不可能是坏的吧?

解决方案

大概这个复选框相当于 / define:TRACE 选项。您可能希望关闭发布版本,因为您不希望最终用户由于某些原因(例如安全性)来查看跟踪输出,或者提高性能。当然,性能的提升将取决于当前的工作情况,而将导致编译器从生成的IL中完全删除函数调用(包括任何字符串格式化等),所以它可以有显着差异。


In Visual Studio 2010, if you go to a project's properties and go to the Build Tab, there is a checkbox for "Define TRACE Constant." Which is the equivalent of doing a #define TRACE.

All of the methods of System.Diagnostics.Trace have a [Conditional("TRACE")] around them.

My question is why would you ever turn this off? I mean, if you don't have any trace listeners defined, then it isn't as if you're going to fill up a log or something. It just feels weird to me. If you are going through the effort to put in calls to Trace, why would you want to not control it through the App/Web.config, but instead control it via a compiler switch, which rules out the possibility of turning it back on without a recompile.

Am I missing something? Surely, it can't be THAT bad for performance, right?

解决方案

Presumably this checkbox is equivalent to the /define:TRACE compiler option. You might want to turn this option off for a release build either because you don't want end users to see the trace output for some reason (e.g. security), or to improve performance. Of course, the performance increase will depend on how much work is being done when it's on, but the Conditional attribute will cause the compiler to completely remove the function call (including any string formatting, etc.) from the generated IL, so it could make a significant difference.

这篇关于在.NET / Visual Studio中定义TRACE常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:30