XmlWriterTraceListener.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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 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(""); 
            if (data != null) {
                InternalWrite(""); 
                WriteData(data);
                InternalWrite("");
            }
            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("");
            if (data != null) { 
                for (int i=0; i");
                    if (data[i] != null) 
                        WriteData(data[i]);
                    InternalWrite("");
                }
            } 
            InternalWrite("");
 
            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("");
            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("");
 
            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(""); 

                    Stack s = eventCache.LogicalOperationStack as Stack;

                    if (s != null) { 
                        foreach (object correlationId in s) {
                            InternalWrite(""); 
                            WriteEscaped(correlationId.ToString()); 
                            InternalWrite("");
                        } 
                    }
                    InternalWrite("");
                }
 
                InternalWrite("");
                InternalWrite(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture)); 
                InternalWrite(""); 

                if (writeCallstack) { 
                    InternalWrite("");
                    WriteEscaped(eventCache.Callstack);
                    InternalWrite("");
                } 

                InternalWrite(""); 
            } 

            InternalWrite(""); 
        }

        private void WriteEscaped(string str) {
            if (str == null) 
                return;
 
            int lastIndex = 0; 
            for (int i=0; i':
                        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 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(""); 
            if (data != null) {
                InternalWrite(""); 
                WriteData(data);
                InternalWrite("");
            }
            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("");
            if (data != null) { 
                for (int i=0; i");
                    if (data[i] != null) 
                        WriteData(data[i]);
                    InternalWrite("");
                }
            } 
            InternalWrite("");
 
            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("");
            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("");
 
            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(""); 

                    Stack s = eventCache.LogicalOperationStack as Stack;

                    if (s != null) { 
                        foreach (object correlationId in s) {
                            InternalWrite(""); 
                            WriteEscaped(correlationId.ToString()); 
                            InternalWrite("");
                        } 
                    }
                    InternalWrite("");
                }
 
                InternalWrite("");
                InternalWrite(eventCache.Timestamp.ToString(CultureInfo.InvariantCulture)); 
                InternalWrite(""); 

                if (writeCallstack) { 
                    InternalWrite("");
                    WriteEscaped(eventCache.Callstack);
                    InternalWrite("");
                } 

                InternalWrite(""); 
            } 

            InternalWrite(""); 
        }

        private void WriteEscaped(string str) {
            if (str == null) 
                return;
 
            int lastIndex = 0; 
            for (int i=0; i':
                        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

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK