Debugging a Web service–based application can be difficult because part of the processing is performed on a computer to which you do not have access. Because you cannot step through the code on the server, it can be helpful to see the XML requests and responses that are exchanged between the client and the server to determine which part of the application is causing an error. When you are using the Microsoft Exchange Web Services (EWS) Managed API, you can use the tracing methods on the ExchangeService object to capture the XML request that is sent to Exchange Web Services and the response that the server returns to the application.
To enable tracing on the ExchangeService object:
Dim _exchangeService As New ExchangeService
Dim TC As New EWSTrace.TraceListener
_exchangeService.TraceFlags = TraceFlags.All
_exchangeService.TraceEnabled = True
_exchangeService.TraceListener = TC
The following code example shows simple object that implements the ITraceListener interface and stores the traced requests and responses in XML or text files.
'NOTE: Following programming examples is for illustration only, without warranty either expressed or implied,
'including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose.
'This sample code assumes that you are familiar with the programming language being demonstrated and
'the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment.
Public Sub Trace(ByVal traceType As String, ByVal traceMessage As String) Implements ITraceListener.Trace
CreateXMLTextFile(traceType + " --- " + traceMessage.ToString())
Private Sub CreateXMLTextFile(ByVal traceContent As String)
'Get the path of the application to create log files at
Dim strPath As String = System.AppDomain.CurrentDomain.BaseDirectory
strPath = strPath + "\\EWSLog.txt"
Dim FS As System.IO.FileStream
If System.IO.File.Exists(strPath) = False Then
FS = System.IO.File.Create(strPath)
FS = System.IO.File.OpenWrite(strPath)
' Create an instance of StreamWriter to write text to a file.
Dim sw As System.IO.StreamWriter = System.IO.File.AppendText(strPath)
sw.WriteLine(System.DateTime.Now.ToString() + " : " + traceContent)
FS = Nothing
sw = Nothing
For reference and C# sample code snippet refer:
- EWS Managed API Application Debugging
- Tracing EWS Requests