Most of the time we need to review the request and response logs when troubleshooting any application/script which is utilizing the Exchange Web Services. Although setting the TraceEnabled flag to true on the ExchangeService object outputs the trace to the PowerShell window, there is no easy way to copy the trace to a file. Below is an example of using the inbuilt tracing capabilities of the EWS Managed API to enable the logging of request and response in a text file. Here the assembly version and PublicKeyToken correspond to the Microsoft.Exchange.WebServices.dll obtained after installing EWS Managed API 2.2 from http://www.microsoft.com/en-in/download/details.aspx?id=42951. For a different Managed API version, you need to update the version and PublicKeyToken accordingly.
To-do: In the line number 17, change the log file path to your liking and also provide the file name. I’ve used C: directory as the log file path and Log as the name of the file. There could be permission issues when creating the log file in a directory. So make sure you’ve the permission to create a new file and write to it in the log file directory or run the PowerShell window as an administrator.
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.
After you add this to the script, you need to assign the TraceListener member of the ExchangeService class to an instance of the above TraceListener class.
In this example we’re loading the assembly into the current PowerShell session. So, if you make any changes to the TraceListener class and run the script again in the same PowerShell session, you may well see an error which indicates that the Type is already loaded [Add-Type : Cannot add type. The type name 'TraceListener' already exists.]. In that case, please restart the Windows PowerShell and run the script.
Attached at the bottom of this post is the file (EWS tracing PowerShell.txt) containing the above code snippet.