/// <summary> /// <para> /// Initializes the diagnostic backend for the zero configuration logging option. /// </para> /// <para> /// System.Diagnostic output does not go to log files, nor is there the same level of control /// (rolling logs, etc) as offered by log4NET. So all zero-configuration options are treated the /// same by this class, and no setup of System.Diagnostics is performed. /// </para> /// <para> /// The one action we do take is if the "source" property is not in the configuration, we put the /// value "TopCoder Logger" as the value of this property into the configuration. /// </para> /// <para> /// New in 3.0. /// </para> /// </summary> /// <param name="option">The option about which zero-configuration setup should be configured in /// the backend.</param> /// <param name="configuration">The configuration object to which needed configuration settings are /// added.</param> /// <exception cref="ArgumentNullException">If configuration is null.</exception> /// <exception cref="ConfigException"> /// If any error occurs when accessing the configuration. /// </exception> public static void InitializeZeroConfiguration(ZeroConfigurationOption option, IConfiguration configuration) { Helper.ValidateNotNull(configuration, "configuration"); try { if (configuration.GetSimpleAttribute(SOURCE) == null) { // set the property to default value in configuration configuration.SetSimpleAttribute(SOURCE, DEFAULT_SOURCE); } } catch (Exception e) { throw new ConfigException("Error occurs when accessing the configuration", e); } }
/// <summary> /// <para> /// Initializes the configuration. Sets attribute "isInitiaized" in configuration to true. /// </para> /// </summary> /// <param name="option">The option about which zero-configuration setup should be configured in /// the backend.</param> /// <param name="configuration">The configuration object to which needed configuration settings are /// added.</param> public static void InitializeZeroConfiguration(ZeroConfigurationOption option, IConfiguration configuration) { configuration.SetSimpleAttribute("isInitiaized", "true"); }
/// <summary> /// <para> /// Writes the configuration file using given writer according to the zero-configuration option. /// </para> /// </summary> /// <param name="writer">The writer to write.</param> /// <param name="option">The option about which zero-configuration setup should be configured in /// the backend.</param> private static void WriteConfigFile(XmlTextWriter writer, ZeroConfigurationOption option) { // write log4net element writer.WriteStartElement(LOG4NET_ELEMENT); // write appender element writer.WriteStartElement(APPENDER_ELEMENT); // write name attribute of appender element writer.WriteAttributeString(APPENDER_NAME_ATTRIBUTE, APPENDER_NAME); // write type attribute and the children elements of appender element if (option == ZeroConfigurationOption.Test) { WriteFileAppenderConfig(writer, @"..\..\test_files\log.txt"); } else if (option == ZeroConfigurationOption.Component) { WriteFileAppenderConfig(writer, @"log.txt"); } else { WriteRollingFileAppenderConfig(writer, option); } // write layout element WriteLayoutConfig(writer); // end append element writer.WriteEndElement(); // write root element writer.WriteStartElement(ROOT_ELEMENT); // write appender-ref element of root element writer.WriteStartElement(APPENDER_REF_ELEMENT); // write ref attribute of appender-ref element writer.WriteAttributeString(APPENDER_REF_ATTRIBUTE, APPENDER_NAME); // end appender-ref element writer.WriteEndElement(); // write level element if (option == ZeroConfigurationOption.Component) { WriteElement(writer, LEVEL_ELEMENT, "INFO"); } else if (option == ZeroConfigurationOption.ClientStress) { WriteElement(writer, LEVEL_ELEMENT, "ERROR"); } else if (option == ZeroConfigurationOption.Release) { WriteElement(writer, LEVEL_ELEMENT, "WARN"); } // end root element writer.WriteEndElement(); // end log4net element writer.WriteEndElement(); }
/// <summary> /// <para> /// Writes the type attribute and children elements of RollingFileAppender element. /// </para> /// </summary> /// <param name="writer">The writer to write.</param> /// <param name="option">The option about which zero-configuration setup should be configured in /// the backend.</param> private static void WriteRollingFileAppenderConfig(XmlTextWriter writer, ZeroConfigurationOption option) { // write type attribute writer.WriteAttributeString(APPENDER_TYPE_ATTRIBUTE, "log4net.Appender.RollingFileAppender"); // write file element WriteElement(writer, APPENDER_FILE_ELEMENT, @"logs\log"); // write datePattern element (the file will be like log.2008-01-01.txt) WriteElement(writer, APPENDER_DATAPATTERN_ELEMENT, @".yyyy-MM-dd.\tx\t"); // write staticLogFileName element (set to false) WriteElement(writer, APPENDER_STATICLOGFILENAME_ELEMENT, "false"); if (option == ZeroConfigurationOption.Certification) { // for Certification option, roll daily with no limit on number of logs // write rollingStyle element WriteElement(writer, APPENDER_ROLLINGSTYLE_ELEMENT, "Date"); // write maxSizeRollBackups element WriteElement(writer, APPENDER_MAXSIZEROLLBACKUPS_ELEMENT, "-1"); } else { // for other options, roll daily and keep only the 30 most recent logs // write rollingStyle element WriteElement(writer, APPENDER_ROLLINGSTYLE_ELEMENT, "Composite"); // write maxSizeRollBackups element WriteElement(writer, APPENDER_MAXSIZEROLLBACKUPS_ELEMENT, "30"); } }
/// <summary> /// <para> /// Initializes the log4Net backend for the zero configuration logging option. /// </para> /// <para> /// If the "config_file" property is not in the configuration, the value "log4net.config" will be /// put as the value of this property into the configuration. /// </para> /// <para> /// If the config file does not exist, an appropriate log4Net configuration file will be written /// out to this location according to the zero-configuration option: /// </para> /// <list type="bullet"> /// <item> /// <term>Test</term> /// <description>The configuration uses a FileAppender that writes to the file /// ../../test_files/log.txt.</description> /// </item> /// <item> /// <term>Component</term> /// <description>The configuration uses a FileAppender that writes to the file ./log.txt. /// </description> /// </item> /// <item> /// <term>Certification</term> /// <description>The configuration uses a RollingFileAppender to write to the logs folder. /// Files will be rolled over daily, using a pattern of ¡°yyyy-mm-dd" for the files. The /// configuration is set up so as never to delete old logs. /// </description> /// </item> /// <item> /// <term> Client Debug</term> /// <description>This is the same as certification, but configured to keep only the 30 most recent /// logs.</description> /// </item> /// <item> /// <term>Client Stress</term> /// <description>This is the same as client debug, but configured so that only messages at the /// Level.Error (this is the log4net level) or higher are recorded.</description> /// </item> /// <item> /// <term>Release</term> /// <description>This is the same as client debug, but configured so that only messages at the /// Level.Warn (this.is the log4net level) or higher are recorded.</description> /// </item> /// </list> /// <para> /// If the config file does exist, we assume it is from a previous run and do not alter it. /// </para> /// <para> /// New in 3.0. /// </para> /// </summary> /// <param name="option">The option about which zero-configuration setup should be configured in /// the backend.</param> /// <param name="configuration">The configuration object to which needed configuration settings are /// added.</param> /// <exception cref="ArgumentNullException">If configuration is null.</exception> /// <exception cref="ConfigException"> /// If any error occurs when accessing the configuration or writing the configuration file. /// </exception> public static void InitializeZeroConfiguration(ZeroConfigurationOption option, IConfiguration configuration) { Helper.ValidateNotNull(configuration, "configuration"); // load the config file from configuration (optional) string configFile = Helper.GetStringAttribute(configuration, CONFIG_FILE, false); // set the property to default value in configuration if not present if (configFile == null) { configFile = DEFAULT_CONFIG_FILE; try { configuration.SetSimpleAttribute(CONFIG_FILE, configFile); } catch (Exception e) { throw new ConfigException("Error occurs when accessing the configuration", e); } } // check the config file exist or not if (!File.Exists(configFile)) { try { // create a log4Net configuration file using (XmlTextWriter writer = new XmlTextWriter(configFile, null)) { // enable indent writer.Formatting = Formatting.Indented; // write the configuration file WriteConfigFile(writer, option); writer.Flush(); } } catch (Exception e) { throw new ConfigException("Fail to write the configuration file", e); } } }