gpt4 book ai didi

c# - 使用 DI 在 Azure Function 中使用 ILogger

转载 作者:行者123 更新时间:2023-12-03 03:47:48 25 4
gpt4 key购买 nike

我正在尝试做一件相当简单的事情,但不知何故,在我今天阅读了所有 Microsoft 文档和 SO 帖子之后,我遗漏了一些东西。

我想为我的 Azure Functions 进行一些适当的日志记录,在整个互联网上,最佳实践看起来像是 Application Insights,但我一直在努力让我的 AI 记录我的自定义日志。

这是我的测试函数,如果需要我也可以创建一个测试项目。

主机.json

 {
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": false
},
"logLevel": {
"default": "Information",
"Minerva.Clocking": "Information"
}
}
}
}

启动.cs

    public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{

ServiceProvider serviceProvider = builder.Services.BuildServiceProvider();
Configurations.StaticConfig = serviceProvider.GetService<IConfiguration>();


ConfigureServices(builder.Services);
}

private static void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IEmailService, EmailService>();

services.AddHttpClient();
}
}

我的测试功能

namespace Minerva.Clocking.Endpoints
{
public class LoggingTestFunction
{
private readonly IEmailService m_EmailService;
public LoggingTestFunction(IEmailService emailService)
{
m_EmailService = emailService;
}

[FunctionName("LoggingTestFunction")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation($"C# HTTP trigger function processed a request. Execution time: {DateTime.Now}");

log.LogInformation("Info from function");
log.LogWarning("Warning from function");
log.LogError("Error from function");


m_EmailService.LogSimpleMessage();
m_EmailService.Foo();


string name = req.Query["name"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";

return new OkObjectResult(responseMessage);
}
}
}

我的电子邮件服务

namespace Minerva.Clocking.Services
{
public class EmailService : IEmailService
{
private readonly HttpClient m_httpClient;
private readonly ILogger<EmailService> m_EmailLogger;
public EmailService(IHttpClientFactory httpClientFactory, ILogger<EmailService> logger)
{
m_httpClient = httpClientFactory.CreateClient();
m_EmailLogger = logger;
}

public void LogSimpleMessage()
{
m_EmailLogger.LogInformation("This is a information to be seen in azure");
m_EmailLogger.LogError("This is a error to be seen in azure");
m_EmailLogger.LogWarning("This is a warning to be seen in azure");
}



public void Foo()
{
m_EmailLogger.LogInformation("Foo");
m_Telemetry.TrackTrace("BarLog", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
m_Telemetry.TrackEvent("BarEvent");
}
}
}

每当我在 azure 中查看 AI 日志时,我看到的都是函数本身内记录的消息,而我的 EmailService 中没有任何内容

enter image description here

很可能我遗漏了一些东西,但我不确定是什么,有人可以给我指出一个文档或其他东西,可以让我在 AI 中记录我的 Azure Functions 内的所有内容吗?

我还希望能够在控制台中查看日志,但我在互联网上找不到任何相关信息(我知道我是一个糟糕的搜索者)。

谢谢

编辑:更新了代码以包含 ClientTelemetry。我可以在 Azure 中看到来自遥测的日志,但仍然没有来自 ILogger 的日志。

最佳答案

我相信问题出在您的 host.json 中。请尝试以下操作:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true
},
"logLevel": {
"Minerva.Clocking.Endpoints.LoggingTestFunction": "Information"
}
}
}
}

来源:https://github.com/jeffhollan/functions-csharp-custom-iloggerhttps://github.com/Azure/Azure-Functions/issues/1484#issuecomment-594914999

关于c# - 使用 DI 在 Azure Function 中使用 ILogger,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68458845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com