Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Framework / MS / Internal / TraceData.cs / 1305600 / TraceData.cs
#define TRACE //---------------------------------------------------------------------------- // //// Copyright (C) Microsoft Corporation. All rights reserved. // // // Description: Defines TraceData class, for providing debugging information // for Data Binding and Styling // //--------------------------------------------------------------------------- using System; using System.Collections; using System.ComponentModel; using System.Diagnostics; using System.Reflection; using System.Text; using System.Windows; using System.Windows.Data; using System.Windows.Markup; using MS.Internal.Data; using MS.Win32; namespace MS.Internal { // levels for the various extended traces internal enum TraceDataLevel { // Binding and friends CreateExpression = PresentationTraceLevel.High, // 10, ShowPath = PresentationTraceLevel.High, // 11, ResolveDefaults = PresentationTraceLevel.High, // 13, Attach = PresentationTraceLevel.Low, // 1, AttachToContext = PresentationTraceLevel.Low, // 2, SourceLookup = PresentationTraceLevel.Low, // 4, Activate = PresentationTraceLevel.Low, // 3, Transfer = PresentationTraceLevel.Medium, // 5, Update = PresentationTraceLevel.Medium, // 6, Validation = PresentationTraceLevel.High, // 12, Events = PresentationTraceLevel.Medium, // 7, GetValue = PresentationTraceLevel.High, // 12, ReplaceItem = PresentationTraceLevel.Medium, // 8, GetInfo = PresentationTraceLevel.Medium, // 9, // Data providers ProviderQuery = PresentationTraceLevel.Low, // 1, XmlProvider = PresentationTraceLevel.Medium, // 2, XmlBuildCollection = PresentationTraceLevel.High, // 3, } ////// Provides a central mechanism for providing debugging information /// to aid programmers in using data binding. /// Helpers are defined here. /// The rest of the class is generated; see also: AvTraceMessage.txt and genTraceStrings.pl /// internal static partial class TraceData { // ----------------------------------------------------------------- // Constructors // ----------------------------------------------------------------- static TraceData() { _avTrace.TraceExtraMessages += new AvTraceEventHandler(OnTrace); // This tells tracing that IsEnabled should be true if we're in the debugger, // even if the registry flag isn't turned on. By default, IsEnabled is only // true if the registry is set. _avTrace.EnabledByDebugger = true; // This tells the tracing code not to automatically generate the .GetType // and .HashCode in the trace strings. _avTrace.SuppressGeneratedParameters = true; } // ------------------------------------------------------------------ // Methods // ----------------------------------------------------------------- // determine whether an extended trace should be produced for the given // object static public bool IsExtendedTraceEnabled(object element, TraceDataLevel level) { if (TraceData.IsEnabled) { PresentationTraceLevel traceLevel = PresentationTraceSources.GetTraceLevel(element); return (traceLevel >= (PresentationTraceLevel)level); } else return false; } // 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"); } } } // ------------------------------------------------------------------ // Helper functions for message string construction // ------------------------------------------------------------------ ////// 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. /// /// description will be appended to this builder /// object to be described; /// currently recognized types: BindingExpression, Binding, DependencyObject, Exception ///a string that describes the object 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)); } } ////// Produces a string that describes a source object: /// e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement /// /// description will be appended to this builder /// a source object (e.g. element in a Binding Path, DataItem in BindingExpression, ContextElement) ///a string that describes the object 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()); } } } ////// static public string DescribeSourceObject(object o) { AvTraceBuilder atb = new AvTraceBuilder(null); DescribeSourceObject(atb, o); return atb.ToString(); } ////// Produces a string that describes TargetElement and TargetProperty /// /// description will be appended to this builder /// TargetElement /// TargetProperty ///a string that describes TargetElement and TargetProperty 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); } } ////// static public string DescribeTarget(DependencyObject targetElement, DependencyProperty targetProperty) { AvTraceBuilder atb = new AvTraceBuilder(null); DescribeTarget(atb, targetElement, targetProperty); return atb.ToString(); } static public string Identify(object o) { if (o == null) return ""; Type type = o.GetType(); if (type.IsPrimitive || type.IsEnum) return Format("'{0}'", o); string s = o as String; if (s != null) return Format("'{0}'", AvTrace.AntiFormat(s)); NamedObject n = o as NamedObject; if (n != null) return AvTrace.AntiFormat(n.ToString()); ICollection ic = o as ICollection; if (ic != null) return Format("{0} (hash={1} Count={2})", type.Name, AvTrace.GetHashCodeHelper(o), ic.Count); return Format("{0} (hash={1})", type.Name, AvTrace.GetHashCodeHelper(o)); } static public string IdentifyWeakEvent(Type type) { const string suffix = "EventManager"; string name = type.Name; if (name.EndsWith(suffix, StringComparison.Ordinal)) { name = name.Substring(0, name.Length - suffix.Length); } return name; } static public string IdentifyAccessor(object accessor) { DependencyProperty dp = accessor as DependencyProperty; if (dp != null) return Format("{0}({1})", dp.GetType().Name, dp.Name); PropertyInfo pi = accessor as PropertyInfo;; if (pi != null) return Format("{0}({1})", pi.GetType().Name, pi.Name); PropertyDescriptor pd = accessor as PropertyDescriptor;; if (pd != null) return Format("{0}({1})", pd.GetType().Name, pd.Name); return Identify(accessor); } static public string IdentifyException(Exception ex) { if (ex == null) return " "; return Format("{0} ({1})", ex.GetType().Name, AvTrace.AntiFormat(ex.Message)); } static string Format(string format, params object[] args) { return String.Format(TypeConverterHelper.InvariantEnglishUS, format, args); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XmlSchemaExternal.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- Trace.cs
- InputLangChangeEvent.cs
- PerformanceCounterLib.cs
- HotSpot.cs
- AssemblyUtil.cs
- FontStyle.cs
- WeakReferenceEnumerator.cs
- DataTemplateSelector.cs
- KeyMatchBuilder.cs
- DataContractSerializerOperationGenerator.cs
- TextSegment.cs
- MessageAction.cs
- CryptoProvider.cs
- HostingEnvironment.cs
- SynchronizedDispatch.cs
- FileCodeGroup.cs
- StatusBar.cs
- StorageMappingFragment.cs
- TitleStyle.cs
- DeobfuscatingStream.cs
- TreeWalkHelper.cs
- UpdateProgress.cs
- DataTemplate.cs
- TypeSystem.cs
- DefaultAuthorizationContext.cs
- XmlQueryOutput.cs
- SortDescriptionCollection.cs
- MethodBuilder.cs
- Transactions.cs
- StyleBamlRecordReader.cs
- SafeRightsManagementEnvironmentHandle.cs
- ChannelTracker.cs
- FrameworkElement.cs
- BinaryMessageFormatter.cs
- CustomActivityDesigner.cs
- ProtocolsConfigurationHandler.cs
- StorageRoot.cs
- MdiWindowListItemConverter.cs
- Geometry3D.cs
- AssemblyInfo.cs
- XmlAttributeOverrides.cs
- GroupDescription.cs
- Compensate.cs
- WebPartTransformerCollection.cs
- VariableQuery.cs
- ServiceModelConfigurationSectionGroup.cs
- Size.cs
- LinkDesigner.cs
- ConstantExpression.cs
- SafeLocalAllocation.cs
- HyperLinkField.cs
- DbConnectionInternal.cs
- BooleanFunctions.cs
- NavigationProperty.cs
- ViewGenerator.cs
- PlatformNotSupportedException.cs
- LineGeometry.cs
- PreApplicationStartMethodAttribute.cs
- CodeCastExpression.cs
- ClientRuntime.cs
- EntitySet.cs
- ObjectParameterCollection.cs
- MimeMapping.cs
- TextRange.cs
- PropertyManager.cs
- RootBrowserWindowProxy.cs
- SHA512.cs
- XmlMtomWriter.cs
- ProcessThreadDesigner.cs
- GenericsInstances.cs
- ToolStripSettings.cs
- SyndicationContent.cs
- Int32CAMarshaler.cs
- Int32AnimationUsingKeyFrames.cs
- PasswordBoxAutomationPeer.cs
- SpellerStatusTable.cs
- ViewGenerator.cs
- TextRangeEditTables.cs
- RequestCache.cs
- RichTextBox.cs
- DynamicPropertyReader.cs
- SchemaImporterExtensionElement.cs
- CacheSection.cs
- RegexFCD.cs
- XmlNullResolver.cs
- WebCategoryAttribute.cs
- TransformationRules.cs
- latinshape.cs
- HttpStreamFormatter.cs
- PersistenceTypeAttribute.cs
- TargetFrameworkUtil.cs
- MessageFault.cs
- Activation.cs
- Peer.cs
- System.Data_BID.cs
- InputScopeNameConverter.cs
- CollectionView.cs
- DesignerLoader.cs