You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When the authentication providers work, they are a delight. But when they don't work, they are rather difficult to troubleshoot.
Describe the solution
We need better logging to expose some insights into what the provider is doing, and what exceptions or failures are occurring. However, I feel like this is a great opportunity to handle logging in a more generic way across the whole toolkit.
I see a few moving parts:
ILogger interface - defines the basic functions of a logger. At a minimum, log a string message.
DebugLogger implementation - extends ILogger and uses System.Diagnostics.Debug.WriteLine to log messages. This is the default.
LogManager singleton - maintains active ILogger implementation used across the packages.
1. The logger interface and implementation
I'd like to see the ILogger interface put in the CommunityToolkit.Diagnostics package (link). This enables us to offer the simple DebugLogger based on System.Diagnostics.Debug.WriteLine
// Defines a basic loggerpublicinterfaceILogger{// Turn logging on or offboolIsEnabled{get;set;}// Log a string message, if enabledvoidLog(stringmessage);}// Default logger implementationpublicclassDebugLogger:ILogger{// Turned off by default.publicboolIsEnabled{get;set;}=false;publicvoidLog(stringmessage){if(IsEnabled){
System.Diagnostics.Debug.WriteLine(message);}}}// Manages the global logger instancepublicclassLogManager{publicstaticLogManagerInstance{get;}=new LogManager();publicILoggerLogger{get;set;}=new DebugLogger();}
2. Consuming the logger
Developers with a custom logger can extend ILogger OR use a shim class instead to connect it to the LogManager:
// Set somewhere in app startup code
LogManager.Instance.Logger.IsEnabled =true;// OR// An example custom logger shim, if you don't want the default DebugLogger:publicclassCustomLogger:ILogger{publicboolIsEnabled{get;set;}=true;publicvoidLog(stringmessage){// Call your custom logger here}}// Set somewhere in app startup code
LogManager.Instance.Logger =new CustomLogger();
3. Applying the logger to WindowsProvider
Pretty straight forward, ensure that every try/catch block sends any exceptions to the logger. Also look for opportunities to log other common events that could help with troubleshooting.
4. Applying the logger to MsalProvider
First ensure that every try/catch block sends any exceptions to the logger. But also, the MsalProvider leverages the PublicClientApplication object from MSAL for .NET which has a mechanism for passing in a method to handle the log output. This is enabled by using the WithLogging method on the client builder. This can be connected with the LogManager like so:
Describe the problem this feature would solve
When the authentication providers work, they are a delight. But when they don't work, they are rather difficult to troubleshoot.
Describe the solution
We need better logging to expose some insights into what the provider is doing, and what exceptions or failures are occurring. However, I feel like this is a great opportunity to handle logging in a more generic way across the whole toolkit.
I see a few moving parts:
ILogger
interface - defines the basic functions of a logger. At a minimum, log a string message.DebugLogger
implementation - extendsILogger
and usesSystem.Diagnostics.Debug.WriteLine
to log messages. This is the default.LogManager
singleton - maintains activeILogger
implementation used across the packages.1. The logger interface and implementation
I'd like to see the
ILogger
interface put in theCommunityToolkit.Diagnostics
package (link). This enables us to offer the simpleDebugLogger
based onSystem.Diagnostics.Debug.WriteLine
2. Consuming the logger
Developers with a custom logger can extend
ILogger
OR use a shim class instead to connect it to theLogManager
:3. Applying the logger to WindowsProvider
Pretty straight forward, ensure that every try/catch block sends any exceptions to the logger. Also look for opportunities to log other common events that could help with troubleshooting.
4. Applying the logger to MsalProvider
First ensure that every try/catch block sends any exceptions to the logger. But also, the
MsalProvider
leverages thePublicClientApplication
object from MSAL for .NET which has a mechanism for passing in a method to handle the log output. This is enabled by using theWithLogging
method on the client builder. This can be connected with theLogManager
like so:clientBuilder.WithLogging((level, message, containsPii) => LogManager.Instance.Logger.Log(message));
Additional context & Screenshots
Here is an example of a developers having issues with troubleshooting in the auth providers:
MicrosoftDocs/WindowsCommunityToolkitDocs#593
Open questions
The text was updated successfully, but these errors were encountered: