MsmqDiagnostics.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / MsmqDiagnostics.cs / 1 / MsmqDiagnostics.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
namespace System.ServiceModel.Channels
{ 
    using System.Collections.Generic;
    using System.Diagnostics; 
    using System.Globalization; 
    using System.ServiceModel.Diagnostics;
    using System.Transactions; 
    using System.Xml;
    using System.Text;

 
    static class MsmqDiagnostics
    { 
 
        public static void CannotPeekOnQueue(string formatName, Exception ex)
        { 
            if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent(
                    TraceEventType.Warning, 
                    TraceCode.MsmqCannotPeekOnQueue,
                    new StringTraceRecord("QueueFormatName", formatName), 
                    null, 
                    ex);
            } 
        }

        public static void CannotReadQueues(string host, bool publicQueues, Exception ex)
        { 
            if (DiagnosticUtility.ShouldTraceWarning)
            { 
                Dictionary dictionary = new Dictionary(2); 
                dictionary["Host"] = host;
                dictionary["PublicQueues"] = Convert.ToString(publicQueues, CultureInfo.InvariantCulture); 
                TraceUtility.TraceEvent(
                    TraceEventType.Warning,
                    TraceCode.MsmqCannotReadQueues,
                    new DictionaryTraceRecord(dictionary), 
                    null,
                    ex); 
            } 
        }
 
