我目前正在使用CloudConfigurationManager.GetSetting("setting")我的应用程序获取设置,但是它正在将所有检查的内容写入控制台(在Debug和Release中):
Getting "setting" from ServiceRuntime: FAIL.
Getting "setting" from ConfigurationManager: PASS (Data Source=...
Getting "setting" from ServiceRuntime: FAIL.
Getting "setting" from ConfigurationManager: PASS (Data Source=...
Run Code Online (Sandbox Code Playgroud)
有没有办法阻止它这样做,或者是一个不那么冗长的替代版本?
大多数情况下,我只是喜欢我的单元测试输出很干净,但我也有点担心它会在生产服务器上以纯文本形式打印连接字符串(以及密码)等内容.
小智 12
CloudConfigurationManager.GetSetting现在有一个带有参数的方法重载outputResultsToTrace.
如果您传递false给此方法,那么它将禁用Trace.WriteLine其他地方用于"垃圾" Trace日志.
所以
var mySetting = CloudConfigurationManager.GetSetting("MySetting");
Run Code Online (Sandbox Code Playgroud)
变
var mySetting = CloudConfigurationManager.GetSetting("MySetting", false);
Run Code Online (Sandbox Code Playgroud)
我发现这个通过直接在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
San*_*tia 11
并不是的.如果你看一下底层GetValue方法的代码,你会看到:
private static string GetValue(string providerName, string settingName, Func<string, string> getValue)
{
string str1 = getValue(settingName);
string str2;
if (str1 != null)
str2 = string.Format((IFormatProvider) CultureInfo.InvariantCulture, "PASS ({0})", new object[1]
{
(object) str1
});
else
str2 = "FAIL";
Trace.WriteLine(string.Format((IFormatProvider) CultureInfo.InvariantCulture, "Getting \"{0}\" from {1}: {2}.", (object) settingName, (object) providerName, (object) str2));
return str1;
}
Run Code Online (Sandbox Code Playgroud)
始终调用Trace.WriteLine而不考虑Debug或Release.现在您可以简单地删除应该禁止所有消息的Default侦听器:
<system.diagnostics>
<trace>
<listeners>
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
Run Code Online (Sandbox Code Playgroud)
现在,如果你看一下CloudConfigurationManager它并没有做那么多.如果这对你来说是一个问题,你可以自己做点什么,从这开始:
if (RoleEnvironment.IsAvailable)
return RoleEnvironment.GetConfigurationSettingValue(setting);
else
return ConfigurationManager.AppSettings[setting];
Run Code Online (Sandbox Code Playgroud)
注意:CloudConfigurationManager比这更多,比如加载没有程序集引用的程序集.
| 归档时间: |
|
| 查看次数: |
5735 次 |
| 最近记录: |