Guest User

Untitled

a guest
Jul 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using log4net;
  6. using System.Reflection;
  7. using System.Configuration;
  8.  
  9. namespace Ideal.Web
  10. {
  11. public class AppSettings
  12. {
  13. private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  14.  
  15. #region AppSettings Helpers
  16.  
  17. /// <summary>
  18. /// Gets a configuration setting with a character value
  19. /// </summary>
  20. /// <param name="key">The setting key</param>
  21. /// <param name="defaultCharacter">The default to use if a configuration value is not found</param>
  22. /// <returns>The configured value, or defaultCharacter if no configured value is found.</returns>
  23. public static char GetConfigurationCharacter(string key, char defaultCharacter)
  24. {
  25.  
  26. if (string.IsNullOrEmpty(key))
  27. {
  28. return defaultCharacter;
  29. }
  30.  
  31. try
  32. {
  33. string configurationValue = ConfigurationManager.AppSettings[key];
  34. if (configurationValue == null)
  35. {
  36. throw new AppSettingsException();
  37. }
  38. return configurationValue[0];
  39. }
  40. catch
  41. {
  42. string message =
  43. string.Format("Could not find configured value for key: {0}. Using default instead: {1}",
  44. key, defaultCharacter);
  45. log.Warn(message);
  46. return defaultCharacter;
  47. }
  48.  
  49. }
  50.  
  51. public static string GetConfigurationString(string key)
  52. {
  53. return GetConfigurationString(key, string.Empty);
  54. }
  55.  
  56. /// <summary>
  57. /// Gets a configuration setting with a string value
  58. /// </summary>
  59. /// <param name="key">The setting key</param>
  60. /// <param name="defaultString">The default to use if a configuration value is not found</param>
  61. /// <returns>The configured value, or defaultString if no configured value is found.</returns>
  62. public static string GetConfigurationString(string key, string defaultString)
  63. {
  64.  
  65. if (string.IsNullOrEmpty(key))
  66. {
  67. return defaultString;
  68. }
  69.  
  70. try
  71. {
  72. string configurationValue = ConfigurationManager.AppSettings[key];
  73. if (configurationValue == null)
  74. {
  75. throw new AppSettingsException();
  76. }
  77. return configurationValue;
  78. }
  79. catch
  80. {
  81. string message =
  82. string.Format("Could not find configured value for key: {0}. Using default instead: {1}",
  83. key, defaultString);
  84. log.Warn(message);
  85. return defaultString;
  86. }
  87.  
  88.  
  89. }
  90.  
  91. /// <summary>
  92. /// Gets a configuration setting with a boolean value
  93. /// </summary>
  94. /// <param name="key">The setting key</param>
  95. /// <param name="defaultBoolean">The default to use if a configuration value is not found</param>
  96. /// <returns>The configured value, or defaultBoolean if no configured value is found.</returns>
  97. public static bool GetConfigurationBoolean(string key, bool defaultBoolean)
  98. {
  99. if (string.IsNullOrEmpty(key))
  100. {
  101. return defaultBoolean;
  102. }
  103. try
  104. {
  105. string configurationValue = ConfigurationManager.AppSettings[key];
  106. if (configurationValue == null)
  107. {
  108. throw new AppSettingsException();
  109. }
  110. return Convert.ToBoolean(configurationValue);
  111. }
  112. catch
  113. {
  114. string message =
  115. string.Format("Could not find configured boolean for key: {0}. Using default instead: {1}",
  116. key, defaultBoolean);
  117. log.Warn(message);
  118. return defaultBoolean;
  119. }
  120.  
  121. }
  122.  
  123. /// <summary>
  124. /// Gets a configuration setting with an integer value
  125. /// </summary>
  126. /// <param name="key">The setting key</param>
  127. /// <param name="defaultInteger">The default integer.</param>
  128. /// <returns>
  129. /// The configured value, or defaultInteger if no configured value is found.
  130. /// </returns>
  131. public static int GetConfigurationInteger(string key, int defaultInteger)
  132. {
  133.  
  134. if (string.IsNullOrEmpty(key))
  135. {
  136. return defaultInteger;
  137. }
  138.  
  139.  
  140. try
  141. {
  142. string configurationValue = ConfigurationManager.AppSettings[key];
  143. if (configurationValue == null)
  144. {
  145. throw new AppSettingsException();
  146. }
  147. return Convert.ToInt32(configurationValue);
  148. }
  149. catch
  150. {
  151. string message =
  152. string.Format("Could not find configured integer for key: {0}. Using default instead: {1}",
  153. key, defaultInteger);
  154. log.Warn(message);
  155. return defaultInteger;
  156. }
  157.  
  158. }
  159.  
  160. /// <summary>
  161. /// Gets a configuration setting with a double value
  162. /// </summary>
  163. /// <param name="key">The setting key</param>
  164. /// <param name="defaultDouble">The default to use if a configuration value is not found</param>
  165. /// <returns>The configured value, or defaultDouble if no configured value is found.</returns>
  166. public static double GetConfigurationDouble(string key, double defaultDouble)
  167. {
  168. if (string.IsNullOrEmpty(key))
  169. {
  170. return defaultDouble;
  171. }
  172.  
  173.  
  174. try
  175. {
  176. string configurationValue = ConfigurationManager.AppSettings[key];
  177. if (configurationValue == null)
  178. {
  179. throw new AppSettingsException();
  180. }
  181. return Convert.ToDouble(configurationValue);
  182. }
  183. catch
  184. {
  185. string message =
  186. string.Format("Could not find configured double for key: {0}. Using default instead: {1}",
  187. key, defaultDouble);
  188. log.Warn(message);
  189. return defaultDouble;
  190. }
  191.  
  192. }
  193.  
  194. /// <summary>
  195. /// Gets a configuration value.
  196. /// </summary>
  197. /// <param name="key">The key to retrieve.</param>
  198. /// <param name="defaultValue">The default value.</param>
  199. /// <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>
  200. /// <returns>
  201. /// A value of type <typeparam name="T">T</typeparam> retrieved from the XML configuration file, or the default if no such key exists
  202. /// </returns>
  203. /// <exception cref="System.Exception">Thrown if the type cannot be converted automatically</exception>
  204. public static T GetConfigurationValue<T>(string key, T defaultValue, bool warnOnMissingValue)
  205. {
  206.  
  207. string configurationValue = ConfigurationManager.AppSettings[key];
  208. if (configurationValue == null)
  209. {
  210. if (warnOnMissingValue)
  211. {
  212. log.Warn(string.Format("Could not find configuration value for key: {0}; using default: {1}", key,
  213. defaultValue.ToString()));
  214. }
  215. return defaultValue;
  216. }
  217. return GenericConverter.GetValue(configurationValue, defaultValue);
  218. }
  219.  
  220. /// <summary>
  221. /// Gets a configuration value.
  222. /// </summary>
  223. /// <param name="key">The key to retrieve.</param>
  224. /// <param name="defaultValue">The default value.</param>
  225. /// <returns>
  226. /// A value of type <typeparam name="T">T</typeparam> retrieved from the XML configuration file, or the default if no such key exists
  227. /// </returns>
  228. public static T GetConfigurationValue<T>(string key, T defaultValue)
  229. {
  230. return GetConfigurationValue(key, defaultValue, true);
  231. }
  232.  
  233. #endregion
  234.  
  235. }
  236. }
Add Comment
Please, Sign In to add comment