gpt4 book ai didi

c# - 构造函数中的依赖关系日志的行为与注入(inject)到函数中的日志的行为不同

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

我创建了以下函数:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp1
{
public class DINotWorking
{
ILogger _log;
public DINotWorking(ILogger<DINotWorking> log)
{
_log = log;
_log.LogInformation("I can in constructor");
}

[FunctionName("HttpTrg1")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,ILogger logger1)
{
_log.LogInformation("Contructor injected Log works !");
logger1.LogInformation("Function injected log Work!");

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

string responseMessage = $"Hello, '{name}'. This HTTP triggered function executed successfully.";

return new OkObjectResult(responseMessage);
}
}
}

host.json

{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"logLevel": {
"FunctionApp1.DINotWorking": "Information",
"FunctionApp1.DINotWorking.User": "Information"
}
}
}

该函数有两个可用的记录器:

  1. _log是类型为 ILogger<DINotWorking> 的依赖注入(inject)类成员.

  2. logger1是类型为 ILogger 的依赖注入(inject)函数参数

我将把它们称为 future 的#1 和#2

它们在不同的环境中表现不同:

本地开发 - Azure Function 工具

在 azure 函数工具的本地开发环境中,#1 和 #2 的行为相同:

enter image description here

已部署 - Azure Function 门户 - Application Insight

在 Azure Function 门户中,它们都登录到 Application Insight。意味着 #1 和 #2 的行为相同:

enter image description here

已部署 - Azure 函数门户 - 文件系统日志

在 Azure Function 门户中,#1 日志条目不会显示在文件系统日志中。 为什么?

enter image description here

问题:为什么 #1 日志条目不显示在文件系统日志中?

Azure 函数日志记录对我来说是一个黑匣子。

显然,构造函数注入(inject)的ILogger是有区别的和函数注入(inject)ILogger .

这与日志类别explained here有什么关系吗?

如何查看代码中的差异?

如何让它们在 host.json 中的所有环境中表现相同?

更新#1

根据 @Hari Krishna 的建议,我更改了 host.json文件如下。

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"fileLoggingMode": "always",
"logLevel": {
"FunctionApp1.HttpTrg1": "Information",
"FunctionApp1.HttpTrg1.User": "Information"
}
}
}

问题仍然存在(未解决)。

如何诊断此问题?无论如何,主机是否告诉我为什么它忽略文件系统日志中构造函数注入(inject)的日志条目?

最佳答案

据我所知,

In Azure Function portal, #1 log entries do not show in the Filesystem logs. Why?

默认情况下,文件系统日志显示函数执行日志,例如正在执行、已执行和错误。

我们可以配置 host.json 文件来获取文件系统日志中的所有日志(App Insights 日志 + 文件系统日志),因为无论您想要推送到应用程序的日志是什么,都应该配置所有内容见解:

{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
},
"fileLoggingMode": "always",
"logLevel": {
"Function.HttpTrigger1": "Information",
"default": "None"
}
}
}

我已在 host.json 中配置了 fileLoggingMode 来记录两者(应用程序和功能)。那么输出是:

文件系统日志:

enter image description here

应用洞察日志:

enter image description here

有关 fileLoggingMode 属性的更多信息,请参阅此 MS Doc和 GitHub 开放信息 Ticket .

更新的答案:

请检查我是否已将代码上传至我的 GitHub Repository通过编写与您类似的代码并展示如何配置 Host.json 以及函数代码。

结果:

在本地我可以看到这两个日志:

enter image description here

部署到 Azure Function App 后,App Insights 日志为:

enter image description here

此外,文件系统日志是:

enter image description here

关于c# - 构造函数中的依赖关系日志的行为与注入(inject)到函数中的日志的行为不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74320224/

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