Create a static instance of each class where you want to use tracing. It does basically encapsulate the typename and enables fast trace filters.
 /// <summary>
 ///     Executes the callback when the given trace level is enabled. That allows when tracing is enabled
 ///     complex string formatting only when needed.
 /// </summary>
 /// <param name="msgType">The message type which must match.</param>
 /// <param name="level">The trace level which must match.</param>
 /// <param name="type">TypeHandle of your class.</param>
 /// <param name="action">The delegate to execute when it does match.</param>
 /// <returns>true when the delegate was executed, false otherwise.</returns>
 public static bool Execute(MessageTypes msgType, Level level, TypeHashes type, Action action)
 {
     if (TracerConfig.Instance.IsEnabled(type, msgType, level))
     {
         action();
         return(true);
     }
     return(false);
 }
        /// <summary>
        ///     Write an exception to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="ex">The excepton to trace.</param>
        public static void Error(Level level, TypeHashes type, string method, Exception ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, "{0}", ex);
            }
        }
        public virtual bool IsMatch(TypeHashes type, MessageTypes msgTypeFilter, Level level)
        {
            var lret = ((level & this.myLevelFilter) != Level.None);

            if (lret)
            {
                var areSameSize = (this.myFilterHashes.Length == type.myTypeHashes.Length);

                for (var i = 0; i < this.myFilterHashes.Length; i++)
                {
                    if (this.myFilterHashes[i] == MATCHANY)
                    {
                        break;
                    }

                    if (i < type.myTypeHashes.Length)
                    {
                        // The current filter does not match exit
                        // otherwise we compare the next round.
                        if (this.myFilterHashes[i] != type.myTypeHashes[i])
                        {
                            lret = false;
                            break;
                        }

                        // We are still here when the last arry item matches
                        // This is a full match
                        if (i == this.myFilterHashes.Length - 1 && areSameSize)
                        {
                            break;
                        }
                    }
                    else // the filter string is longer than the domain. That can never match
                    {
                        lret = false;
                        break;
                    }
                }
            }

            if (lret)
            {
                lret = (msgTypeFilter & this.myMsgTypeFilter) != MessageTypes.None;
            }

            // If no match try next filter
            if (this.Next != null && lret == false)
            {
                lret = this.Next.IsMatch(type, msgTypeFilter, level);
            }

            return(lret);
        }
