Skip to content

Commit

Permalink
enable MSAL logging
Browse files Browse the repository at this point in the history
  • Loading branch information
christothes committed Mar 26, 2021
1 parent be06367 commit bd0a6f0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions sdk/identity/Azure.Identity/src/AzureIdentityEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ internal sealed class AzureIdentityEventSource : EventSource
private const int ProbeImdsEndpointEvent = 4;
private const int ImdsEndpointFoundEvent = 5;
private const int ImdsEndpointUnavailableEvent = 6;
private const int MsalLogVerboseEvent = 7;
private const int MsalLogInfoEvent = 8;
private const int MsalLogWarningEvent = 9;
private const int MsalLogErrorEvent = 10;

private AzureIdentityEventSource() : base(EventSourceName, EventSourceSettings.Default, AzureEventSourceListener.TraitName, AzureEventSourceListener.TraitValue) { }

Expand Down Expand Up @@ -151,6 +155,55 @@ private static string FormatException(Exception ex)
return sb.ToString();
}

[NonEvent]
public void LogMsal(Microsoft.Identity.Client.LogLevel level, string message, bool containsPii)
{
if (!containsPii)
{
switch (level)
{
case Microsoft.Identity.Client.LogLevel.Error:
LogMsalError(message);
break;
case Microsoft.Identity.Client.LogLevel.Warning:
LogMsalWarning(message);
break;
case Microsoft.Identity.Client.LogLevel.Info:
LogMsalInformational(message);
break;
case Microsoft.Identity.Client.LogLevel.Verbose:
LogMsalVerbose(message);
break;
default:
break;
}
}
}

[Event(MsalLogErrorEvent, Level = EventLevel.Error, Message = "{0}")]
public void LogMsalError(string message)
{
WriteEvent(MsalLogErrorEvent, message);
}

[Event(MsalLogWarningEvent, Level = EventLevel.Warning, Message = "{0}")]
public void LogMsalWarning(string message)
{
WriteEvent(MsalLogWarningEvent, message);
}

[Event(MsalLogInfoEvent, Level = EventLevel.Informational, Message = "{0}")]
public void LogMsalInformational(string message)
{
WriteEvent(MsalLogInfoEvent, message);
}

[Event(MsalLogVerboseEvent, Level = EventLevel.Verbose, Message = "{0}")]
public void LogMsalVerbose(string message)
{
WriteEvent(MsalLogVerboseEvent, message);
}

[NonEvent]
private static string FormatStringArray(string[] array)
{
Expand Down
2 changes: 1 addition & 1 deletion sdk/identity/Azure.Identity/src/MsalPublicClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected override ValueTask<IPublicClientApplication> CreateClientAsync(bool as

var authorityUri = new UriBuilder(authorityHost.Scheme, authorityHost.Host, authorityHost.Port, TenantId ?? Constants.OrganizationsTenantId).Uri;

PublicClientApplicationBuilder pubAppBuilder = PublicClientApplicationBuilder.Create(ClientId).WithAuthority(authorityUri).WithHttpClientFactory(new HttpPipelineClientFactory(Pipeline.HttpPipeline));
PublicClientApplicationBuilder pubAppBuilder = PublicClientApplicationBuilder.Create(ClientId).WithAuthority(authorityUri).WithHttpClientFactory(new HttpPipelineClientFactory(Pipeline.HttpPipeline)).WithLogging(AzureIdentityEventSource.Singleton.LogMsal);

if (!string.IsNullOrEmpty(RedirectUrl))
{
Expand Down

0 comments on commit bd0a6f0

Please sign in to comment.