ApplicationConfiguration.cs 17.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
namespace SysFramework
{
    using System;
    using System.Collections;
    using System.Diagnostics;
    using System.Configuration;
    using System.Xml;
    using System.Collections.Specialized;
    
    /// <summary>
    ///     Standard configuration settings to enable tracing and logging
    ///     with the ApplicationLog class.
    ///     <remarks> 
    ///         An application can use this class as a model for 
    ///         adding additional settings to a Web.Config(以后应改成escalade.config) file.
    ///         Special Considerations:
    ///         The OnApplicationStart function in this class must be called
    ///         from the Application_OnStart event in Global.asax. This is
    ///         currently used to determine the path of the application,
    ///         the HttpContext object is passed it to enable the app
    ///         to read other settings in the future, and to minimize the code
    ///         in global.asax. 
    ///         目前引用缺省的静态变量中的信息。
    ///         <example>
    ///             The global.asax file should be similar to the following code:
    ///             <code>
    ///                 <%@ Import Namespace="T1.SysFramework"  %>
    ///                 <script language="c#" runat="SERVER">
    ///                     void Application_OnStart()
    ///                     {
    ///                         ApplicationConfiguration.OnApplicationStart(Context);
    ///                     }
    ///                 </script>
    ///             </code>
    ///         </example>
    ///     </remarks>
    /// </summary>
    public class ApplicationConfiguration : IConfigurationSectionHandler
    {

        //
        // Constant values for all of the SysFramework standard settings
        //
        private const String TRACING_ENABLED = "SysFramework.Tracing.Enabled";
        private const String TRACING_TRACEFILE = "SysFramework.Tracing.TraceFile";
        private const String TRACING_TRACELEVEL = "SysFramework.Tracing.TraceLevel";
        private const String TRACING_SWITCHNAME = "SysFramework.Tracing.SwitchName";
        private const String TRACING_SWITCHDESCRIPTION = "SysFramework.Tracing.SwitchDescription";
        private const String EVENTLOG_ENABLED = "SysFramework.EventLog.Enabled";
        private const String EVENTLOG_MACHINENAME = "SysFramework.EventLog.Machine";
        private const String EVENTLOG_SOURCENAME = "SysFramework.EventLog.SourceName";
        private const String EVENTLOG_TRACELEVEL = "SysFramework.EventLog.LogLevel";
     
        
        //
        // The root directory of the application. Established in the
        //   OnApplicationStart callback form Global.asax.
        //
        private static String appRoot;
        
        //
        // Constant values for all of the default settings.
        //
        private const bool TRACING_ENABLED_DEFAULT = true;
        private const String TRACING_TRACEFILE_DEFAULT = "MyCMMITrace.txt";
        private const TraceLevel TRACING_TRACELEVEL_DEFAULT = TraceLevel.Verbose;
        private const String TRACING_SWITCHNAME_DEFAULT = "MyCMMITraceSwitch";
        private const String TRACING_SWITCHDESCRIPTION_DEFAULT = "MyCMMI error and tracing information";
        private const bool EVENTLOG_ENABLED_DEFAULT = true;
        private const String EVENTLOG_MACHINENAME_DEFAULT = ".";
        private const String EVENTLOG_SOURCENAME_DEFAULT = "MyCMMI";
        private const TraceLevel EVENTLOG_TRACELEVEL_DEFAULT = TraceLevel.Error;
        

		   
		//
		// Static member variables. These contain the application settings
		//   from Config.Web, or the default values.
		//
		private static bool tracingEnabled= TRACING_ENABLED_DEFAULT;
		private static String tracingTraceFile = TRACING_TRACEFILE_DEFAULT;
		private static TraceLevel tracingTraceLevel= TRACING_TRACELEVEL_DEFAULT;
		private static String tracingSwitchName= TRACING_SWITCHNAME_DEFAULT;
		private static String tracingSwitchDescription= TRACING_SWITCHDESCRIPTION_DEFAULT;
		private static bool eventLogEnabled= EVENTLOG_ENABLED_DEFAULT;
		private static String eventLogMachineName= EVENTLOG_MACHINENAME_DEFAULT;
		private static String eventLogSourceName= EVENTLOG_SOURCENAME_DEFAULT;
		private static TraceLevel eventLogTraceLevel= EVENTLOG_TRACELEVEL_DEFAULT;

