Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / Diagnostics / XmlWriterTraceListener.cs / 1305376 / XmlWriterTraceListener.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.Text; using System.Xml; using System.Xml.XPath; using System.IO; using System.Globalization; using System.Collections; using System.Security.Permissions; using System.Runtime.Versioning; namespace System.Diagnostics { [HostProtection(Synchronization=true)] public class XmlWriterTraceListener : TextWriterTraceListener { private const string fixedHeader = "" + " "); } private void WriteEscaped(string str) { if (str == null) return; int lastIndex = 0; for (int i=0; i"; private readonly string machineName = Environment.MachineName; private StringBuilder strBldr = null; private XmlTextWriter xmlBlobWriter = null; public XmlWriterTraceListener(Stream stream) : base(stream){ } public XmlWriterTraceListener(Stream stream, string name) : base(stream, name){ } public XmlWriterTraceListener(TextWriter writer) : base(writer){ } public XmlWriterTraceListener(TextWriter writer, string name) : base(writer, name){ } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public XmlWriterTraceListener(string filename) : base(filename){ } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public XmlWriterTraceListener(string filename, string name) : base(filename, name){ } public override void Write(string message) { this.WriteLine(message); } public override void WriteLine(string message) { this.TraceEvent(null, SR.GetString(SR.TraceAsTraceSource), TraceEventType.Information, 0, message); } public override void Fail(string message, string detailMessage) { StringBuilder failMessage = new StringBuilder(message); if (detailMessage != null) { failMessage.Append(" "); failMessage.Append(detailMessage); } this.TraceEvent(null, SR.GetString(SR.TraceAsTraceSource), TraceEventType.Error, 0, failMessage.ToString()); } public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string format, params object[] args) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args)) return; WriteHeader(source, eventType, id, eventCache); string message; if (args != null) message = String.Format(CultureInfo.InvariantCulture, format, args); else message = format; WriteEscaped(message); WriteFooter(eventCache); } public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string message) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message)) return; WriteHeader(source, eventType, id, eventCache); WriteEscaped(message); WriteFooter(eventCache); } public override void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, object data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data)) return; WriteHeader(source, eventType, id, eventCache); InternalWrite(" "); InternalWrite(""); if (data != null) { InternalWrite(" "); WriteFooter(eventCache); } public override void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, params object[] data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data)) return; WriteHeader(source, eventType, id, eventCache); InternalWrite(""); WriteData(data); InternalWrite(" "); } InternalWrite(""); if (data != null) { for (int i=0; i "); WriteFooter(eventCache); } // Special case XPathNavigator dataitems to write out XML blob unescaped private void WriteData(object data) { XPathNavigator xmlBlob = data as XPathNavigator; if(xmlBlob == null) WriteEscaped(data.ToString()); else { if (strBldr == null) { strBldr = new StringBuilder(); xmlBlobWriter = new XmlTextWriter(new StringWriter(strBldr, CultureInfo.CurrentCulture)); } else strBldr.Length = 0; try { // Rewind the blob to point to the root, this is needed to support multiple XMLTL in one TraceData call xmlBlob.MoveToRoot(); xmlBlobWriter.WriteNode(xmlBlob, false); InternalWrite(strBldr.ToString()); } catch (Exception) { // We probably only care about XmlException for ill-formed XML though InternalWrite(data.ToString()); } } } public override void Close() { base.Close(); if (xmlBlobWriter != null) xmlBlobWriter.Close(); xmlBlobWriter = null; strBldr = null; } public override void TraceTransfer(TraceEventCache eventCache, String source, int id, string message, Guid relatedActivityId) { WriteHeader(source, TraceEventType.Transfer, id, eventCache, relatedActivityId); WriteEscaped(message); WriteFooter(eventCache); } private void WriteHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache, Guid relatedActivityId) { WriteStartHeader(source, eventType, id, eventCache); InternalWrite("\" RelatedActivityID=\""); InternalWrite(relatedActivityId.ToString("B")); WriteEndHeader(eventCache); } private void WriteHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache) { WriteStartHeader(source, eventType, id, eventCache); WriteEndHeader(eventCache); } private void WriteStartHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache) { InternalWrite(fixedHeader); InternalWrite(""); if (data[i] != null) WriteData(data[i]); InternalWrite(""); } } InternalWrite(" "); InternalWrite(((uint)id).ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); InternalWrite("3 "); InternalWrite("0 "); InternalWrite(""); int sev = (int)eventType; if (sev > 255) sev = 255; if (sev < 0) sev = 0; InternalWrite(sev.ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); InternalWrite(""); InternalWrite(""); InternalWrite(" "); InternalWrite(" "); InternalWrite(" "); InternalWrite(" "); InternalWrite(machineName); InternalWrite(" "); InternalWrite(""); } private void WriteFooter(TraceEventCache eventCache) { bool writeLogicalOps = IsEnabled(TraceOptions.LogicalOperationStack); bool writeCallstack = IsEnabled(TraceOptions.Callstack); if (eventCache != null && (writeLogicalOps || writeCallstack)) { InternalWrite(" "); if (writeLogicalOps) { InternalWrite(" "); } InternalWrite(""); Stack s = eventCache.LogicalOperationStack as Stack; if (s != null) { foreach (object correlationId in s) { InternalWrite(" "); } InternalWrite(""); WriteEscaped(correlationId.ToString()); InternalWrite(" "); } } InternalWrite(""); InternalWrite(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); if (writeCallstack) { InternalWrite(""); WriteEscaped(eventCache.Callstack); InternalWrite(" "); } InternalWrite("': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(">"); lastIndex = i +1; break; case '"': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite("""); lastIndex = i +1; break; case '\'': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite("'"); lastIndex = i +1; break; case (char)0xD: InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(" "); lastIndex = i +1; break; case (char)0xA: InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(" "); lastIndex = i +1; break; } } InternalWrite(str.Substring(lastIndex, str.Length-lastIndex)); } private void InternalWrite(string message) { if (!EnsureWriter()) return; // NeedIndent is nop writer.Write(message); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // // Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- using System; using System.Text; using System.Xml; using System.Xml.XPath; using System.IO; using System.Globalization; using System.Collections; using System.Security.Permissions; using System.Runtime.Versioning; namespace System.Diagnostics { [HostProtection(Synchronization=true)] public class XmlWriterTraceListener : TextWriterTraceListener { private const string fixedHeader = "" + " "); } private void WriteEscaped(string str) { if (str == null) return; int lastIndex = 0; for (int i=0; i"; private readonly string machineName = Environment.MachineName; private StringBuilder strBldr = null; private XmlTextWriter xmlBlobWriter = null; public XmlWriterTraceListener(Stream stream) : base(stream){ } public XmlWriterTraceListener(Stream stream, string name) : base(stream, name){ } public XmlWriterTraceListener(TextWriter writer) : base(writer){ } public XmlWriterTraceListener(TextWriter writer, string name) : base(writer, name){ } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public XmlWriterTraceListener(string filename) : base(filename){ } [ResourceExposure(ResourceScope.Machine)] [ResourceConsumption(ResourceScope.Machine)] public XmlWriterTraceListener(string filename, string name) : base(filename, name){ } public override void Write(string message) { this.WriteLine(message); } public override void WriteLine(string message) { this.TraceEvent(null, SR.GetString(SR.TraceAsTraceSource), TraceEventType.Information, 0, message); } public override void Fail(string message, string detailMessage) { StringBuilder failMessage = new StringBuilder(message); if (detailMessage != null) { failMessage.Append(" "); failMessage.Append(detailMessage); } this.TraceEvent(null, SR.GetString(SR.TraceAsTraceSource), TraceEventType.Error, 0, failMessage.ToString()); } public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string format, params object[] args) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, format, args)) return; WriteHeader(source, eventType, id, eventCache); string message; if (args != null) message = String.Format(CultureInfo.InvariantCulture, format, args); else message = format; WriteEscaped(message); WriteFooter(eventCache); } public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, int id, string message) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, message)) return; WriteHeader(source, eventType, id, eventCache); WriteEscaped(message); WriteFooter(eventCache); } public override void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, object data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data)) return; WriteHeader(source, eventType, id, eventCache); InternalWrite(" "); InternalWrite(""); if (data != null) { InternalWrite(" "); WriteFooter(eventCache); } public override void TraceData(TraceEventCache eventCache, String source, TraceEventType eventType, int id, params object[] data) { if (Filter != null && !Filter.ShouldTrace(eventCache, source, eventType, id, null, null, null, data)) return; WriteHeader(source, eventType, id, eventCache); InternalWrite(""); WriteData(data); InternalWrite(" "); } InternalWrite(""); if (data != null) { for (int i=0; i "); WriteFooter(eventCache); } // Special case XPathNavigator dataitems to write out XML blob unescaped private void WriteData(object data) { XPathNavigator xmlBlob = data as XPathNavigator; if(xmlBlob == null) WriteEscaped(data.ToString()); else { if (strBldr == null) { strBldr = new StringBuilder(); xmlBlobWriter = new XmlTextWriter(new StringWriter(strBldr, CultureInfo.CurrentCulture)); } else strBldr.Length = 0; try { // Rewind the blob to point to the root, this is needed to support multiple XMLTL in one TraceData call xmlBlob.MoveToRoot(); xmlBlobWriter.WriteNode(xmlBlob, false); InternalWrite(strBldr.ToString()); } catch (Exception) { // We probably only care about XmlException for ill-formed XML though InternalWrite(data.ToString()); } } } public override void Close() { base.Close(); if (xmlBlobWriter != null) xmlBlobWriter.Close(); xmlBlobWriter = null; strBldr = null; } public override void TraceTransfer(TraceEventCache eventCache, String source, int id, string message, Guid relatedActivityId) { WriteHeader(source, TraceEventType.Transfer, id, eventCache, relatedActivityId); WriteEscaped(message); WriteFooter(eventCache); } private void WriteHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache, Guid relatedActivityId) { WriteStartHeader(source, eventType, id, eventCache); InternalWrite("\" RelatedActivityID=\""); InternalWrite(relatedActivityId.ToString("B")); WriteEndHeader(eventCache); } private void WriteHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache) { WriteStartHeader(source, eventType, id, eventCache); WriteEndHeader(eventCache); } private void WriteStartHeader(String source, TraceEventType eventType, int id, TraceEventCache eventCache) { InternalWrite(fixedHeader); InternalWrite(""); if (data[i] != null) WriteData(data[i]); InternalWrite(""); } } InternalWrite(" "); InternalWrite(((uint)id).ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); InternalWrite("3 "); InternalWrite("0 "); InternalWrite(""); int sev = (int)eventType; if (sev > 255) sev = 255; if (sev < 0) sev = 0; InternalWrite(sev.ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); InternalWrite(""); InternalWrite(""); InternalWrite(" "); InternalWrite(" "); InternalWrite(" "); InternalWrite(" "); InternalWrite(machineName); InternalWrite(" "); InternalWrite(""); } private void WriteFooter(TraceEventCache eventCache) { bool writeLogicalOps = IsEnabled(TraceOptions.LogicalOperationStack); bool writeCallstack = IsEnabled(TraceOptions.Callstack); if (eventCache != null && (writeLogicalOps || writeCallstack)) { InternalWrite(" "); if (writeLogicalOps) { InternalWrite(" "); } InternalWrite(""); Stack s = eventCache.LogicalOperationStack as Stack; if (s != null) { foreach (object correlationId in s) { InternalWrite(" "); } InternalWrite(""); WriteEscaped(correlationId.ToString()); InternalWrite(" "); } } InternalWrite(""); InternalWrite(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture)); InternalWrite(" "); if (writeCallstack) { InternalWrite(""); WriteEscaped(eventCache.Callstack); InternalWrite(" "); } InternalWrite("': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(">"); lastIndex = i +1; break; case '"': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite("""); lastIndex = i +1; break; case '\'': InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite("'"); lastIndex = i +1; break; case (char)0xD: InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(" "); lastIndex = i +1; break; case (char)0xA: InternalWrite(str.Substring(lastIndex, i-lastIndex)); InternalWrite(" "); lastIndex = i +1; break; } } InternalWrite(str.Substring(lastIndex, str.Length-lastIndex)); } private void InternalWrite(string message) { if (!EnsureWriter()) return; // NeedIndent is nop writer.Write(message); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AssemblyNameUtility.cs
- DataControlButton.cs
- XmlAutoDetectWriter.cs
- SamlConditions.cs
- NamespaceEmitter.cs
- MultiDataTrigger.cs
- PersonalizationStateInfo.cs
- UrlMappingCollection.cs
- SecurityManager.cs
- SwitchCase.cs
- LocalizedNameDescriptionPair.cs
- ChildDocumentBlock.cs
- RSAOAEPKeyExchangeFormatter.cs
- LogicalCallContext.cs
- ImmutablePropertyDescriptorGridEntry.cs
- InternalPermissions.cs
- WeakReferenceKey.cs
- FormatException.cs
- Point3D.cs
- CodeMemberProperty.cs
- SwitchLevelAttribute.cs
- Slider.cs
- TextFormatterImp.cs
- UserControlBuildProvider.cs
- Setter.cs
- xml.cs
- ConfigXmlElement.cs
- _CommandStream.cs
- ModifiableIteratorCollection.cs
- SqlUtil.cs
- MessageQueuePermissionEntry.cs
- FormsAuthenticationConfiguration.cs
- AsymmetricKeyExchangeDeformatter.cs
- SchemaImporter.cs
- ScriptBehaviorDescriptor.cs
- ZipIOExtraField.cs
- JavaScriptObjectDeserializer.cs
- FullTextBreakpoint.cs
- PassportPrincipal.cs
- Command.cs
- AutomationProperty.cs
- Codec.cs
- ServiceModelConfigurationElementCollection.cs
- VarInfo.cs
- HttpConfigurationSystem.cs
- NameScopePropertyAttribute.cs
- MailAddressCollection.cs
- NativeMethods.cs
- HierarchicalDataSourceIDConverter.cs
- PropertyChangedEventManager.cs
- FixUpCollection.cs
- HtmlControl.cs
- CacheOutputQuery.cs
- Region.cs
- SourceCollection.cs
- EnlistmentTraceIdentifier.cs
- FormViewUpdatedEventArgs.cs
- ConvertTextFrag.cs
- InternalConfigSettingsFactory.cs
- AttributedMetaModel.cs
- VScrollProperties.cs
- Enlistment.cs
- FileResponseElement.cs
- FamilyMap.cs
- OutputCacheEntry.cs
- IndexExpression.cs
- XComponentModel.cs
- ipaddressinformationcollection.cs
- Parameter.cs
- BaseAutoFormat.cs
- IconHelper.cs
- Context.cs
- ThreadPoolTaskScheduler.cs
- ISCIIEncoding.cs
- ApplicationSecurityManager.cs
- mediaclock.cs
- LocalizabilityAttribute.cs
- TypeSystemProvider.cs
- SpecularMaterial.cs
- ToolboxItemFilterAttribute.cs
- PropertyFilter.cs
- TerminatorSinks.cs
- DrawingCollection.cs
- ContentType.cs
- PtsPage.cs
- SupportsPreviewControlAttribute.cs
- TableLayoutStyleCollection.cs
- StylusOverProperty.cs
- WsdlBuildProvider.cs
- basenumberconverter.cs
- XmlSchemaExporter.cs
- RTLAwareMessageBox.cs
- ComponentResourceManager.cs
- ConfigurationFileMap.cs
- DynamicPropertyReader.cs
- FontFamilyIdentifier.cs
- PaintValueEventArgs.cs
- Camera.cs
- XPathAxisIterator.cs
- GPPOINTF.cs