/// <summary>
		/// Create a new evaluator using the specified <see cref="Level"/> threshold.
		/// </summary>
		/// <param name="threshold">the threshold to trigger at</param>
		/// <remarks>
		/// <para>Create a new evaluator using the specified <see cref="Level"/> threshold.</para>
		/// 
		/// <para>This evaluator will trigger if the level of the event
		/// passed to <see cref="IsTriggeringEvent(LoggingEvent)"/>
		/// is equal to or greater than the <see cref="Threshold"/>
		/// level.</para>
		/// </remarks>
		public LevelEvaluator(Level threshold)
		{
			if (threshold == null)
			{
				throw new ArgumentNullException("threshold");
			}

			m_threshold = threshold;
		}
		/// <summary>
		/// Removes the first occurrence of a specific <see cref="Level"/> from the <c>LevelCollection</c>.
		/// </summary>
		/// <param name="item">The <see cref="Level"/> to remove from the <c>LevelCollection</c>.</param>
		/// <exception cref="ArgumentException">
		/// The specified <see cref="Level"/> was not found in the <c>LevelCollection</c>.
		/// </exception>
		public virtual void Remove(Level item)
		{		   
			int i = IndexOf(item);
			if (i < 0)
				throw new System.ArgumentException("Cannot remove the specified item because it was not found in the specified Collection.");
			
			++m_version;
			RemoveAt(i);
		}
		/// <summary>
		/// Add a Level it to the map
		/// </summary>
		/// <param name="level">the Level to add</param>
		public void Add(Level level)
		{
			if (level == null)
			{
				throw new ArgumentNullException("level");
			}
			lock(this)
			{
				m_mapName2Level[level.Name] = level;
				m_mapValue2Level[level.Value] = level;
			}
		}
		/// <summary>
		/// This is the most generic printing method. This generic form is intended to be used by wrappers
		/// </summary>
		/// <param name="level">The level of the message to be logged.</param>
		/// <param name="message">The message object to log.</param>
		/// <param name="t">The exception to log, including its stack trace.</param>
		/// <remarks>
		/// Generate a logging event for the specified <paramref name="level"/> using
		/// the <paramref name="message"/>.
		/// </remarks>
		virtual public void Log(Level level, object message, Exception t) 
		{
			if (IsEnabledFor(level))
			{
				ForcedLog(ThisClassFullName, level, message, t);
			}
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="LoggingEvent" /> class
		/// from the supplied parameters.
		/// </summary>
		/// <param name="fullNameOfLoggerClass">Fully qualified classname of the logger.</param>
		/// <param name="repository">The repository this event is logged in.</param>
		/// <param name="loggerName">The name of the logger of this event.</param>
		/// <param name="level">The level of this event.</param>
		/// <param name="message">The message of this event.</param>
		/// <param name="exception">The exception for this event.</param>
		/// <remarks>
		/// <para>
		/// Except <see cref="TimeStamp"/>, <see cref="Level"/> and <see cref="LoggerName"/>, 
		/// all fields of <c>LoggingEvent</c> are filled when actually needed. Call
		/// <see cref="FixVolatileData()"/> to cache all data locally
		/// to prevent inconsistencies.
		/// </para>
		/// <para>This method is called by the log4net framework
		/// to create a logging event.
		/// </para>
		/// </remarks>
		public LoggingEvent(string fullNameOfLoggerClass, log4net.Repository.ILoggerRepository repository, string loggerName, Level level, object message, Exception exception) 
		{
			m_fqnOfLoggerClass = fullNameOfLoggerClass;
			m_message = message;
			m_repository = repository;
			m_thrownException = exception;

			m_data.LoggerName = loggerName;
			m_data.Level = level;
			m_data.TimeStamp = DateTime.Now;
		}
 protected void WriteMessage(object message, Exception ex, Level level)
 {
     this.WriteMessage(message, ex, level, true);
 }
		/// <summary>
		/// This generic form is intended to be used by wrappers.
		/// </summary>
		/// <param name="callerFullName">The wrapper class' fully qualified class name.</param>
		/// <param name="level">The level of the message to be logged.</param>
		/// <param name="message">The message object to log.</param>
		/// <param name="t">The exception to log, including its stack trace.</param>
		/// <remarks>
		/// Generate a logging event for the specified <paramref name="level"/> using
		/// the <paramref name="message"/> and <paramref name="t"/>.
		/// </remarks>
		virtual public void Log(string callerFullName, Level level, object message, Exception t) 
		{
			if (IsEnabledFor(level))
			{
				ForcedLog((callerFullName != null) ? callerFullName : ThisClassFullName , level, message, t);
			}
		}
			public override int IndexOf(Level x)
			{
				lock(this.m_root)
					return m_collection.IndexOf(x);
			}
			public override void Insert(int pos, Level x)
			{
				lock(this.m_root)
					m_collection.Insert(pos,x);
			}
			public override int Add(Level x)
			{
				lock(this.m_root)
					return m_collection.Add(x);
			}
			public override bool Contains(Level x)
			{
				lock(this.m_root)
					return m_collection.Contains(x);
			}
			public override void CopyTo(Level[] array, int start)
			{
				lock(this.m_root)
					m_collection.CopyTo(array,start);
			}
		/// <summary>
		/// Adds the elements of a <see cref="Level"/> array to the current <c>LevelCollection</c>.
		/// </summary>
		/// <param name="x">The <see cref="Level"/> array whose elements should be added to the end of the <c>LevelCollection</c>.</param>
		/// <returns>The new <see cref="LevelCollection.Count"/> of the <c>LevelCollection</c>.</returns>
		public virtual int AddRange(Level[] x)
		{
			if (m_count + x.Length >= m_array.Length)
				EnsureCapacity(m_count + x.Length);

			Array.Copy(x, 0, m_array, m_count, x.Length);
			m_count += x.Length;
			m_version++;

			return m_count;
		}
		/// <summary>
		/// Removes the element at the specified index of the <c>LevelCollection</c>.
		/// </summary>
		/// <param name="index">The zero-based index of the element to remove.</param>
		/// <exception cref="ArgumentOutOfRangeException">
		/// <para><paramref name="index"/> is less than zero</para>
		/// <para>-or-</para>
		/// <para><paramref name="index"/> is equal to or greater than <see cref="LevelCollection.Count"/>.</para>
		/// </exception>
		public virtual void RemoveAt(int index)
		{
			ValidateIndex(index); // throws
			
			m_count--;

			if (index < m_count)
			{
				Array.Copy(m_array, index + 1, m_array, index, m_count - index);
			}
			
			// We can't set the deleted entry equal to null, because it might be a value type.
			// Instead, we'll create an empty single-element array of the right type and copy it 
			// over the entry we want to erase.
			Level[] temp = new Level[1];
			Array.Copy(temp, 0, m_array, m_count, 1);
			m_version++;
		}
			public override int AddRange(Level[] x)
			{
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}
			public override void Remove(Level x)
			{
				lock(this.m_root)
					m_collection.Remove(x);
			}
		/// <summary>
		/// Initializes a new instance of the <see cref="RootLogger" /> class with
		/// the specified logging level.
		/// </summary>
		/// <param name="level">The level to assign to the root logger.</param>
		/// <remarks>
		/// The root logger names itself as "root". However, the root
		/// logger cannot be retrieved by name.
		/// </remarks>
		public RootLogger(Level level) : base("root")
		{
			this.Level = level;
		}
			public override int AddRange(Level[] x)
			{
				lock(this.m_root)
					return m_collection.AddRange(x);
			}
        protected void WriteMessage(object message, Exception ex, Level level, bool isCommandText)
        {
            var messageInfo = new MessageInfo()
            {
                Level = level.Name,
                Message = message.ToString(),
                Date = DateTime.Now
            };

            if (ex != null)
            {
                messageInfo.Exception = new MessageInfoException()
                {
                    ErrorMessage = ex.Message,
                    Source = ex.Source,
                    StackTrace = ex.StackTrace
                };
            }

            if (isCommandText && ((this.commandsProcessedCount < this.countOfCommands) || (this.countOfCommands <= 0)))
            {
                double a = (++this.commandsProcessedCount * 100.0) / ((this.countOfCommands == 0) ? ((double)1) : ((double)this.countOfCommands));

                messageInfo.Progress = new MessageInfoProgress()
                {
                    Percentage = Math.Round(a),
                    Processed = commandsProcessedCount,
                    TotalToProcess = countOfCommands
                };

                log.Info($"Progress: ({ messageInfo.Progress.Processed}/{ messageInfo.Progress.TotalToProcess} - { messageInfo.Progress.Percentage}%)");
            }
            else
            {
                if (level == Level.FATAL)
                    log.Fatal(message, ex);
                else if (level == Level.ERROR)
                    log.Error(message, ex);
                else if (level == Level.DEBUG)
                    log.Debug(message);
                else
                    log.Info(message);
            }

            var memoryStream = MessageToXmlString(messageInfo);

            HttpContext.Current.Response.Write(Utf8ByteArrayToString(memoryStream.ToArray()));
            HttpContext.Current.Response.Flush();
        }
			public override void CopyTo(Level[] array)
			{
				m_collection.CopyTo(array);
			}
		/// <summary>
		/// Checks if this logger is enabled for a given <see cref="Level"/> passed as parameter.
		/// </summary>
		/// <param name="level">The level to check.</param>
		/// <returns>
		/// <c>true</c> if this logger is enabled for <c>level</c>, otherwise <c>false</c>.
		/// </returns>
		virtual public bool IsEnabledFor(Level level) 
		{
			if (m_hierarchy.IsDisabled(level))
			{
				return false;
			}
			return level >= this.EffectiveLevel;
		}
			public override void CopyTo(Level[] array, int start)
			{
				m_collection.CopyTo(array,start);
			}
		/// <summary>
		/// Creates a new logging event and logs the event without further checks.
		/// </summary>
		/// <param name="callerFullName">The wrapper class' fully qualified class name.</param>
		/// <param name="level">The level of the message to be logged.</param>
		/// <param name="message">The message object to log.</param>
		/// <param name="t">The exception to log, including its stack trace.</param>
		/// <remarks>
		/// Generates a logging event and delivers it to the attached
		/// appenders.
		/// </remarks>
		virtual protected void ForcedLog(string callerFullName, Level level, object message, Exception t) 
		{
			CallAppenders(new LoggingEvent(callerFullName, this.Hierarchy, this.Name, level, message, t));
		}
			public override bool Contains(Level x)
			{
				return m_collection.Contains(x);
			}
Example #25
0
		/// <summary>
		/// Compares two specified <see cref="Level"/> values.
		/// </summary>
		/// <param name="l">A <see cref="Level"/></param>
		/// <param name="r">A <see cref="Level"/></param>
		/// <returns>A signed number indicating the relative values of <c>l</c> and <c>r</c>.</returns>
		/// <remarks>
		/// Less than zero: <c>l</c> is less than <c>r</c>. 
		/// Zero: <c>l</c> and <c>r</c> are equal. 
		/// Greater than zero: <c>l</c> is greater than <c>r</c>. 
		/// </remarks>
		public static int Compare(Level l, Level r)
		{
			if (l == null && r == null)
			{
				return 0;
			}
			if (l == null)
			{
				return -1;
			}
			if (r == null)
			{
				return 1;
			}

			return l.m_level - r.m_level;
		}
			public override int IndexOf(Level x)
			{
				return m_collection.IndexOf(x);
			}
		/// <summary>
		/// Get the equivalent <see cref="EventLogEntryType"/> for a <see cref="Level"/> <paramref name="p"/>
		/// </summary>
		/// <param name="level">the Level to convert to an EventLogEntryType</param>
		/// <returns>The equivalent <see cref="EventLogEntryType"/> for a <see cref="Level"/> <paramref name="p"/></returns>
		/// <remarks>
		/// Because there are fewer applicable <see cref="EventLogEntryType"/>
		/// values to use in logging levels than there are in the 
		/// <see cref="Level"/> this is a one way mapping. There is
		/// a loss of information during the conversion.
		/// </remarks>
		virtual protected System.Diagnostics.EventLogEntryType GetEntryType(Level level)
		{
			if (level >= Level.ERROR) 
			{
				return System.Diagnostics.EventLogEntryType.Error;
			}
			else if (level == Level.WARN) 
			{
				return System.Diagnostics.EventLogEntryType.Warning;
			} 
			// Default setting
			return System.Diagnostics.EventLogEntryType.Information;
		}
			public override void Remove(Level x)
			{           
				throw new NotSupportedException("This is a Read Only Collection and can not be modified");
			}
        /// <summary>
        /// Gets the closest level supported by Common.Logging of the given log4net level
        /// </summary>
        /// <param name="currentLevel">
        /// The current Level.
        /// </param>
        /// <returns>
        /// The <see cref="Level"/>.
        /// </returns>
        protected static Level GetClosestLevel(Level currentLevel)
        {
            if (currentLevel.Equals(Level.OFF))
            {
                return Level.OFF;
            }

            if (currentLevel.Equals(Level.ALL))
            {
                return Level.ALL;
            }

            if (currentLevel >= Level.FATAL)
            {
                return Level.FATAL;
            }

            if (currentLevel >= Level.ERROR)
            {
                return Level.ERROR;
            }

            if (currentLevel >= Level.WARN)
            {
                return Level.WARN;
            }

            if (currentLevel >= Level.INFO)
            {
                return Level.INFO;
            }

            if (currentLevel >= Level.DEBUG)
            {
                return Level.DEBUG;
            }

            if (currentLevel >= Level.TRACE)
            {
                return Level.TRACE;
            }

            return Level.ALL;
        }
		/// <summary>
		/// Inserts an element into the <c>LevelCollection</c> at the specified index.
		/// </summary>
		/// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
		/// <param name="item">The <see cref="Level"/> to insert.</param>
		/// <exception cref="ArgumentOutOfRangeException">
		/// <para><paramref name="index"/> is less than zero</para>
		/// <para>-or-</para>
		/// <para><paramref name="index"/> is equal to or greater than <see cref="LevelCollection.Count"/>.</para>
		/// </exception>
		public virtual void Insert(int index, Level item)
		{
			ValidateIndex(index, true); // throws
			
			if (m_count == m_array.Length)
				EnsureCapacity(m_count + 1);

			if (index < m_count)
			{
				Array.Copy(m_array, index, m_array, index + 1, m_count - index);
			}

			m_array[index] = item;
			m_count++;
			m_version++;
		}