Created
January 19, 2011 15:39
-
-
Save wayne-o/786323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Web; | |
using log4net; | |
using System.Reflection; | |
using System.Configuration; | |
namespace Ideal.Web | |
{ | |
public class AppSettings | |
{ | |
private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | |
#region AppSettings Helpers | |
/// <summary> | |
/// Gets a configuration setting with a character value | |
/// </summary> | |
/// <param name="key">The setting key</param> | |
/// <param name="defaultCharacter">The default to use if a configuration value is not found</param> | |
/// <returns>The configured value, or defaultCharacter if no configured value is found.</returns> | |
public static char GetConfigurationCharacter(string key, char defaultCharacter) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return defaultCharacter; | |
} | |
try | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
throw new AppSettingsException(); | |
} | |
return configurationValue[0]; | |
} | |
catch | |
{ | |
string message = | |
string.Format("Could not find configured value for key: {0}. Using default instead: {1}", | |
key, defaultCharacter); | |
log.Warn(message); | |
return defaultCharacter; | |
} | |
} | |
public static string GetConfigurationString(string key) | |
{ | |
return GetConfigurationString(key, string.Empty); | |
} | |
/// <summary> | |
/// Gets a configuration setting with a string value | |
/// </summary> | |
/// <param name="key">The setting key</param> | |
/// <param name="defaultString">The default to use if a configuration value is not found</param> | |
/// <returns>The configured value, or defaultString if no configured value is found.</returns> | |
public static string GetConfigurationString(string key, string defaultString) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return defaultString; | |
} | |
try | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
throw new AppSettingsException(); | |
} | |
return configurationValue; | |
} | |
catch | |
{ | |
string message = | |
string.Format("Could not find configured value for key: {0}. Using default instead: {1}", | |
key, defaultString); | |
log.Warn(message); | |
return defaultString; | |
} | |
} | |
/// <summary> | |
/// Gets a configuration setting with a boolean value | |
/// </summary> | |
/// <param name="key">The setting key</param> | |
/// <param name="defaultBoolean">The default to use if a configuration value is not found</param> | |
/// <returns>The configured value, or defaultBoolean if no configured value is found.</returns> | |
public static bool GetConfigurationBoolean(string key, bool defaultBoolean) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return defaultBoolean; | |
} | |
try | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
throw new AppSettingsException(); | |
} | |
return Convert.ToBoolean(configurationValue); | |
} | |
catch | |
{ | |
string message = | |
string.Format("Could not find configured boolean for key: {0}. Using default instead: {1}", | |
key, defaultBoolean); | |
log.Warn(message); | |
return defaultBoolean; | |
} | |
} | |
/// <summary> | |
/// Gets a configuration setting with an integer value | |
/// </summary> | |
/// <param name="key">The setting key</param> | |
/// <param name="defaultInteger">The default integer.</param> | |
/// <returns> | |
/// The configured value, or defaultInteger if no configured value is found. | |
/// </returns> | |
public static int GetConfigurationInteger(string key, int defaultInteger) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return defaultInteger; | |
} | |
try | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
throw new AppSettingsException(); | |
} | |
return Convert.ToInt32(configurationValue); | |
} | |
catch | |
{ | |
string message = | |
string.Format("Could not find configured integer for key: {0}. Using default instead: {1}", | |
key, defaultInteger); | |
log.Warn(message); | |
return defaultInteger; | |
} | |
} | |
/// <summary> | |
/// Gets a configuration setting with a double value | |
/// </summary> | |
/// <param name="key">The setting key</param> | |
/// <param name="defaultDouble">The default to use if a configuration value is not found</param> | |
/// <returns>The configured value, or defaultDouble if no configured value is found.</returns> | |
public static double GetConfigurationDouble(string key, double defaultDouble) | |
{ | |
if (string.IsNullOrEmpty(key)) | |
{ | |
return defaultDouble; | |
} | |
try | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
throw new AppSettingsException(); | |
} | |
return Convert.ToDouble(configurationValue); | |
} | |
catch | |
{ | |
string message = | |
string.Format("Could not find configured double for key: {0}. Using default instead: {1}", | |
key, defaultDouble); | |
log.Warn(message); | |
return defaultDouble; | |
} | |
} | |
/// <summary> | |
/// Gets a configuration value. | |
/// </summary> | |
/// <param name="key">The key to retrieve.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <param name="warnOnMissingValue">if set to <c>true</c> put a warning in the log when the key is not found in the configuration file.</param> | |
/// <returns> | |
/// A value of type <typeparam name="T">T</typeparam> retrieved from the XML configuration file, or the default if no such key exists | |
/// </returns> | |
/// <exception cref="System.Exception">Thrown if the type cannot be converted automatically</exception> | |
public static T GetConfigurationValue<T>(string key, T defaultValue, bool warnOnMissingValue) | |
{ | |
string configurationValue = ConfigurationManager.AppSettings[key]; | |
if (configurationValue == null) | |
{ | |
if (warnOnMissingValue) | |
{ | |
log.Warn(string.Format("Could not find configuration value for key: {0}; using default: {1}", key, | |
defaultValue.ToString())); | |
} | |
return defaultValue; | |
} | |
return GenericConverter.GetValue(configurationValue, defaultValue); | |
} | |
/// <summary> | |
/// Gets a configuration value. | |
/// </summary> | |
/// <param name="key">The key to retrieve.</param> | |
/// <param name="defaultValue">The default value.</param> | |
/// <returns> | |
/// A value of type <typeparam name="T">T</typeparam> retrieved from the XML configuration file, or the default if no such key exists | |
/// </returns> | |
public static T GetConfigurationValue<T>(string key, T defaultValue) | |
{ | |
return GetConfigurationValue(key, defaultValue, true); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment