Azure Function异常在Application Insights中全部显示为RpcException

xxls0lw8  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(110)

我在一个隔离进程中运行.NET 7上的Azure函数(V4)。所有例外情况在Azure Application Insights中显示为Rpcident。看起来我的函数被 Package 在这个“异常”中,并且它只能返回RpcException而不是我显式抛出的AppUserException。
这导致数据非常不清晰。我该怎么办?
我只是做了一个新的函数来测试它:

<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.18.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.0.13" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Storage" Version="6.0.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Timer" Version="4.2.0" />
<PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.13.0" />
namespace FunctionApp1
{
    public class Function1
    {
        private readonly ILogger _logger;

        public Function1(ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger<Function1>();
        }

        [Function("Function1")]
        public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
        {
            _logger.LogInformation("C# HTTP trigger function processed a request.");

            throw new AppUserException("usererror");
        }
    }
}

program.cs

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureAppConfiguration(builder =>
    {
        // KeyVault
        var keyVaultEndpoint = GetKeyVaultEndpoint();
        if (string.IsNullOrWhiteSpace(keyVaultEndpoint))
        {
            throw new Exception("keyVaultEndpoint wasn't provided.");
        }
        var credentials = new DefaultAzureCredential();
        builder.AddAzureKeyVault(new Uri(keyVaultEndpoint), credentials, new KeyVaultSecretManager());

        // Azure App Config
        var azureAppConfigEndpoint = GetAzureAppConfigEndpoint();
        if (string.IsNullOrWhiteSpace(azureAppConfigEndpoint))
        {
            throw new Exception("azureAppConfigEndpoint wasn't provided.");
        }
        builder.AddAzureAppConfiguration(options =>
        {
            options.Connect(azureAppConfigEndpoint)
                .UseFeatureFlags(options =>
                {
                    options.CacheExpirationInterval = TimeSpan.FromSeconds(60);
                });
        });
        builder.AddJsonFile("appsettings.json", optional: true);
    })
    .ConfigureServices(services =>
    {
        services.AddApplicationInsightsTelemetryWorkerService();
        services.ConfigureFunctionsApplicationInsights();
        services.Configure<LoggerFilterOptions>(options =>
        {
            var toRemove = options.Rules.FirstOrDefault(rule => rule.ProviderName
                == "Microsoft.Extensions.Logging.ApplicationInsights.ApplicationInsightsLoggerProvider");

            if (toRemove is not null)
            {
                options.Rules.Remove(toRemove);
            }
        });
        services.AddSolutionServices();
    })
    .ConfigureLogging((hostingContext, logging) =>
    {
        // Make sure the configuration of the appsettings.json file is picked up.
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
    })
    .Build();
static string? GetKeyVaultEndpoint() => Environment.GetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUP__KEYVAULT__CONFIGURATIONVAULT");
static string? GetAzureAppConfigEndpoint() => Environment.GetEnvironmentVariable("AZURE_APPCONFIG_ENDPOINT");

host.Run();
km0tfn4u

km0tfn4u1#

看起来应该有RpcException。在Github上有多个问题,它似乎是Application Insights团队的未决项目。您可以在以下网站上获得有关它们的更多信息:
https://github.com/Azure/azure-functions-dotnet-worker/issues/370
https://github.com/Azure/azure-functions-host/issues/6284
https://github.com/Azure/azure-functions-host/issues/4192#issuecomment-481813144

s4chpxco

s4chpxco2#

我相信我已经解决了这个问题,至少在我的情况下。
Azure Function和AppInsights的默认行为似乎是将Azure Function Host日志发送到AppInsights。无论出于何种原因,主机日志都会将所有记录的异常替换为RpcException(即使将EnableUserCodeException设置为true)。然后,当日志被发送到AppInsights时,您将获得相同的行为。
但是,如果您将函数主机配置为将日志直接推送到AppInsights,则会获得预期的异常类型。
https://learn.microsoft.com/en-us/azure/azure-functions/dotnet-isolated-process-guide#application-insights

相关问题