Exemplo n.º 4
0
        internal bool IsEnabled(TypeHashes type, MessageTypes msgType, Level level)
        {
            if (this.myListeners == null || type == null)
            {
                return(false);
            }

            var lret = this.myFilters.IsMatch(type, msgType, level);

            if (this.myNotFilters != null && lret)
            {
                lret = this.myNotFilters.IsMatch(type, msgType, level);
            }

            return(lret);
        }
        /// <summary>
        ///     Write an error trace to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="fmt">Trace message format string</param>
        /// <param name="args">Optional message format arguments.</param>
        public static void Error(Level level, TypeHashes type, string method, string fmt, params object[] args)
        {
            if (fmt == null)
            {
                throw new ArgumentNullException(fmt);
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, fmt, args);
            }
        }
        /// <summary>
        ///     Write an exception to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="ex">The exception to trace.</param>
        /// <param name="fmt">Message describing what the exception is about.</param>
        /// <param name="args">Optional format arguments for the description message string.</param>
        public static void Error(Level level, TypeHashes type, string method, Exception ex, string fmt, params object[] args)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }
            if (fmt == null)
            {
                throw new ArgumentNullException("fmt");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                var traceMsg = FormatStringSafe(fmt, args);
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, "{0}{1}{2}", traceMsg, Environment.NewLine, ex);
            }
        }
        /// <summary>
        ///     Create a new Tracer which traces method enter and leave (on Dispose)
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        public Tracer(Level level, TypeHashes type, string method)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            this.myEnterTime      = DateTime.MinValue;
            this.myMethod         = method;
            this.myType           = type;
            this.myLevel          = level;
            this.myTypeMethodName = null;

            if (TracerConfig.Instance.IsEnabled(this.myType, MessageTypes.InOut, this.myLevel))
            {
                this.myEnterTime = DateTime.Now;
                TraceMsg(MsgTypeIn, this.TypeMethodName, this.myEnterTime, null, null);
            }
        }
 /// <summary>
 ///     Create a new Tracer which traces method enter and leave (on Dispose)
 /// </summary>
 /// <param name="type">
 ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
 ///     your type.
 /// </param>
 /// <param name="method">The method name of your current method.</param>
 public Tracer(TypeHashes type, string method) : this(Level.L1, type, method)
 {
 }
        /// <summary>
        ///     Write an exception to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="ex">The exception to trace.</param>
        /// <param name="fmt">Message describing what the exception is about.</param>
        /// <param name="args">Optional format arguments for the description message string.</param>
        public static void Error(Level level, TypeHashes type, string method, Exception ex, string fmt, params object[] args)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }
            if (fmt == null)
            {
                throw new ArgumentNullException("fmt");
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                var traceMsg = FormatStringSafe(fmt, args);
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, "{0}{1}{2}", traceMsg, Environment.NewLine, ex);
            }
        }
        /// <summary>
        ///     Write an exception to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="ex">The excepton to trace.</param>
        public static void Error(Level level, TypeHashes type, string method, Exception ex)
        {
            if (ex == null)
            {
                throw new ArgumentNullException("ex");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, "{0}", ex);
            }
        }
        /// <summary>
        ///     Write an error trace to the configured output device.
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        /// <param name="fmt">Trace message format string</param>
        /// <param name="args">Optional message format arguments.</param>
        public static void Error(Level level, TypeHashes type, string method, string fmt, params object[] args)
        {
            if (fmt == null)
            {
                throw new ArgumentNullException(fmt);
            }
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (TracerConfig.Instance.IsEnabled(type, MessageTypes.Error, level))
            {
                TraceMsg(MsgTypeError, GenerateTypeMethodName(type, method), DateTime.Now, fmt, args);
            }
        }
 /// <summary>
 ///     Executes the callback when the given trace level is enabled. That allows when tracing is enabled
 ///     complex string formatting only when needed.
 /// </summary>
 /// <param name="msgType">The message type which must match.</param>
 /// <param name="level">The trace level which must match.</param>
 /// <param name="type">TypeHandle of your class.</param>
 /// <param name="action">The delegate to execute when it does match.</param>
 /// <returns>true when the delegate was executed, false otherwise.</returns>
 public static bool Execute(MessageTypes msgType, Level level, TypeHashes type, Action action)
 {
     if (TracerConfig.Instance.IsEnabled(type, msgType, level))
     {
         action();
         return true;
     }
     return false;
 }
 private static string GenerateTypeMethodName(TypeHashes type, string method)
 {
     return type.FullQualifiedTypeName + "." + method;
 }
 /// <summary>
 ///     Create a new Tracer which traces method enter and leave (on Dispose)
 /// </summary>
 /// <param name="type">
 ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
 ///     your type.
 /// </param>
 /// <param name="method">The method name of your current method.</param>
 public Tracer(TypeHashes type, string method) : this(Level.L1, type, method)
 {
 }
        /// <summary>
        ///     Create a new Tracer which traces method enter and leave (on Dispose)
        /// </summary>
        /// <param name="level">Trace Level. 1 is the high level overview, 5 is for high volume detailed traces.</param>
        /// <param name="type">
        ///     TypeHandle instance which identifies your class type. This instance should be a static instance of
        ///     your type.
        /// </param>
        /// <param name="method">The method name of your current method.</param>
        public Tracer(Level level, TypeHashes type, string method)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (method == null)
            {
                throw new ArgumentNullException("method");
            }

            this.myEnterTime = DateTime.MinValue;
            this.myMethod = method;
            this.myType = type;
            this.myLevel = level;
            this.myTypeMethodName = null;

            if (TracerConfig.Instance.IsEnabled(this.myType, MessageTypes.InOut, this.myLevel))
            {
                this.myEnterTime = DateTime.Now;
                TraceMsg(MsgTypeIn, this.TypeMethodName, this.myEnterTime, null, null);
            }
        }
        internal bool IsEnabled(TypeHashes type, MessageTypes msgType, Level level)
        {
            if (this.myListeners == null || type == null)
            {
                return false;
            }

            var lret = this.myFilters.IsMatch(type, msgType, level);
            if (this.myNotFilters != null && lret)
            {
                lret = this.myNotFilters.IsMatch(type, msgType, level);
            }

            return lret;
        }
 private static string GenerateTypeMethodName(TypeHashes type, string method)
 {
     return(type.FullQualifiedTypeName + "." + method);
 }