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

在AX中调用PuppeteerSharp遇LogError方法缺失问题求助

Dynamics 365 AX调用自定义PDF生成DLL日志API兼容问题

问题详情

  • 运行场景:在Dynamics 365 for Finance and Operations(AX)中调用自定义C# DLL,基于PuppeteerSharp生成PDF
  • 触发错误:Method not found: 'Void Microsoft.Extensions.Logging.LoggerExtensions.LogError(Microsoft.Extensions.Logging.ILogger, System.Exception, System.String, System.Object[])'
  • 核心原因:自定义DLL依赖的LogError(Exception, string, object[])重载方法仅在高版本Microsoft.Extensions.Logging.Abstractions中存在,但AX内置的该DLL版本为1.1.2,不支持此重载;自定义DLL单独测试正常,仅AX调用时触发异常,更换多版本PuppeteerSharp仍无法解决日志依赖冲突

相关代码

X++ 调用代码

class ConvertXMLtoPDF
{
    public static void main(Args _args)
    {
        try
        {
            helper.PuppeteerPDF();
        }
        catch(Exception::CLRError)
        {
            System.Exception ex = CLRInterop::getLastException();
            info(ex.ToString());
        }
    }
}

C# 自定义DLL代码

using PuppeteerSharp;
using PuppeteerSharp.Media;
using Microsoft.Extensions.Logging;

public async void PuppeteerPDF()
{
    string path = @"C:\Temp\test\tes2t.pdf";
    string save = @"C:\Temp\test\test.html";
    var options = new LaunchOptions
    {
        Headless = true,
        ExecutablePath = "C:\\Program Files\\Google\\Chrome\\Application\\Chrome.exe"
    };
    try
    {
        using (var browser = await Puppeteer.LaunchAsync(options))  // <--- 报错触发位置
        using (var page = await browser.NewPageAsync())
        {
            await page.GoToAsync(save);
            var result = await page.GetContentAsync();
            await page.PdfAsync(path, new PdfOptions
            {
                Format = PaperFormat.A4,
                DisplayHeaderFooter = true,
                Scale = (decimal)0.5,
                PrintBackground = true,
            });
            await page.DisposeAsync();
            await browser.DisposeAsync();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
        logger.LogError(ex, "test");
        logger.LogInformation(ex, "test");
    }
}

解决方案

方案1:适配旧版本日志API(最安全)

直接修改自定义DLL的日志调用,避免使用AX不支持的重载:

  • 替换异常捕获中的日志代码,改用1.1.2版本支持的API:
    // 替换原来的LogError和LogInformation调用
    logger.LogError($"test: {ex.ToString()}");
    logger.LogInformation($"test: {ex.ToString()}");
    
  • 禁用PuppeteerSharp的内置日志,防止其内部触发高版本API调用:
    var options = new LaunchOptions
    {
        Headless = true,
        ExecutablePath = "C:\\Program Files\\Google\\Chrome\\Application\\Chrome.exe",
        LogLevel = LogLevel.None // 关闭内置日志输出
    };
    

方案2:降级PuppeteerSharp依赖

选择兼容Microsoft.Extensions.Logging.Abstractions 1.1.2的PuppeteerSharp版本,比如1.x系列版本,该版本依赖的日志库版本较低,不会调用高版本的LogError重载。

方案3:绑定重定向(需谨慎验证)

若必须使用高版本依赖,可通过绑定重定向让AX加载指定版本的日志DLL:

  1. 将高版本Microsoft.Extensions.Logging.Abstractions.dll(如2.1.1版本)复制到AX客户端/服务器的运行目录(例:C:\Program Files\Microsoft Dynamics 365\7.0\Client\Bin)
  2. 修改AX配置文件(如Dynamics.exe.config),添加绑定重定向节点:
    <configuration>
        <runtime>
            <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly>
                    <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" 
                                      publicKeyToken="adb9793829ddae60" 
                                      culture="neutral" />
                    <bindingRedirect oldVersion="0.0.0.0-2.1.1.0" 
                                     newVersion="2.1.1.0" />
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
    </configuration>
    

注意:此方法可能影响AX其他模块的兼容性,必须在测试环境充分验证后再部署到生产。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 20:00:58