Walkthrough: Tracking requests from Sitecore to xConnect

Abstract

How to track requests from Sitecore to xConnect.

The ISendRequestHandler interface injects logic to run before and after a request is sent to xConnect. Any client that wants to track and log requests to xConnect must implement the ISendRequestHandler interface and register it in the configuration file.

This walkthrough describes how to:

  • Implement the ISendRequestHandler interface

  • Register the handler

Implement the ISendRequestHandler interface

To implement the ISendRequestHandler interface:

  • Create a new class that extends ISendRequestHandler. For example:

    public class TestSendRequestHandler : ISendRequestHandler 
    {	 
          public async Task PreHandle(HttpRequestMessage request) 
          { 
              PreHandleCount++; 
              PreHandledMessages.Add(request); 
          } 
     
           public async Task PostHandle(HttpResponseMessage response, TimeSpan? duration) 
           { 
               PostHandleCount++; 
               PostHandledMessages.Add(response); 
           } 
    
    } 

Register the handler

To register the handler in the configuration file:

  1. Create an XML file under the App_Data\Config\Sitecore\Collection folder (you can specify any filename).

  2. Register the handler using the following code (substituting the name of the handler):

    <Settings>
      <Sitecore>
        <XConnect>
          <Collection>
            <Services>
                <ISendRequestHandler>
                    <Type>Sitecore.Xdb.UnitTests.RequestLoggingFixture.TestSendRequestHandler, Sitecore.Xdb. UnitTests</Type>
                    <As>Sitecore.Xdb.Common.Web.ISendRequestHandler, Sitecore.Xdb.Common.Web</As>
                    <LifeTime>Singleton</LifeTime>
                </ISendRequestHandler>  
            </Services>
          </Collection>
        </XConnect>
      </Sitecore>
    </Settings>