        /// <summary>
        ///     Called from OnApplicationStart to initialize settings from
        ///     the Web.Config file(s). 
        ///     <remarks>
        ///         The app domain will restart if settings change, so there is 
        ///         no reason to read these values more than once. This funtion
        ///         uses the NameValueSectionHandler base class to generate a 
        ///         hashtablefrom the XML, which is then used to store the current
        ///         settings.  Because all settings are read here, we do not actually 
        ///         store the generated hashtable object for later retrieval by
        ///         Context.GetConfig. The application should use the accessor
        ///         functions directly.
        ///     </remarks>
        ///     <param name="parent">An object created by processing a section 
        ///         with this name in a Config.Web file in a parent directory.
        ///     </param>
        ///     <param name="configContext">The config's context.</param>
        ///     <param name="section">The section to be read.</param>
        ///     <retvalue>
        ///		    <para>
        ///             A ConfigOutput object: which we leave empty because all settings
        ///             are stored at this point.
        ///		    </para>
        ///		    <para>
        ///             null:  if there was an error.
        ///		    </para>
    	///	    </retvalue>
        /// </summary>
        public Object Create(Object parent, object configContext, XmlNode section)
        {
                    
            NameValueCollection settings;
                    
            try
            {
        	NameValueSectionHandler baseHandler = new NameValueSectionHandler();
                settings = (NameValueCollection)baseHandler.Create(parent, configContext, section);
            }
            catch
            {
                settings = null;
            }
        
            if (settings == null)
            {
                tracingEnabled = TRACING_ENABLED_DEFAULT;
                tracingTraceFile = TRACING_TRACEFILE_DEFAULT;
                tracingTraceLevel = TRACING_TRACELEVEL_DEFAULT;
                tracingSwitchName = TRACING_SWITCHNAME_DEFAULT;
                tracingSwitchDescription = TRACING_SWITCHDESCRIPTION_DEFAULT;
                eventLogEnabled = EVENTLOG_ENABLED_DEFAULT;
                eventLogMachineName = EVENTLOG_MACHINENAME_DEFAULT;
                eventLogSourceName = EVENTLOG_SOURCENAME_DEFAULT;
                eventLogTraceLevel = EVENTLOG_TRACELEVEL_DEFAULT;
            }
            else
            {
                tracingEnabled = ReadSetting(settings, TRACING_ENABLED, TRACING_ENABLED_DEFAULT);
                tracingTraceFile = ReadSetting(settings, TRACING_TRACEFILE, TRACING_TRACEFILE_DEFAULT);
                tracingTraceLevel = ReadSetting(settings, TRACING_TRACELEVEL, TRACING_TRACELEVEL_DEFAULT);
                tracingSwitchName = ReadSetting(settings, TRACING_SWITCHNAME, TRACING_SWITCHNAME_DEFAULT);
                tracingSwitchDescription = ReadSetting(settings, TRACING_SWITCHDESCRIPTION, TRACING_SWITCHDESCRIPTION_DEFAULT);
                eventLogEnabled = ReadSetting(settings, EVENTLOG_ENABLED, EVENTLOG_ENABLED_DEFAULT);
                eventLogMachineName = ReadSetting(settings, EVENTLOG_MACHINENAME, EVENTLOG_MACHINENAME_DEFAULT);
                eventLogSourceName = ReadSetting(settings, EVENTLOG_SOURCENAME, EVENTLOG_SOURCENAME_DEFAULT);
                eventLogTraceLevel = ReadSetting(settings, EVENTLOG_TRACELEVEL, EVENTLOG_TRACELEVEL_DEFAULT);
            }

            return null;
        }
        
        /// <summary>
        ///     String version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
    	///	    </retvalue>
        /// </summary>
        public static String ReadSetting(NameValueCollection settings, String key, String defaultValue)
        {
            try
            {
                Object setting = settings[key];
                
                return (setting == null) ? defaultValue : (String)setting;
            }
            catch
            {
                return defaultValue;
            }
        }
        
        /// <summary>
        ///     Boolean version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
    	///	    </retvalue>
        /// </summary>
        public static bool ReadSetting(NameValueCollection settings, String key, bool defaultValue)
        {
            try
            {
                Object setting = settings[key];
                
                return (setting == null) ? defaultValue : Convert.ToBoolean((String)setting);
            }
            catch
            {
                return defaultValue;
            }
        }
        
        /// <summary>
        ///     int version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
    	///	    </retvalue>
        /// </summary>
        public static int ReadSetting(NameValueCollection settings, String key, int defaultValue)
        {
            try
            {
                Object setting = settings[key];
                
                return (setting == null) ? defaultValue : Convert.ToInt32((String)setting);
            }
            catch
            {
                return defaultValue;
            }
        }
        
