共用方式為


Visual Basic Code Example: Requesting Tracing

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a Sub procedure that requests report messages based on a given tracing level value. This procedure sets the MSMQMessage.Trace property of the message using the tracing level supplied by the caller and then sends the message to a known destination queue.

For information on how Message Queuing traces messages, see Tracing Messages.

To request tracing

  1. Declare the objects needed to send the message that requests tracing. This procedure declares the following objects:

  2. Generate a direct format name using the computer name and queue name provided by the caller.

  3. Set the MSMQDestination.FormatName property.

  4. Set the MSMQMessage.Trace property using the tracing level provided but the caller.

  5. Optional. Set additional message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message: Tracing".

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name. Note that when using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access and then closes the destination queue when the object is released.

Code Example

The following code example requires MSMQ 3.0.

Sub RequestTracing( _  
                   strComputerName As String, _  
                   strQueueName As String, _  
                   lTracingLevel As Long _  
                   )  
  Dim strFormatName As String  
  Dim dest As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create a direct format name.  
  strFormatName = "DIRECT=OS:" & strComputerName & "\" & strQueueName  
  
  ' Set format name of MSMQDestination object  
  On Error GoTo ErrorHandler  
  dest.FormatName = strFormatName  
  
  ' Set MSMQMessage.Trace to request tracing.  
  msg.Trace = lTracingLevel  
  
  ' Set other message properties.  
  msg.Label = "Test Message: Tracing"  
  
  ' Send message  
  msg.Send DestinationQueue:=dest  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub