本文介绍了如何使CloudConfigurationManager.GetSetting不再那么冗长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在使用 CloudConfigurationManager.GetSetting("setting") 获取我的应用程序的设置,但是它将正在检查的所有内容的日志写入控制台(在Debug和Release中):

I'm currently using CloudConfigurationManager.GetSetting("setting") to get settings for my application, but it's writing logs of everything it's checking to the console (in both Debug and 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=...

有什么方法可以阻止它执行此操作,还是可以使用更详细的替代版本?

Is there any way to prevent it from doing this, or an alternative version that's less verbose?

大多数情况下,我只是喜欢单元测试的输出是干净整洁的,但是我也有点担心它会在生产服务器上以纯文本的形式打印出诸如连接字符串(因此是密码)之类的东西.

Mostly I just like my unit test output to be nice and clean, but I'm also a little concerned that it's printing out things like connection strings (and hence passwords) in plain text on the production server.

推荐答案

不是.如果您查看基础GetValue方法的代码,则会看到以下内容:

Not really. If you look at the code of the underlying GetValue method you'll see this:

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;
}

总是在不考虑调试"或释放"的情况下调用Trace.WriteLine.现在,您只需删除默认侦听器即可删除所有消息:

The Trace.WriteLine is always called without taking into account Debug or Release. Now you can simply remove the Default listener which should suppress all messages:

  <system.diagnostics>
    <trace>
      <listeners>
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>

现在,如果您查看CloudConfigurationManager,它并不会做很多事情.如果这对您来说是个问题,您可以自己做一些事情,从此开始:

Now if you look at the CloudConfigurationManager it doesn't do that much. If this is a problem for you you can cook up something yourself, starting with this:

        if (RoleEnvironment.IsAvailable)
            return RoleEnvironment.GetConfigurationSettingValue(setting);
        else
            return ConfigurationManager.AppSettings[setting];

注意:CloudConfigurationManager的作用远不止于此,例如,在没有程序集引用的情况下加载程序集.

Note: The CloudConfigurationManager does a lot more than this, like loading the assembly without assembly reference.

这篇关于如何使CloudConfigurationManager.GetSetting不再那么冗长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!