        /// <summary>
        ///     TraceLevel version of ReadSetting.
        ///     <remarks>
        ///         Reads a setting from a hashtable and converts it to the correct
        ///         type. One of these functions is provided for each type
        ///         expected in the hash table. These are public so that other
        ///         classes don't have to duplicate them to read settings from
        ///         a hash table.
        ///     </remarks>
        ///     <param name="settings">The Hashtable to read from.</param>
        ///     <param name="key">A key for the value in the Hashtable.</param>
        ///     <param name="defaultValue">The default value if the item is not found.</param>
        ///     <retvalue>
        ///		    <para>value: from the hash table</para>
        ///         <para>
        ///             default: if the item is not in the table or cannot be case to the expected type.
        ///		    </para>
    	///	    </retvalue>
        /// </summary>
        public static TraceLevel ReadSetting(NameValueCollection settings, String key, TraceLevel defaultValue)
        {
            try
            {
                Object setting = settings[key];
                
                return (setting == null) ? defaultValue : (TraceLevel)Convert.ToInt32((String)setting);
            }
            catch
            {
                return defaultValue;
            }
        }
        
        /// <summary>
        ///     Function to be called by Application_OnStart as described in the
        ///     class description. Initializes the application root.
        ///     <param name="myAppPath">The path of the running application.</param>
        /// </summary>
        public static void OnApplicationStart(String myAppPath)
        {
            appRoot = myAppPath;

            //修改编号:0001
            System.Configuration.ConfigurationManager.GetSection("ApplicationConfiguration");
            System.Configuration.ConfigurationManager.GetSection("MyCMMIConfiguration");
           
        }
        
        /// <value>
        ///     Property AppRoot is used to get the root path of the application.
        /// </value>
        public static String AppRoot
        {
            get
            {
                return appRoot;
            }
        }
        
        /// <value>
        ///     Property TracingEnabled is used to get the configuration setting, defaulting to False on error.
        /// </value>
        public static bool TracingEnabled
        {
            get
            {
                return tracingEnabled;
            }
        }
        
        /// <value>
        ///     Property TracingTraceFile is used to get the full path name to the file that contains trace 
        ///     settings, defaults to ApplicationTrace.txt.
        /// </value>
        public static String TracingTraceFile
        {
            get
            {
                return appRoot + "\\" + tracingTraceFile;
            }
        }
        
        /// <value>
        ///     Property TracingTraceFile is used to get the highest logging level that should be written to 
        ///     the tracing file, defaults to TraceLevel.Verbose (however, TracingEnabled defaults
        ///     to False, so you have to turn it on explicitly).
        /// </value>
        public static TraceLevel TracingTraceLevel
        {
            get
            {
                return tracingTraceLevel;
            }
        }
        
        /// <value>
        ///   Property TracingSwitchName is used to get the trace switch name, defaults to ApplicationTraceSwitch.
        /// </value>
        public static String TracingSwitchName
        {
            get
            {
                return tracingSwitchName;
            }
        }
        
        /// <value>
        ///   Property TracingSwitchDescription is used to get the trace settings file, defaults to 
        ///     "Application error and tracing information".
        /// </value>
        public static String TracingSwitchDescription
        {
            get
            {
                return tracingSwitchDescription;
            }
        }
        
        /// <value>
        ///     Property EventLogEnabled is used to get whether writing to the event log is support, defaults to True.
        ///     <remarks>Returns true if writing to the event log is enabled, false otherwise</remarks>
        /// </value>
        public static bool EventLogEnabled
        {
            get
            {
                return eventLogEnabled;
            }
        }
        /// <value>
        ///     Property EventLogMachineName is used to get the machine name to log the event to, defaults to an
        ///     empty string, indicating the current machine.  A machine name 
        ///     (without \\), may be empty.
        /// </value>
        public static String EventLogMachineName
        {
            get
            {
                return eventLogMachineName;
            }
        }
        
        /// <value>
        ///     Property EventLogMachineName is used to get the source of the error to be written to the event log, 
        ///     defaults WebApplication.
        /// </value>
        public static String EventLogSourceName
        {
            get
            {
                return eventLogSourceName;
            }
        }
        
        /// <value>
        ///     Property EventLogTraceLevel is used to get the highest logging level that should be written to the event log,
        ///     defaults to TraceLevel.Error.
        /// </value>
        public static TraceLevel EventLogTraceLevel
        {
            get
            {
                return eventLogTraceLevel;
            }
        }        
    }
}