How do I make CloudConfigurationManager.GetSetting less verbose?

后端 未结 8 1380
天命终不由人
天命终不由人 2021-01-04 01:05

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

相关标签:
8条回答
  • 2021-01-04 01:25

    Fixed in version 3.0.0. Please update Microsoft Azure Configuration Manager nuget package.

    0 讨论(0)
  • 2021-01-04 01:33

    I had quite similar problems. I updated from Azure SDK 2.0 to 2.2 - during this process I used the NuGet Manager to update the Microsoft.WindowsAzure.Storage to the latest. The PackageManager automatically took Microsoft.WindowsAzure.Configuration to 1.8.0.0. I was not able to get this running (it was for .Net 2.0!?). After I manually set all References to

    • Microsoft.WindowsAzure.Storage 2.1.0.0
    • Microsoft.WindowsAzure.Configuration 2.0.0.0

    everything worked.

    I think this is because of the way CloudConfigurationManager.GetSetting loads the assembly and calls the funktions (via reflection).

    0 讨论(0)
  • 2021-01-04 01:40

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

    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>
    

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

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

    0 讨论(0)
  • 2021-01-04 01:40

    CloudConfigurationManager.GetSetting now has a method overload with a parameter called outputResultsToTrace.

    If you pass false to this method, then it'll disable the Trace.WriteLine used elsewhere to "spam" the Trace log.

    So

    var mySetting = CloudConfigurationManager.GetSetting("MySetting");
    

    becomes

    var mySetting = CloudConfigurationManager.GetSetting("MySetting", false);
    

    I found this by looking directly at the source code on GitHub: https://github.com/Azure/azure-sdk-for-net/blob/52fc67253a176bea01c37c164f71c7eba8eaedba/src/Common/Configuration/CloudConfigurationManager.cs#L35

    It's probably worth mentioning that this overload is not documented: https://msdn.microsoft.com/en-us/library/azure/mt634648.aspx

    So I'm not sure if it's an official and supported part of the API, or if it's something that might change or go away in the future.

    This change was made at the end of 2015: https://github.com/Azure/azure-sdk-for-net/commit/e14398136d7d3b6d5e4675f1e8ccbdd37a8c6b01

    0 讨论(0)
  • 2021-01-04 01:44

    Alternative option, if you're calling CloudConfigurationManager.GetSetting() in one part (ie, a wrapper/helper class):

    var oldListeners = Trace.Listeners.Cast<TraceListener>().ToArray();
    Trace.Listeners.Clear();
    
    var stringValue = CloudConfigurationManager.GetSetting(key);
    Trace.Listeners.AddRange(oldListeners);
    

    First, we remove all listeners on Trace. Then we grab the setting, and re-add the listeners. Of course, this potentially could cause problems with threaded applications

    0 讨论(0)
  • 2021-01-04 01:45

    I just installed the nuget package Microsoft.WindowsAzure.ConfigurationManager version 3.1.0, and I can confirm that the issue has been fixed in this version. Taking a look at the github issue referenced in the question's comments, we can see, in the changelog, the fetched value is no longer written to the trace output. In particular, the trace message has been changed from:

    message = string.Format(CultureInfo.InvariantCulture, "PASS ({0})", value);
    

    to:

    message = "PASS";
    

    This still makes the configuration manager quite verbose though.

    0 讨论(0)
提交回复
热议问题