如何在本地运行Azure Functions时获取日志类别?

huangapple go评论48阅读模式
英文:

How can one get the log category when running Azure Functions locally?

问题

我有一个函数应用项目,其中包含我的一些代码、一些与Azure存储的交互、一些持久性功能、实体框架,总体来说就是一锅大杂烩。我希望能够正确配置日志,以隐藏不相关的记录。但问题在于,默认的控制台记录器不会输出此或彼日志消息的“类别”,以便我可以在我的 host.json 文件中添加相应的过滤器。有可能添加 "fileLoggingMode": "always" 设置,但这实际上并没有帮助,因为它也不输出类别。

我假设可以简单地将其部署到Azure,然后从AppInsights中获取类别,但是否有本地可用的选项,而不涉及部署呢?

环境:.net7,功能 v4,dotnet-isolated。

更新:只是为了清晰起见,基本上我想在每个这些日志行中看到一个记录器类别。

英文:

I have a function app project where i have a mix of my code, some Azure storage interaction, some durable functions, entity framework, the whole soup. I want to be able to configure logs properly to hide irrelevant records. But the problem is that the default console logger doesn't output the Category of this or that log message so that I could add a corresponding filter in my host.json file. There is a possibility to add the "fileLoggingMode": "always" setting but that doesn't really help because it doesn't output the category either.

I assume one could simply deploy it to Azure and then grab the category from the AppInsights, but isn't there any locally available option which doesn't involve deployment?

Environment: .net7, function v4, dotnet-isolated.

UPD: just for clarity basically i wanna see a logger category for each of these log lines
如何在本地运行Azure Functions时获取日志类别?

答案1

得分: 1

Program.cs 中找到以下可能的解决方案:

            .ConfigureLogging(builder =>
            {
                builder.AddJsonConsole(options =>
                {
                    options.JsonWriterOptions = new JsonWriterOptions() { Indented = true };
                });
            })

然后在控制台中,我们将看到:
如何在本地运行Azure Functions时获取日志类别?

英文:

Found the following possible solution. In the Program.cs:

            .ConfigureLogging(builder =>
            {
                builder.AddJsonConsole(options =>
                {
                    options.JsonWriterOptions = new JsonWriterOptions() { Indented = true };
                });
            })

and then in the console we'll have:
如何在本地运行Azure Functions时获取日志类别?

答案2

得分: -1

据我所知,

如何在本地运行Azure Functions时获取日志类别?

在本地环境中安装了一个名为Azure Functions Core Tools的程序,用于运行Azure Functions。在每次执行时,它会在控制台上显示发生的情况,如执行函数、启动日志、主机锁释放(租约获取)等,这也可能被称为函数运行时日志或主机日志,具体取决于日志的类型。

默认的 Host.json 代码

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

为了避免这种类型的信息日志,你可以在 host.json 中设置日志级别:

 "logLevel": {
      "Function": "Error",
    }

此外,如果这些集体的大量日志导致更多的成本,可以在 host.json 代码中设置日志级别类别的过滤器,就像我在其中一个解决方法中所示。

英文:

AFAIK,

如何在本地运行Azure Functions时获取日志类别?

There is a program called Azure Functions Core Tools installed in the local environment to run the Azure Functions. It shows in the console on every execution, what's happening such as executing the function, started logging, host lock release (Lease acquisition), etc. which may also known as Function Runtime Logs or Host Logs depends on the logs coming.

Default Host.json code:

{
    "version": "2.0",
    "logging": {
        "applicationInsights": {
            "samplingSettings": {
                "isEnabled": true,
                "excludedTypes": "Request"
            }
        }
    }
}

In order to avoid this type of information logs, you can set the log level in the host.json to:

 "logLevel": {
      "Function": "Error",
    }

Also, if those collective huge logs giving more cost, set the filters in the log level categories from the host.json code as I have shown in one of workarounds.

huangapple
  • 本文由 发表于 2023年3月31日 22:05:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定