public void TestCtorAccuracy() { NamedMessage msg = new NamedMessage(TEXT, NAME, list, LEVEL); Assert.IsNotNull(msg, "fail to create instance"); Assert.AreEqual(TEXT, msg.Text, "incorrect text"); Assert.AreEqual(NAME, msg.Name, "incorrect name"); Assert.AreEqual(LEVEL, msg.DefaultLevel, "incorrect default level"); Assert.AreNotSame(list, msg.ParameterNames, "shallow copy expected"); Assert.AreEqual(list.Count, msg.ParameterNames.Count, "incorrect number of parameters"); foreach (string s in list) { Assert.IsTrue(msg.ParameterNames.Contains(s), "incorrect parameter"); } }
protected void SetUp() { namedMsg = new NamedMessage(MESSAGE, NAMEDMESSAGE, new List<string>(PARAMETER_NAMES), Level.ERROR); config = new DefaultConfiguration("default"); config.SetSimpleAttribute("logger_name", LOGGER_NAME); config.SetSimpleAttribute("default_level", DEFAULT_LEVEL.ToString()); IConfiguration msgsConfig = new DefaultConfiguration("NamedMessages"); IConfiguration msgConfig = new DefaultConfiguration(NAMEDMESSAGE); msgConfig.SetSimpleAttribute("text", MESSAGE); msgConfig.SetSimpleAttribute("default_level", Level.DEBUG.ToString()); msgConfig.SetAttribute("parameters", new object[] {PARAMETER_NAMES[0], PARAMETER_NAMES[1]}); msgsConfig.AddChild(msgConfig); config.AddChild(msgsConfig); simpleLogger = new SimpleLogger(config); anotherLogger = new AnotherSimpleLogger(EXCEPTION_LOGGER_NAME); logger = new ExceptionSafeLogger(simpleLogger, anotherLogger); }
protected void SetUp() { namedMsg = new NamedMessage(MESSAGE, NAMEDMESSAGE, new List<string>(PARAMETER_NAMES), Level.ERROR); IDictionary<string, NamedMessage> msgs = new Dictionary<string, NamedMessage>(); msgs.Add(NAMEDMESSAGE, new NamedMessage(MESSAGE, NAMEDMESSAGE, new List<string>(PARAMETER_NAMES), Level.ERROR)); simpleLogger = new SimpleLogger(LOGGER_NAME, DEFAULT_LEVEL, msgs); filteredLevels = new List<Level>(); filteredLevels.Add(Level.FATAL); logger = new LevelFilteredLogger(simpleLogger, filteredLevels); }
public void TestLogNamedMessageAccuracy() { // remember the id of last message EventLog log = new EventLog("", MACHINE_NAME, SOURCE); int index = log.Entries.Count; namedMsg = new NamedMessage(MESSAGE, NAMEDMESSAGE, new List<string>(PARAMETER_NAMES), Level.ERROR); logger.LogNamedMessage(LEVEL, namedMsg, PARAMETERS); // check the message logged by service string msg = log.Entries[index].Message; Assert.IsTrue(msg.IndexOf(NAMED_FORMATTED) >= 0, "incorrect message"); Assert.IsTrue(msg.IndexOf("warn", StringComparison.OrdinalIgnoreCase) >= 0, "incorrect level"); // check the parameters are removed for (int i = 0; i < PARAMETER_NAMES.Length; i++) { Assert.IsNull(LogicalThreadContext.Properties[PARAMETER_NAMES[i]], "paramters in logical thread context should be removed"); } }
protected void SetUp() { namedMsg = new NamedMessage(MESSAGE, NAMEDMESSAGE, new List<string>(PARAMETER_NAMES), Level.ERROR); config = new DefaultConfiguration("default"); config.SetSimpleAttribute("logger_name", LOGNAME); config.SetSimpleAttribute("default_level", DEFAULT_LEVEL.ToString()); config.SetSimpleAttribute("config_file", CONFIG_FILE); IConfiguration msgsConfig = new DefaultConfiguration("NamedMessages"); IConfiguration msgConfig = new DefaultConfiguration(NAMEDMESSAGE); msgConfig.SetSimpleAttribute("text", MESSAGE); msgConfig.SetSimpleAttribute("default_level", Level.DEBUG); msgConfig.SetAttribute("parameters", new object[] {PARAMETER_NAMES[0], PARAMETER_NAMES[1]}); msgsConfig.AddChild(msgConfig); config.AddChild(msgsConfig); logger = new Log4NETImpl(config); }
/// <summary> /// <para> /// Logs the named message to the underlying implementation with the specified logging level. /// Delegates for the same named method in super class. /// </para> /// <para> /// If ex is not null, it will be thrown. /// </para> /// </summary> /// <param name="level">The level at which to log the message.</param> /// <param name="message">The named message to log.</param> /// <param name="param">The parameters to use in formatting the message.</param> /// <exception cref="ArgumentNullException">If any parameter is null.</exception> /// <exception cref="MessageFormattingException">If there is an error formatting the message from /// the params.</exception> /// <exception cref="LoggingException">If there is a failure in the backend logging system. /// </exception> public new void LogNamedMessage(Level level, NamedMessage message, params object[] param) { base.LogNamedMessage(level, message, param); }
/// <summary> /// <para> /// Logs the named message to the underlying implementation with the specified logging level. /// </para> /// <para> /// The message will not be logged if the given level is in the list of levels to be filtered. /// </para> /// </summary> /// <param name="level">The level at which to log the message.</param> /// <param name="message">The named message to log.</param> /// <param name="param">The parameters to use in formatting the message.</param> /// <exception cref="ArgumentNullException">If message or params is null.</exception> /// <exception cref="MessageFormattingException">If there is an error formatting the message with /// params.</exception> /// <exception cref="LoggingException">If an error occurs in the backend.</exception> protected internal override void LogNamedMessage(Level level, NamedMessage message, params object[] param) { Helper.ValidateNotNull(message, "message"); Helper.ValidateNotNull(param, "param"); if (!filteredLevels.Contains(level)) { underlyingLogger.LogNamedMessage(level, message, param); } }
/// <summary> /// <para> /// Loads the attributes in given configuration to create a NamedMessage object and add it to /// namedMessages dictionary with name of configuration as key. /// </para> /// <para> /// If default_level is not provided in configuration, the default level of logger will be used /// as default level of named message. /// </para> /// <para> /// New in 3.0. /// </para> /// </summary> /// <param name="config">The configuration object to load settings from.</param> /// <exception cref="ConfigException"> /// If any of the configuration settings are missing or are invalid values. /// </exception> private void AddNamedMessage(IConfiguration config) { // load the text from configuration (required) string text = Helper.GetStringAttribute(config, TEXT, true); // load the default level from configuration (optional, default to the default level of logger) Level msgDefaultLevel = Helper.GetLevelAttribute(config, DEFAULT_LEVEL, defaultLevel); // load the parameters from configuration (optional) IList<string> parameters = Helper.GetStringListAttribute(config, PARAMETERS, false); // create a NamedMessage object NamedMessage msg = new NamedMessage(text, config.Name, parameters, msgDefaultLevel); // add to namedMessages dictionary namedMessages.Add(config.Name, msg); }
/// <summary> /// <para> /// Logs the named message to the underlying implementation with the specified logging level. /// </para> /// <para> /// Subclasses may override this method to perform appropriate backend specific processing, if the /// backend supports message parameters in a superior way to the normal way it is handled in the /// Log method. /// </para> /// <para> /// New in 3.0. /// </para> /// </summary> /// <param name="level">The level at which to log the message.</param> /// <param name="message">The named message to log.</param> /// <param name="param">The parameters to use in formatting the message.</param> /// <exception cref="ArgumentNullException">If any parameter is null.</exception> /// <exception cref="MessageFormattingException">If there is an error formatting the message from /// the params.</exception> /// <exception cref="LoggingException">If there is a failure in the backend logging system. /// </exception> protected internal virtual void LogNamedMessage(Level level, NamedMessage message, params object[] param) { Log(level, message.Text, param); }
protected void SetUp() { IList<string> param = new List<string>(); param.Add("param1"); param.Add("param2"); namedMsg = new NamedMessage("text1", NAME1, param, Level.ERROR); msgs = new Dictionary<string, NamedMessage>(); msgs.Add(NAME1, namedMsg); msgs.Add(NAME2, new NamedMessage("text2", NAME2, new List<string>(), Level.WARN)); config = new DefaultConfiguration("default"); config.SetSimpleAttribute("logger_name", LOGNAME); config.SetSimpleAttribute("default_level", LEVEL.ToString()); IConfiguration msgsConfig = new DefaultConfiguration("NamedMessages"); IConfiguration msgConfig = new DefaultConfiguration(NAME1); msgConfig.SetSimpleAttribute("text", "text1"); msgConfig.SetSimpleAttribute("default_level", Level.ERROR); msgConfig.SetAttribute("parameters", new object[] {"param1", "param2"}); msgsConfig.AddChild(msgConfig); msgConfig = new DefaultConfiguration(NAME2); msgConfig.SetSimpleAttribute("text", "text2"); msgConfig.SetSimpleAttribute("default_level", Level.WARN); msgsConfig.AddChild(msgConfig); config.AddChild(msgsConfig); logger = new SimpleLogger(LOGNAME, LEVEL, msgs); }
/// <summary> /// <para> /// Logs the named message to the ILog instance used by this class with the specified logging level. /// </para> /// <para> /// The params array will be used for both pattern like {0}, {1}, etc. in the text of named message, /// and the custom pattern like %property{name} in conversion pattern in log4net. /// </para> /// <para> /// If the level is not supported by this class, or the level is off, the message will not be logged. /// </para> /// <para> /// New in 3.0. /// </para> /// </summary> /// <param name="level">The level at which to log the message.</param> /// <param name="message">The named message to log.</param> /// <param name="param">The parameters to use in formatting the message.</param> /// <exception cref="ArgumentNullException">If message or params is null.</exception> /// <exception cref="MessageFormattingException">If the number of parameters in the params array /// does not match the number of parameters for the NamedMessage.</exception> /// <exception cref="LoggingException">If there is a failure in the log4net call.</exception> protected internal override void LogNamedMessage(Level level, NamedMessage message, params object[] param) { Helper.ValidateNotNull(message, "message"); Helper.ValidateNotNull(param, "param"); if (level == LoggingWrapper.Level.OFF || !IsLevelEnabled(level)) { return; } // check the number of parameters match if (message.ParameterNames.Count != param.Length) { throw new MessageFormattingException("The number of parameters doesn't match"); } // put the params in the log4Net context for (int i = 0; i < param.Length; i++) { LogicalThreadContext.Properties[message.ParameterNames[i]] = param[i]; } // format the message with params string formatted; try { formatted = string.Format(message.Text, param); } catch (FormatException e) { throw new MessageFormattingException( "Error occurs when formatting the message with params", e); } // Log the message LogMessage(level, formatted); // remove the params from log4Net context for (int i = 0; i < param.Length; i++) { LogicalThreadContext.Properties.Remove(message.ParameterNames[i]); } }
/// <summary> /// <para> /// Logs the named message to the underlying implementation with the specified logging level. /// </para> /// <para> /// This method guarantees that no exception will ever be thrown. /// </para> /// </summary> /// <param name="level">The level at which to log the message.</param> /// <param name="message">The named message to log.</param> /// <param name="param">The parameters to use in formatting the message.</param> protected internal override void LogNamedMessage(Level level, NamedMessage message, params object[] param) { try { underlyingLogger.LogNamedMessage(level, message, param); } catch (Exception e) { // try to log the exception to exceptionLogger LogException(e); } }