        public static ServiceModelActivity StartListenAtActivity(MsmqReceiveHelper receiver)
        {
            ServiceModelActivity activity = receiver.Activity;
            if (DiagnosticUtility.ShouldUseActivity && null == activity) 
            {
                activity = ServiceModelActivity.CreateActivity(true); 
                DiagnosticUtility.DiagnosticTrace.TraceTransfer(activity.Id); 
                ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityListenAt, receiver.ListenUri.ToString()), ActivityType.ListenAt);
            } 
            return activity;
        }

        public static Activity BoundOpenOperation(MsmqReceiveHelper receiver) 
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            { 
                DiagnosticUtility.DiagnosticTrace.TraceEvent(
                    TraceEventType.Information, 
                    TraceCode.TransportListen,
                    SR.GetString(SR.TraceCodeTransportListen, receiver.ListenUri.ToString()),
                    null,
                    null, 
                    receiver);
            } 
            return ServiceModelActivity.BoundOperation(receiver.Activity); 
        }
 
        public static Activity BoundReceiveOperation(MsmqReceiveHelper receiver)
        {
            if (DiagnosticUtility.ShouldUseActivity && null != ServiceModelActivity.Current && ActivityType.ProcessAction != ServiceModelActivity.Current.ActivityType)
            { 
                return ServiceModelActivity.BoundOperation(receiver.Activity);
            } 
            else 
            {
                return null; 
            }
        }

 
        public static ServiceModelActivity BoundDecodeOperation()
        { 
            ServiceModelActivity activity = null; 
            if (DiagnosticUtility.ShouldUseActivity)
            { 
                activity = ServiceModelActivity.CreateBoundedActivity(true);
                ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityProcessingMessage, TraceUtility.RetrieveMessageNumber()), ActivityType.ProcessMessage);
            }
            return activity; 
        }
 
        public static ServiceModelActivity BoundReceiveBytesOperation() 
        {
            ServiceModelActivity activity = null; 
            if (DiagnosticUtility.ShouldUseActivity)
            {
                activity = ServiceModelActivity.CreateBoundedActivityWithTransferInOnly(Guid.NewGuid());
                ServiceModelActivity.Start(activity, SR.GetString(SR.ActivityReceiveBytes, TraceUtility.RetrieveMessageNumber()), ActivityType.ReceiveBytes); 
            }
            return activity; 
        } 

        public static void TransferFromTransport(Message message) 
        {
            if (DiagnosticUtility.ShouldUseActivity)
            {
                TraceUtility.TransferFromTransport(message); 
            }
        } 
 
        public static void ExpectedException(Exception ex)
        { 
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                DiagnosticUtility.ExceptionUtility.TraceHandledException(ex, TraceEventType.Information);
            } 
        }
 
        public static void ScanStarted() 
        {
            if (DiagnosticUtility.ShouldTraceVerbose) 
            {
                TraceUtility.TraceEvent(
                    TraceEventType.Verbose,
                    TraceCode.MsmqScanStarted, 
                    null,
                    null, 
                    null); 
            }
        } 

        public static void MatchedApplicationFound(string host, string queueName, bool isPrivate, string canonicalPath)
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                Dictionary dictionary = new Dictionary(4); 
                dictionary["Host"] = host; 
                dictionary["QueueName"] = queueName;
                dictionary["Private"] = Convert.ToString(isPrivate, CultureInfo.InvariantCulture); 
                dictionary["CanonicalPath"] = canonicalPath;
                TraceUtility.TraceEvent(
                    TraceEventType.Information,
                    TraceCode.MsmqMatchedApplicationFound, 
                    new DictionaryTraceRecord(dictionary),
                    null, 
                    null); 
            }
        } 

        public static void StartingApplication(string application)
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                TraceUtility.TraceEvent( 
                    TraceEventType.Information, 
                    TraceCode.MsmqStartingApplication,
                    new StringTraceRecord("Application", application), 
                    null,
                    null);
            }
        } 

 
        public static void StartingService(string host, string name, bool isPrivate, string processedVirtualPath) 
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                Dictionary dictionary = new Dictionary(4);
                dictionary["Host"] = host;
                dictionary["Name"] = name; 
                dictionary["Private"] = Convert.ToString(isPrivate, CultureInfo.InvariantCulture);
                dictionary["VirtualPath"] = processedVirtualPath; 
                TraceUtility.TraceEvent( 
                    TraceEventType.Information,
                    TraceCode.MsmqStartingService, 
                    new DictionaryTraceRecord(dictionary),
                    null,
                    null);
            } 
        }
 
        public static void FoundBaseAddress(Uri uri, string virtualPath) 
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                Dictionary dictionary = new Dictionary(2);
                dictionary["Uri"] = uri.ToString();
                dictionary["VirtualPath"] = virtualPath; 
                TraceUtility.TraceEvent(
                    TraceEventType.Information, 
                    TraceCode.MsmqFoundBaseAddress, 
                    new DictionaryTraceRecord(dictionary),
                    null, 
                    null);
            }
        }
 
        static void DatagramSentOrReceived(NativeMsmqMessage.BufferProperty messageId, Message message, TraceCode code)
        { 
            if (DiagnosticUtility.ShouldTraceVerbose) 
            {
                Guid msmqId = MessageIdToGuid(messageId); 
                UniqueId indigoId = message.Headers.MessageId;
                TraceRecord record = null;
                if (null == indigoId)
                    record = new StringTraceRecord("MSMQMessageId", msmqId.ToString()); 
                else
                { 
                    Dictionary dictionary = new Dictionary(2); 
                    dictionary["MSMQMessageId"] = msmqId.ToString();
                    dictionary["WCFMessageId"] = indigoId.ToString(); 
                    record = new DictionaryTraceRecord(dictionary);
                }
                TraceUtility.TraceEvent(TraceEventType.Verbose, code, record, null, null);
            } 
        }
 
        public static void DatagramReceived(NativeMsmqMessage.BufferProperty messageId, Message message) 
        {
            DatagramSentOrReceived(messageId, message, TraceCode.MsmqDatagramReceived); 
        }

        public static void DatagramSent(NativeMsmqMessage.BufferProperty messageId, Message message)
        { 
            DatagramSentOrReceived(messageId, message, TraceCode.MsmqDatagramSent);
        } 
 
        static Guid MessageIdToGuid(NativeMsmqMessage.BufferProperty messageId)
        { 
            if (UnsafeNativeMethods.PROPID_M_MSGID_SIZE != messageId.Buffer.Length)
                DiagnosticUtility.DebugAssert(String.Format("Unexpected messageId size: {0}", messageId.Buffer.Length));

            byte[] buffer = new byte[16]; 
            Buffer.BlockCopy(messageId.Buffer, 4, buffer, 0, 16);
            return new Guid(buffer); 
        } 

        public static void MessageConsumed(string uri, string messageId, bool rejected) 
        {
            if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent( 
                    TraceEventType.Warning,
                    rejected ? TraceCode.MsmqMessageRejected : TraceCode.MsmqMessageDropped, 
                    new StringTraceRecord("MSMQMessageId", messageId), 
                    null,
                    null); 
            }

            if (PerformanceCounters.PerformanceCountersEnabled)
            { 
                if (rejected)
                { 
                    PerformanceCounters.MsmqRejectedMessage(uri); 
                }
                else 
                {
                    PerformanceCounters.MsmqDroppedMessage(uri);
                }
            } 
        }
 
        public static void MessageLockedUnderTheTransaction(long lookupId) 
        {
            if (DiagnosticUtility.ShouldTraceWarning) 
            {
                TraceUtility.TraceEvent(
                    TraceEventType.Warning,
                    TraceCode.MsmqMessageLockedUnderTheTransaction, 
                    new StringTraceRecord("MSMQMessageLookupId", Convert.ToString(lookupId, CultureInfo.InvariantCulture)),
                    null, 
                    null); 
            }
        } 

        public static void MoveOrDeleteAttemptFailed(long lookupId)
        {
            if (DiagnosticUtility.ShouldTraceWarning) 
            {
                TraceUtility.TraceEvent( 
                    TraceEventType.Warning, 
                    TraceCode.MsmqMoveOrDeleteAttemptFailed,
                    new StringTraceRecord("MSMQMessageLookupId", Convert.ToString(lookupId, CultureInfo.InvariantCulture)), 
                    null,
                    null);
            }
        } 

        public static void MsmqDetected(Version version) 
        { 
            if (DiagnosticUtility.ShouldTraceInformation)
            { 
                TraceUtility.TraceEvent(
                    TraceEventType.Information,
                    TraceCode.MsmqDetected,
                    new StringTraceRecord("MSMQVersion", version.ToString()), 
                    null,
                    null); 
            } 
        }
 
        public static void PoisonMessageMoved(string messageId, bool poisonQueue, string uri)
        {
            if (DiagnosticUtility.ShouldTraceWarning)
            { 
                TraceUtility.TraceEvent(
                    TraceEventType.Warning, 
                    poisonQueue ? TraceCode.MsmqPoisonMessageMovedPoison : TraceCode.MsmqPoisonMessageMovedRetry, 
                    new StringTraceRecord("MSMQMessageId", messageId),
                    null, 
                    null);
            }
            if (poisonQueue && PerformanceCounters.PerformanceCountersEnabled)
            { 
                PerformanceCounters.MsmqPoisonMessage(uri);
            } 
        } 

        public static void PoisonMessageRejected(string messageId, string uri) 
        {
            if (DiagnosticUtility.ShouldTraceWarning)
            {
                TraceUtility.TraceEvent( 
                    TraceEventType.Warning,
                    TraceCode.MsmqPoisonMessageRejected, 
                    new StringTraceRecord("MSMQMessageId", messageId), 
                    null,
                    null); 
            }
            if (PerformanceCounters.PerformanceCountersEnabled)
            {
                PerformanceCounters.MsmqPoisonMessage(uri); 
            }
        } 
 
        static bool poolFullReported = false;
 
        public static void PoolFull(int poolSize)
        {
            if (DiagnosticUtility.ShouldTraceInformation && ! poolFullReported)
            { 
                TraceUtility.TraceEvent(
                    TraceEventType.Information, 
                    TraceCode.MsmqPoolFull, 
                    null,
                    null, 
                    null);

                poolFullReported = true;
            } 
        }
 
        public static void PotentiallyPoisonMessageDetected(string messageId) 
        {
            if (DiagnosticUtility.ShouldTraceWarning) 
            {
                TraceUtility.TraceEvent(
                    TraceEventType.Warning,
                    TraceCode.MsmqPotentiallyPoisonMessageDetected, 
                    new StringTraceRecord("MSMQMessageId", messageId),
                    null, 
                    null); 
            }
        } 

        public static void QueueClosed(string formatName)
        {
            if (DiagnosticUtility.ShouldTraceInformation) 
            {
                TraceUtility.TraceEvent( 
                    TraceEventType.Information, 
                    TraceCode.MsmqQueueClosed,
                    new StringTraceRecord("FormatName", formatName), 
                    null,
                    null);
            }
        } 

        public static void QueueOpened(string formatName) 
        { 
            if (DiagnosticUtility.ShouldTraceInformation)
            { 
                TraceUtility.TraceEvent(
                    TraceEventType.Information,
                    TraceCode.MsmqQueueOpened,
                    new StringTraceRecord("FormatName", formatName), 
                    null,
                    null); 
            } 
        }
 
        public static void QueueTransactionalStatusUnknown(string formatName)
        {
            if (DiagnosticUtility.ShouldTraceWarning)
            { 
                TraceUtility.TraceEvent(
                    TraceEventType.Warning, 
                    TraceCode.MsmqQueueTransactionalStatusUnknown, 
                    new StringTraceRecord("FormatName", formatName),
                    null, 
                    null);
            }
        }
 
        public static void SessiongramSent(string sessionId, NativeMsmqMessage.BufferProperty messageId, int numberOfMessages)
        { 
            if (DiagnosticUtility.ShouldTraceVerbose) 
            {
                Dictionary dictionary = new Dictionary(3); 
                dictionary["SessionId"] = sessionId;
                dictionary["MSMQMessageId"] = MsmqMessageId.ToString(messageId.Buffer);
                dictionary["NumberOfMessages"] = Convert.ToString(numberOfMessages, CultureInfo.InvariantCulture);
                TraceUtility.TraceEvent( 
                    TraceEventType.Verbose,
                    TraceCode.MsmqSessiongramSent, 
                    new DictionaryTraceRecord(dictionary), 
                    null,
                    null); 
            }
        }

        public static void SessiongramReceived(string sessionId, NativeMsmqMessage.BufferProperty messageId, int numberOfMessages) 
        {
            if (DiagnosticUtility.ShouldTraceVerbose) 
            { 
                Dictionary dictionary = new Dictionary(3);
                dictionary["SessionId"] = sessionId; 
                dictionary["MSMQMessageId"] = MsmqMessageId.ToString(messageId.Buffer);
                dictionary["NumberOfMessages"] = Convert.ToString(numberOfMessages, CultureInfo.InvariantCulture);
                TraceUtility.TraceEvent(
                    TraceEventType.Verbose, 
                    TraceCode.MsmqSessiongramReceived,
                    new DictionaryTraceRecord(dictionary), 
                    null, 
                    null);
            } 
        }

        public static void UnexpectedAcknowledgment(string messageId, int acknowledgment)
        { 
            if (DiagnosticUtility.ShouldTraceVerbose)
            { 
                Dictionary dictionary = new Dictionary(2); 
                dictionary["MSMQMessageId"] = messageId;
                dictionary["Acknowledgment"] = Convert.ToString(acknowledgment, CultureInfo.InvariantCulture); 
                TraceUtility.TraceEvent(
                    TraceEventType.Verbose,
                    TraceCode.MsmqUnexpectedAcknowledgment,
                    new DictionaryTraceRecord(dictionary), 
                    null,
                    null); 
            } 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

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