ADAL v3 diagnostic logging

If you read the documentation on http://graph.microsoft.io regarding authentication, you eventually get directed to the ADAL documentation for version 2.19.208020213 for .NET Desktop. There is a link for other versions, which points to the reference page for the library: https://docs.microsoft.com/en-us/active-directory/adal/microsoft.identitymodel.clients.activedirectory

However, at the time of this post, the reference pages do not have samples or remarks. You can browse the GitHub repo, but the README document discusses v2. So, here is how to get the information in v3. :)

First, you need a class that implements IAdalLogCallback:

public class MyAdalLoggerCallback : IAdalLogCallback  
{
  public void Log(LogLevel level, string message)
  {
    // platform-specific code goes here
  }
}

Next, to tell ADAL that you want to use your logging callback, set the Callback property of the static LoggerCallbackHandler object:

    LoggerCallbackHandler.Callback = new MyAdalLoggerCallback();

Now, calls to ADAL functions will provide logging info by calling into your callback object.

In a WebJob (which is really a console app), I used Console.WriteLine(message) to write out the entries for a call to get a token:

1/23/2017 5:46:51 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: Looking up cache for a token...  
1/23/2017 5:46:51 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: An item matching the requested resource was found in the cache  
1/23/2017 5:46:51 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: An expired or near expiry token was found in the cache  
1/23/2017 5:46:51 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: A matching item (access token or refresh token or both) was found in the cache  
1/23/2017 5:46:51 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - AcquireTokenHandlerBase.cs: Refreshing access token...  
1/23/2017 5:46:52 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: Storing token in the cache...  
1/23/2017 5:46:52 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - TokenCache.cs: An item was stored in the cache  
1/23/2017 5:46:52 PM: b30a4fcd-4e22-40d8-967c-885a0e82f785 - AcquireTokenHandlerBase.cs: === Token Acquisition finished successfully. An access token was retuned:  
        Access Token Hash: rSD0[snip]E88=
        Expiration Time: 1/23/2017 6:46:51 PM +00:00
        User Hash: 2ekn[snip]4i8=

1/23/2017 5:46:52 PM:  - TokenCache.cs: Serializing token cache with 1 items.  
comments powered by Disqus