Source

flioops-net / FLiOOPS.Logging / Core.cs

Full commit
using ComLib.Configuration;
using ComLib.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace FLiOOPS.Logging
{
    /// <summary>
    /// Flags used to decide what to do when overwriting something.
    /// </summary>
    enum OverwriteMode { 
        /// <summary>
        /// Clear all values. Existing values in container 
        /// object <b>have to</b> be removed.
        /// </summary>
        CLEAR, 
        /// <summary>
        /// Do nothing if target already exists.
        /// </summary>
        IGNORE, 
        /// <summary>
        /// Overwrite target if it already exists.
        /// </summary>
        OVERWRITE };

    /// <summary>
    /// Interface implemented by classes that setup a whole 
    /// logging environment containing multiple log sources,
    /// by following a well-known strategy (e.g. by reading 
    /// rules found in a configuration file)
    /// </summary>
    public interface ILogSetup
    {
        /// <summary>
        /// Setup the logging environment by following 
        /// the default strategy. Previous configuration 
        /// will be lost after calling this method.
        /// </summary>
        /// <exception cref="System.Configuration.ConfigurationException">
        /// A configuration error may be raised (e.g. configuration 
        /// data has invalid characters or wrong format).
        /// </exception>
        void DefaultSetup();
        /// <summary>
        /// Setup the logging environment by following 
        /// the default strategy, but customize the procedure 
        /// according to the options supplied in.
        /// </summary>
        /// <param name="options">
        /// A dictionary containing custom options to consider
        /// at setup time. 
        /// </param>
        /// <exception cref="System.Configuration.ConfigurationException">
        /// A configuration error may be raised (e.g. configuration 
        /// data has invalid characters or wrong format).
        /// </exception>
        /// <remarks>
        /// So far the following key|value pairs supported in 
        /// <paramref name="options"/> have special meaning :
        /// 
        /// <list type="bullet">
        /// <item>
        ///     <term>overwrite</term>
        ///     <description>
        ///     Overwrite mode (<see cref="FLiOOPS.Logging.OverwriteMode"/>).
        ///     Defaults to <value>FLiOOPS.Logging.OverwriteMode.CLEAR</value>.
        ///     </description>
        /// </item>
        /// </list>
        /// 
        /// Options specific to particular instances 
        /// implementing this interface <b>should</b> 
        /// start with an underscore (e.g. <b>_</b>) in order 
        /// to avoid conflicts.
        /// </remarks>
        /// <see cref="System.Collections.Generic.IDictionary"/>
        void CustomSetup(IDictionary<object, object> options);
        /// <summary>
        /// Load custom logging rules from an external configuration 
        /// source (e.g. configuration file).
        /// </summary>
        /// <param name="config">
        /// Configuration source used to read logging setup rules.
        /// </param>
        void Load(IConfigSection config);
    }

    public class EnhancedLogEvent : LogEvent
    {
        public new LogLevel Level 
        {
            get { return base.Level; }
            set { base.Level = value; }
        }
        public new object Message 
        {
            get { return base.Message; }
            set { base.Message = value; }
        }
        public new string FinalMessage 
        {
            get { return base.FinalMessage; }
            set { base.FinalMessage = value; }
        }
        public new Exception Error 
        {
            get { return base.Error; }
            set { base.Error = value; }
        }
        public new object[] Args 
        {
            get { return base.Args; }
            set { base.Args = value; }
        }
        public new string Computer 
        {
            get { return base.Computer; }
            set { base.Computer = value; }
        }
        public new DateTime CreateTime
        {
            get { return base.CreateTime; }
            set { base.CreateTime = value; }
        }
        public new string ThreadName 
        {
            get { return base.ThreadName; }
            set { base.ThreadName = value; }
        }
        public new string UserName 
        {
            get { return base.UserName; }
            set { base.UserName = value; }
        }
        public new Exception Ex 
        {
            get { return base.Ex; }
            set { base.Ex = value; }
        }
        public new Type LogType 
        {
            get { return base.LogType; }
            set { base.LogType = value; }
        }

        public EnhancedLogEvent(LogEvent logEvent) 
            : base()
        {
            Type thisType = this.GetType();
            foreach (FieldInfo fi in typeof(LogEvent).GetFields())
            {
                PropertyInfo pi = thisType.GetProperty(fi.Name);
                pi.SetValue(this, fi.GetValue(logEvent), null);
            }
        }
    }
}