You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置编译时日志源生成后,无法以扩展方法形式调用日志方法

问题原因与解决方法

你的代码无法以扩展方法形式调用,核心问题是没有在ILogger参数前添加this关键字——扩展方法的定义要求第一个参数必须用this修饰符标记要扩展的类型。

修改后的代码如下:

using Microsoft.Extensions.Logging;

namespace ScreenshotCreator.Logic;

public static partial class Log
{
    private const string Prefix = nameof(Logic);

    [LoggerMessage(EventId = 0,
                  EventName = Prefix + nameof(PlaywrightInitialized),
                  Level = LogLevel.Information,
                  Message = "Playwright initialized")]
    // 关键修改:给ILogger参数添加this修饰符
    public static partial void PlaywrightInitialized(this ILogger logger);
}

修改完成后,你就能通过_logger.PlaywrightInitialized()的形式调用该日志方法了。

额外注意两点:

  • 确保静态类Log是public访问级别,这样跨命名空间也能正常使用该扩展方法
  • 调用代码所在的文件需要引入ScreenshotCreator.Logic命名空间,否则编译器无法识别该扩展方法

内容的提问来源于stack exchange,提问作者mu88

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 19:12:34