[EWS]How To enable EWS request and response tracing in PowerShell script using EWS Managed API?

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.

    1: $Assem = ( 
    2:     "Microsoft.Exchange.WebServices, Version=, Culture=neutral,  PublicKeyToken =31bf3856ad364e35"
    3:    ) 
    5: $Source = @"
    6: using System;
    7: using System.Text;
    8: using Microsoft.Exchange.WebServices.Data;
    9: public class TraceListener : Microsoft.Exchange.WebServices.Data.ITraceListener
   10:     {
   11:         public void Trace(string traceType, string traceMessage)
   12:         {
   13:             CreateXMLTextFile(traceType, traceMessage.ToString());
   14:         }
   15:         private void CreateXMLTextFile(string fileName, string traceContent)
   16:         {           
   17:            string strPath = "C:\\Log.txt";
   18:             System.IO.FileStream fs;
   19:             if (System.IO.File.Exists(strPath) == false)
   20:             {
   21:                 fs = System.IO.File.Create(strPath);
   22:             }
   23:             else
   24:             {
   25:                 fs = System.IO.File.OpenWrite(strPath);
   26:             }
   27:             fs.Close();
   28:             // Create an instance of StreamWriter to write text to a file.
   29:             System.IO.StreamWriter sw = System.IO.File.AppendText(strPath);
   30:             sw.WriteLine(System.DateTime.Now.ToString() + ": " + traceContent);
   31:             sw.Close();
   32:         }
   33:     }
   34: "@
   36: Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp  

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.

    1: $service = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2)
    2: $service.TraceListener = new-object TraceListener
    3: #Set it to $false, if you want to disable this tracing
    4: $service.TraceEnabled = $true 

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.

Happy scripting!

EWS tracing PowerShell.txt