[Feature] Improve logging in providers

未关闭
#179 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

@shweaver-MSFT 已经在做这个了。

开始于 2022年1月3日。

  • #180 来自 @shweaver-MSFT —— 未关闭

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
csharp

调研方向

首先检查 CommunityToolkit.Diagnostics 包以及拟议的 ILogger、DebugLogger 和 LogManager 入口点。然后检查 WindowsProvider 和 MsalProvider,包括 MsalProvider 的 WithLogging 集成,并找出 issue 中提到的 try/catch 块和 provider 事件。完成的标准是 logging 设计已经确定,并且两个 provider 都能提供有用的失败信息和故障排查信息。

由索引模型根据 Issue 内容生成。

描述

Area: Providers enhancement :sparkles: In-PR :rocket:

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 - 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 logger
public interface ILogger
{
    // Turn logging on or off
    bool IsEnabled { get; set; }

    // Log a string message, if enabled
    void Log(string message);
}

// Default logger implementation
public class DebugLogger : ILogger
{
    // Turned off by default.
    public bool IsEnabled { get; set; } = false;

    public void Log(string message)
    {
        if (IsEnabled)
        {
            System.Diagnostics.Debug.WriteLine(message);
        }
    }
}

// Manages the global logger instance
public class LogManager
{
    public static LogManager Instance { get; } = new LogManager();

    public ILogger Logger { 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:
public class CustomLogger : ILogger
{
    public bool IsEnabled { get; set; } = true;

    public void Log(string message)
    {
        // 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:

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:
https://github.com/MicrosoftDocs/WindowsCommunityToolkitDocs/issues/593

Open questions

  1. Should the auth providers handle logging internally (local to the auth packages)?
  2. OR is a logging system the right thing to do?
  3. Where should the core classes to support logging go? In the Diagnostics or Common packages?
主要语言
C#
星标
156
派生
37
PR 合并指标
30 天内没有已合并 PR

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 有 Pull Request 模板
  • 没有贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

CommunityToolkit/Graph-Controls 的其他 Issue

查看 CommunityToolkit/Graph-Controls 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。