Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / Diagnostics / XmlWriterTraceListener.cs / 1 / 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); } } }
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ImpersonationContext.cs
- Matrix3D.cs
- XmlCDATASection.cs
- DesignerObjectListAdapter.cs
- TimersDescriptionAttribute.cs
- TypeConverterAttribute.cs
- XmlSchemaDatatype.cs
- WebEvents.cs
- X509Certificate2.cs
- NameSpaceExtractor.cs
- _IPv4Address.cs
- Graphics.cs
- SQLInt16.cs
- SingleObjectCollection.cs
- HitTestParameters.cs
- OdbcCommandBuilder.cs
- LabelTarget.cs
- DisplayInformation.cs
- BaseProcessor.cs
- AttributeExtensions.cs
- LogicalMethodInfo.cs
- SqlWebEventProvider.cs
- DataServiceQueryProvider.cs
- AttributeProviderAttribute.cs
- ToolStripManager.cs
- BinaryMessageEncodingElement.cs
- BeginStoryboard.cs
- XmlChildEnumerator.cs
- Matrix.cs
- AesManaged.cs
- JsonReader.cs
- NumberFormatter.cs
- PersonalizationStateQuery.cs
- ReflectionPermission.cs
- ScalarOps.cs
- HostedAspNetEnvironment.cs
- XsdCachingReader.cs
- WindowsScrollBarBits.cs
- keycontainerpermission.cs
- ReferencedAssembly.cs
- XmlSchemaSimpleContentExtension.cs
- dsa.cs
- ExtenderHelpers.cs
- xmlglyphRunInfo.cs
- StreamGeometryContext.cs
- SimpleType.cs
- ValidationErrorCollection.cs
- JsonReader.cs
- CollectionType.cs
- BindingMAnagerBase.cs
- HideDisabledControlAdapter.cs
- VisualStyleElement.cs
- PropertyEmitterBase.cs
- DictionaryContent.cs
- ControlCachePolicy.cs
- OneToOneMappingSerializer.cs
- ContentHostHelper.cs
- MessageDesigner.cs
- XmlSchemaSimpleTypeRestriction.cs
- TypeReference.cs
- SqlDataSourceEnumerator.cs
- ProxyElement.cs
- XMLUtil.cs
- ZipPackagePart.cs
- ProxyElement.cs
- SafeProcessHandle.cs
- SetState.cs
- OperationContext.cs
- HostProtectionException.cs
- XmlNodeReader.cs
- DataSourceXmlSerializer.cs
- EnumBuilder.cs
- WorkflowLayouts.cs
- WindowsStartMenu.cs
- CodeNamespaceImport.cs
- InputLanguage.cs
- AttributeData.cs
- FilteredReadOnlyMetadataCollection.cs
- ContentType.cs
- TableParaClient.cs
- BitmapFrame.cs
- SamlAttributeStatement.cs
- TypeToArgumentTypeConverter.cs
- WebPartMovingEventArgs.cs
- HttpCapabilitiesBase.cs
- PersonalizationAdministration.cs
- XmlNode.cs
- BuildProvider.cs
- NameObjectCollectionBase.cs
- EndpointNameMessageFilter.cs
- Aggregates.cs
- DoWorkEventArgs.cs
- LogManagementAsyncResult.cs
- UpWmlPageAdapter.cs
- TagPrefixCollection.cs
- WebConfigurationHostFileChange.cs
- PersonalizationDictionary.cs
- FontCollection.cs
- DbDataReader.cs
- XMLSchema.cs