Beispiel #1
0
        // ------------------------------------------------------------------
        // Helper functions for message string construction
        // ------------------------------------------------------------------

        /// <summary>
        /// Construct a string that describes data and debugging information about the object.
        /// A title is appended in front if provided.
        /// If object o is not a recognized object, it will be ToString()'ed.
        /// </summary>
        /// <param name="traceBuilder">description will be appended to this builder</param>
        /// <param name="o">object to be described;
        /// currently recognized types: BindingExpression, Binding, DependencyObject, Exception</param>
        /// <returns>a string that describes the object</returns>
        static public void Describe(AvTraceBuilder traceBuilder, object o)
        {
            if (o == null)
            {
                traceBuilder.Append("null");
            }

            else if (o is BindingExpression)
            {
                BindingExpression bindingExpr = o as BindingExpression;

                Describe(traceBuilder, bindingExpr.ParentBinding);
                traceBuilder.Append("; DataItem=");
                DescribeSourceObject(traceBuilder, bindingExpr.DataItem);
                traceBuilder.Append("; ");
                DescribeTarget(traceBuilder, bindingExpr.TargetElement, bindingExpr.TargetProperty);
            }

            else if (o is Binding)
            {
                Binding binding = o as Binding;
                if (binding.Path != null)
                {
                    traceBuilder.AppendFormat("Path={0}", binding.Path.Path);
                }
                else if (binding.XPath != null)
                {
                    traceBuilder.AppendFormat("XPath={0}", binding.XPath);
                }
                else
                {
                    traceBuilder.Append("(no path)");
                }
            }

            else if (o is BindingExpressionBase)
            {
                BindingExpressionBase beb = o as BindingExpressionBase;
                DescribeTarget(traceBuilder, beb.TargetElement, beb.TargetProperty);
            }

            else if (o is DependencyObject)
            {
                DescribeSourceObject(traceBuilder, o);
            }

            else
            {
                traceBuilder.AppendFormat("'{0}'", AvTrace.ToStringHelper(o));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Produces a string that describes TargetElement and TargetProperty
        /// </summary>
        /// <param name="traceBuilder">description will be appended to this builder</param>
        /// <param name="targetElement">TargetElement</param>
        /// <param name="targetProperty">TargetProperty</param>
        /// <returns>a string that describes TargetElement and TargetProperty</returns>
        static public void DescribeTarget(AvTraceBuilder traceBuilder, DependencyObject targetElement, DependencyProperty targetProperty)
        {
            if (targetElement != null)
            {
                traceBuilder.Append("target element is ");
                DescribeSourceObject(traceBuilder, targetElement);
                if (targetProperty != null)
                {
                    traceBuilder.Append("; ");
                }
            }

            if (targetProperty != null)
            {
                traceBuilder.AppendFormat("target property is '{0}' (type '{1}')", targetProperty.Name, targetProperty.PropertyType.Name);
            }
        }
Beispiel #3
0
 // report/describe any additional parameters passed to TraceData.Trace()
 static public void OnTrace(AvTraceBuilder traceBuilder, object[] parameters, int start)
 {
     for (int i = start; i < parameters.Length; i++)
     {
         object o = parameters[i];
         string s = o as string;
         traceBuilder.Append(" ");
         if (s != null)
         {
             traceBuilder.Append(s);
         }
         else if (o != null)
         {
             traceBuilder.Append(o.GetType().Name);
             traceBuilder.Append(":");
             Describe(traceBuilder, o);
         }
         else
         {
             traceBuilder.Append("null");
         }
     }
 }
Beispiel #4
0
 /// <summary>
 /// Produces a string that describes a source object:
 /// e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement
 /// </summary>
 /// <param name="traceBuilder">description will be appended to this builder</param>
 /// <param name="o">a source object (e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement)</param>
 /// <returns>a string that describes the object</returns>
 static public void DescribeSourceObject(AvTraceBuilder traceBuilder, object o)
 {
     if (o == null)
     {
         traceBuilder.Append("null");
     }
     else
     {
         FrameworkElement fe = o as FrameworkElement;
         if (fe != null)
         {
             traceBuilder.AppendFormat("'{0}' (Name='{1}')", fe.GetType().Name, fe.Name);
         }
         else
         {
             traceBuilder.AppendFormat("'{0}' (HashCode={1})", o.GetType().Name, o.GetHashCode());
         }
     }
 }
 // report/describe any additional parameters passed to TraceData.Trace()
 static public void OnTrace( AvTraceBuilder traceBuilder, object[] parameters, int start )
 {
     for( int i = start; i < parameters.Length; i++ )
     {
         object o = parameters[i];
         string s = o as string;
         traceBuilder.Append(" ");
         if (s != null)
         {
             traceBuilder.Append(s);
         }
         else if (o != null)
         {
             traceBuilder.Append(o.GetType().Name);
             traceBuilder.Append(":");
             Describe(traceBuilder, o);
         }
         else
         {
             traceBuilder.Append("null");
         }
     }
 }
        /// <summary>
        /// Produces a string that describes TargetElement and TargetProperty
        /// </summary>
        /// <param name="traceBuilder">description will be appended to this builder</param>
        /// <param name="targetElement">TargetElement</param>
        /// <param name="targetProperty">TargetProperty</param>
        /// <returns>a string that describes TargetElement and TargetProperty</returns>
        static public void DescribeTarget(AvTraceBuilder traceBuilder, DependencyObject targetElement, DependencyProperty targetProperty)
        {
            if (targetElement != null)
            {
                traceBuilder.Append("target element is ");
                DescribeSourceObject(traceBuilder, targetElement);
                if (targetProperty != null)
                {
                    traceBuilder.Append("; ");
                }
            }

            if (targetProperty != null)
            {
                traceBuilder.AppendFormat("target property is '{0}' (type '{1}')", targetProperty.Name, targetProperty.PropertyType.Name);
            }
        }
 /// <summary>
 /// Produces a string that describes a source object:
 /// e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement
 /// </summary>
 /// <param name="traceBuilder">description will be appended to this builder</param>
 /// <param name="o">a source object (e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement)</param>
 /// <returns>a string that describes the object</returns>
 static public void DescribeSourceObject(AvTraceBuilder traceBuilder, object o)
 {
     if (o == null)
     {
         traceBuilder.Append("null");
     }
     else
     {
         FrameworkElement fe = o as FrameworkElement;
         if (fe != null)
         {
             traceBuilder.AppendFormat("'{0}' (Name='{1}')", fe.GetType().Name, fe.Name);
         }
         else
         {
             traceBuilder.AppendFormat("'{0}' (HashCode={1})", o.GetType().Name, o.GetHashCode());
         }
     }
 }
        // ------------------------------------------------------------------
        // Helper functions for message string construction
        // ------------------------------------------------------------------

        /// <summary>
        /// Construct a string that describes data and debugging information about the object.
        /// A title is appended in front if provided.
        /// If object o is not a recognized object, it will be ToString()'ed.
        /// </summary>
        /// <param name="traceBuilder">description will be appended to this builder</param>
        /// <param name="o">object to be described;
        /// currently recognized types: BindingExpression, Binding, DependencyObject, Exception</param>
        /// <returns>a string that describes the object</returns>
        static public void Describe(AvTraceBuilder traceBuilder, object o)
        {
            if (o == null)
            {
                traceBuilder.Append("null");
            }

            else if (o is BindingExpression)
            {
                BindingExpression bindingExpr = o as BindingExpression;

                Describe(traceBuilder, bindingExpr.ParentBinding);
                traceBuilder.Append("; DataItem=");
                DescribeSourceObject(traceBuilder, bindingExpr.DataItem);
                traceBuilder.Append("; ");
                DescribeTarget(traceBuilder, bindingExpr.TargetElement, bindingExpr.TargetProperty);
            }

            else if (o is Binding)
            {
                Binding binding = o as Binding;
                if (binding.Path != null)
                    traceBuilder.AppendFormat("Path={0}", binding.Path.Path );
                else if (binding.XPath != null)
                    traceBuilder.AppendFormat("XPath={0}", binding.XPath );
                else
                    traceBuilder.Append("(no path)");
            }

            else if (o is BindingExpressionBase)
            {
                BindingExpressionBase beb = o as BindingExpressionBase;
                DescribeTarget(traceBuilder, beb.TargetElement, beb.TargetProperty);
            }

            else if (o is DependencyObject)
            {
               DescribeSourceObject(traceBuilder, o);
            }

            else
            {
                traceBuilder.AppendFormat("'{0}'", AvTrace.ToStringHelper(o));
            }
        }
Beispiel #9
0
        //
        //  Trace an event
        //
        //  note: labels start at index 1, parameters start at index 0
        //

        public void Trace(TraceEventType type, int eventId, string message, string[] labels, object[] parameters)
        {
            // Don't bother building the string if this trace is going to be ignored.

            if (_traceSource == null ||
                !_traceSource.Switch.ShouldTrace(type))
            {
                return;
            }


            // Compose the trace string.

            AvTraceBuilder traceBuilder = new AvTraceBuilder(AntiFormat(message)); // Holds the format string
            ArrayList      arrayList    = new ArrayList();                         // Holds the combined labels & parameters arrays.

            int formatIndex = 0;

            if (parameters != null && labels != null && labels.Length > 0)
            {
                int i = 1, j = 0;
                for ( ; i < labels.Length && j < parameters.Length; i++, j++)
                {
                    // Append to the format string a "; {0} = '{1}'", where the index increments (e.g. the second iteration will
                    // produce {2} & {3}).

                    traceBuilder.Append("; {" + (formatIndex++).ToString() + "}='{" + (formatIndex++).ToString() + "}'");

                    // If this parameter is null, convert to "<null>"; otherwise, when a string.format is ultimately called
                    // it produces bad results.

                    if (parameters[j] == null)
                    {
                        parameters[j] = "<null>";
                    }

                    // Otherwise, if this is an interesting object, add the hash code and type to
                    // the format string explicitely.

                    else if (!SuppressGeneratedParameters &&
                             parameters[j].GetType() != typeof(string) &&
                             !(parameters[j] is ValueType) &&
                             !(parameters[j] is Type) &&
                             !(parameters[j] is DependencyProperty))
                    {
                        traceBuilder.Append("; " + labels[i].ToString() + ".HashCode='"
                                            + GetHashCodeHelper(parameters[j]).ToString() + "'");

                        traceBuilder.Append("; " + labels[i].ToString() + ".Type='"
                                            + GetTypeHelper(parameters[j]).ToString() + "'");
                    }


                    // Add the label & parameter to the combined list.
                    // (As an optimization, the generated classes could pre-allocate a thread-safe static array, to avoid
                    // this allocation and the ToArray allocation below.)

                    arrayList.Add(labels[i]);
                    arrayList.Add(parameters[j]);
                }

                // It's OK if we terminate because we have more lables than parameters;
                // this is used by traces to have out-values in the Stop message.

                if (TraceExtraMessages != null && j < parameters.Length)
                {
                    TraceExtraMessages(traceBuilder, parameters, j);
                }
            }

            // Send the trace

            _traceSource.TraceEvent(
                type,
                eventId,
                traceBuilder.ToString(),
                arrayList.ToArray());

            // When in the debugger, always flush the output, to guarantee that the
            // traces and other info (e.g. exceptions) get interleaved correctly.

            if (IsDebuggerAttached())
            {
                _traceSource.Flush();
            }
        }
Beispiel #10
0
        //
        //  Trace an event
        //
        //  note: labels start at index 1, parameters start at index 0
        //

        public void Trace( TraceEventType type, int eventId, string message, string[] labels, object[] parameters )
        {
            // Don't bother building the string if this trace is going to be ignored.

            if( _traceSource == null
                || !_traceSource.Switch.ShouldTrace( type ))
            {
                return;
            }


            // Compose the trace string.

            AvTraceBuilder traceBuilder = new AvTraceBuilder(AntiFormat(message)); // Holds the format string
            ArrayList arrayList = new ArrayList(); // Holds the combined labels & parameters arrays.

            int formatIndex = 0;

            if (parameters != null && labels != null && labels.Length > 0)
            {
                int i = 1, j = 0;
                for( ; i < labels.Length && j < parameters.Length; i++, j++ )
                {
                    // Append to the format string a "; {0} = '{1}'", where the index increments (e.g. the second iteration will
                    // produce {2} & {3}).

                    traceBuilder.Append("; {" + (formatIndex++).ToString() + "}='{" + (formatIndex++).ToString() + "}'" );

                    // If this parameter is null, convert to "<null>"; otherwise, when a string.format is ultimately called
                    // it produces bad results.

                    if( parameters[j] == null )
                    {
                        parameters[j] = "<null>";
                    }

                    // Otherwise, if this is an interesting object, add the hash code and type to
                    // the format string explicitely.

                    else if( !SuppressGeneratedParameters
                             && parameters[j].GetType() != typeof(string)
                             && !(parameters[j] is ValueType)
                             && !(parameters[j] is Type)
                             && !(parameters[j] is DependencyProperty) )
                    {
                        traceBuilder.Append("; " + labels[i].ToString() + ".HashCode='"
                                                    + GetHashCodeHelper(parameters[j]).ToString() + "'" );

                        traceBuilder.Append("; " + labels[i].ToString() + ".Type='"
                                                    + GetTypeHelper(parameters[j]).ToString() + "'" );
                    }


                    // Add the label & parameter to the combined list.
                    // (As an optimization, the generated classes could pre-allocate a thread-safe static array, to avoid
                    // this allocation and the ToArray allocation below.)

                    arrayList.Add( labels[i] );
                    arrayList.Add( parameters[j] );


                }

                // It's OK if we terminate because we have more lables than parameters;
                // this is used by traces to have out-values in the Stop message.

                if( TraceExtraMessages != null && j < parameters.Length)
                {
                    TraceExtraMessages( traceBuilder, parameters, j );
                }

            }

            // Send the trace

            _traceSource.TraceEvent(
                type,
                eventId,
                traceBuilder.ToString(),
                arrayList.ToArray() );

            // When in the debugger, always flush the output, to guarantee that the
            // traces and other info (e.g. exceptions) get interleaved correctly.

            if( IsDebuggerAttached() )
            {
                _traceSource.Flush();
            }

        }