Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable MSAL logging #19637

Merged
merged 2 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 when IsEnabled(EventLevel.Error, EventKeywords.All):
LogMsalError(message);
break;
case Microsoft.Identity.Client.LogLevel.Warning when IsEnabled(EventLevel.Warning, EventKeywords.All):
LogMsalWarning(message);
break;
case Microsoft.Identity.Client.LogLevel.Info when IsEnabled(EventLevel.Informational, EventKeywords.All):
LogMsalInformational(message);
break;
case Microsoft.Identity.Client.LogLevel.Verbose when IsEnabled(EventLevel.Verbose, EventKeywords.All):
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