我目前正在使用CloudConfigurationManager.GetSetting("setting")
获取我的应用程序的设置,但它将检查的所有内容的日志写入控制台(在调试和发布中):
Getting "setting" from ServiceRuntime: FAIL.
Getting "setting" from ConfigurationManager: PASS (Data Source=...
Getting "setting" from ServiceRuntime: FAIL.
Getting "setting" from ConfigurationManager: PASS (Data Source=...
有什么方法可以阻止它这样做,或者有一个更简洁的替代版本吗?
大多数情况下,我只是希望单元测试输出干净整洁,但我也有点担心它会在生产服务器上以纯文本形式打印连接字符串(以及密码)等内容。
最佳答案
CloudConfigurationManager.GetSetting
现在有一个方法重载,其参数名为 outputResultsToTrace
。
如果将 false
传递给此方法,那么它将禁用其他地方使用的 Trace.WriteLine
来“垃圾邮件”Trace
日志.
所以
var mySetting = CloudConfigurationManager.GetSetting("MySetting");
变成了
var mySetting = CloudConfigurationManager.GetSetting("MySetting", false);
我通过直接查看 GitHub 上的源代码发现了这个:https://github.com/Azure/azure-sdk-for-net/blob/52fc67253a176bea01c37c164f71c7eba8eaedba/src/Common/Configuration/CloudConfigurationManager.cs#L35
可能值得一提的是,这种重载没有记录:https://msdn.microsoft.com/en-us/library/azure/mt634648.aspx
所以我不确定它是否是 API 的官方且受支持的部分,或者它是否将来可能会更改或消失。
此更改于 2015 年底进行:https://github.com/Azure/azure-sdk-for-net/commit/e14398136d7d3b6d5e4675f1e8ccbdd37a8c6b01
关于c# - 如何使 CloudConfigurationManager.GetSetting 更简洁?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837191/