gpt4 book ai didi

azure - 如何在其他类中注入(inject)默认的 azure 函数记录器?

转载 作者:行者123 更新时间:2023-12-03 00:21:45 24 4
gpt4 key购买 nike

我正在使用 dotnet core 3.1 的 azure 函数,但我无法使自定义日志按预期工作。

local.settings.json 文件的值部分中,我输入了以下键:APPINSIGHTS_INSTRUMENTATIONKEY 并且我在文档中读到函数运行时会自动添加日志.

我可以在应用程序洞察面板上看到默认日志,但我的自定义日志不会写入其中。在我的类里面我这样做了:

     private readonly ILogger<LoginService> _logger;
public CustomService(ILogger<CustomService> logger)
{
_logger = logger;
}

public void Test()
{
_logger.LogError("TestLog");
}

如何在其他类的构造函数(而不是函数方法本身)中注入(inject)默认日志记录的正确方法?

最佳答案

ILogger is indeed not a valid injections. ILogger does work andcreates a filter of type T. However you need to manually add theability to listen to custom filters (in this case your class) for themto show up. you can do that in host.json. Here's a sample.

主机.json:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingExcludedTypes": "Request",
"samplingSettings": {
"isEnabled": true
}
},
"logLevel": {
"Hollan.Functions.HttpTrigger": "Information"
}
}
}

HttpTrigger.cs

   public class HttpTrigger
{
private readonly ILogger<HttpTrigger> _log;

public HttpTrigger(ILogger<HttpTrigger> log)
{
_log = log;
}

[FunctionName("HttpTrigger")]
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req)
{
_log.LogInformation("C# HTTP trigger function processed a request.");

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);
}
}
}

来源:https://github.com/Azure/Azure-Functions/issues/1484

关于azure - 如何在其他类中注入(inject)默认的 azure 函数记录器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67508001/

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