Code:
/ 4.0 / 4.0 / untmp / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- InteropExecutor.cs
- LocalizableAttribute.cs
- PagedDataSource.cs
- EntityModelBuildProvider.cs
- WebPart.cs
- InteropBitmapSource.cs
- AttributeAction.cs
- HttpInputStream.cs
- Brush.cs
- AdRotatorDesigner.cs
- AmbientValueAttribute.cs
- TextEditorCopyPaste.cs
- RuleSetReference.cs
- ButtonBaseAdapter.cs
- ExceptionWrapper.cs
- NullRuntimeConfig.cs
- CharacterBuffer.cs
- OuterProxyWrapper.cs
- TemplateControlBuildProvider.cs
- ExpressionLink.cs
- DesignerForm.cs
- GacUtil.cs
- ClientOptions.cs
- AtomEntry.cs
- InternalPolicyElement.cs
- ExpressionBuilder.cs
- AdRotator.cs
- ColumnResult.cs
- BitmapEffectInput.cs
- NegatedCellConstant.cs
- SqlConnectionString.cs
- HttpModulesSection.cs
- TextEditorTyping.cs
- GridViewEditEventArgs.cs
- PersonalizationDictionary.cs
- SqlDataReaderSmi.cs
- PkcsMisc.cs
- CfgArc.cs
- OciHandle.cs
- Fx.cs
- GenericIdentity.cs
- TextServicesContext.cs
- DataContext.cs
- DocumentViewerConstants.cs
- LockRecursionException.cs
- IntSecurity.cs
- ThreadExceptionEvent.cs
- connectionpool.cs
- WpfGeneratedKnownProperties.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- SplineQuaternionKeyFrame.cs
- CompilerInfo.cs
- ManipulationDelta.cs
- NameValuePair.cs
- RawStylusSystemGestureInputReport.cs
- RtType.cs
- MDIWindowDialog.cs
- StorageInfo.cs
- DataGridState.cs
- NTAccount.cs
- WhitespaceSignificantCollectionAttribute.cs
- SecurityElement.cs
- MenuRendererClassic.cs
- SessionPageStatePersister.cs
- TransactionProtocolConverter.cs
- ApplicationFileCodeDomTreeGenerator.cs
- CreatingCookieEventArgs.cs
- PartBasedPackageProperties.cs
- AutomationPatternInfo.cs
- JavaScriptString.cs
- BorderGapMaskConverter.cs
- WmfPlaceableFileHeader.cs
- MethodAccessException.cs
- MonthCalendarDesigner.cs
- PeerValidationBehavior.cs
- LoginStatusDesigner.cs
- MonitoringDescriptionAttribute.cs
- ObjectStateManager.cs
- DeleteHelper.cs
- LoadRetryAsyncResult.cs
- CalendarDateRange.cs
- PeerNameRecord.cs
- TagPrefixCollection.cs
- X509Chain.cs
- ReadContentAsBinaryHelper.cs
- WhitespaceRule.cs
- ProxyWebPartConnectionCollection.cs
- DataGridAddNewRow.cs
- ContentFileHelper.cs
- Evaluator.cs
- StylusPlugInCollection.cs
- GridViewItemAutomationPeer.cs
- RepeatBehavior.cs
- MethodBody.cs
- SqlServer2KCompatibilityAnnotation.cs
- ChildrenQuery.cs
- SchemaImporterExtensionElementCollection.cs
- SizeChangedInfo.cs
- PointValueSerializer.cs
- Substitution.cs