1

我正在尝试将 Configure 类中的消息记录到 Application Insights。虽然消息已正确记录在 Run 方法中,但在哪里添加 polly,它没有将消息发送到 Appinsights。

//我能够从这里记录消息。

[FunctionName("Function1")]
public async Task Run(string msg,
            ILogger log)
        {
            log.LogInformation("An error occurred.");
}

// 但不是从这里开始。

[assembly: FunctionsStartup(typeof(Startup))]
namespace TestFunc2
{
    public class Startup : FunctionsStartup
    {
        public override void Configure(IFunctionsHostBuilder builder)
        {
            builder.Services.HTTPExtension();
        }
    }

public static class DependencyExtension
{
    public static IServiceCollection HTTPExtension(this IServiceCollection services)
    {
        services.AddHttpClient<Function1>("client", (provider, client) =>
        {
            var logger = provider.GetService<ILogger<Function1>>();
            logger.LogInformation("func2");
            logger.LogError("func2");
            client.BaseAddress = new Uri("http://www.ggl.com");
            client.DefaultRequestHeaders.Add("Accept", "application/json");
        });

        return services;

    }
}

}
4

2 回答 2

1

我现在可以将日志推送到 App Insights。我正在处理 github 问题,这里通知我们需要在 host.json 中添加日志记录作为一种解决方法,以确保它覆盖预定义的配置。

这是我们需要在host.json中添加的配置

{
    "version": "2.0",
    "logging": {
        "logLevel": {
            "Default": "Information"
        }
    }
}
于 2019-09-16T07:08:57.107 回答
0

昨天有人遇到这个创建这个样本的人 - 看看这是否有帮助:

https://github.com/Runamok81/AzureFunctionHttpClientFactoryPollyLogging

要注意的另一件事是,如果您现在生成自己的 ILogger 实例,则需要将其显式添加为host.json. 我不知道这里是否是这种情况,因为您要求添加一个我认为应该添加的 Function1 实例,但例如在这里我使用字符串“Startup”创建一个:

https://github.com/jeffhollan/functions-csharp-sqlconnection/blob/b5f1d94e54db96112cbda0257e938e6c887c9310/functions-csharp-sqlconnection/Startup.cs#L19

所以我必须更新host.json以确保它包含这种类型的 logLevel “信息”:

https://github.com/jeffhollan/functions-csharp-sqlconnection/blob/b5f1d94e54db96112cbda0257e938e6c887c9310/functions-csharp-sqlconnection/host.json#L5

于 2019-09-14T18:49:13.643 回答