azure 函数logError慢

标签 azure function performance logging ilogger

我有一个 Azure 函数,其方法采用 ILogger 作为参数。

log.LogInformation(...)
log.LogWarning(...)

速度很快,但是

log.LogError(...)

非常慢。

可能是什么原因?

2022-02-18T13:59:48.852 [Information] this is a test
2022-02-18T13:59:48.852 [Information] this is a test
x100
2022-02-18T13:59:48.856 [Information] Information loop: 4ms
2022-02-18T13:59:48.856 [Warning] this is a test
2022-02-18T13:59:48.856 [Warning] this is a test
x100
2022-02-18T13:59:48.859 [Information] Warning loop: 3ms
2022-02-18T13:59:48.902 [Error] this is a test
2022-02-18T13:59:48.996 [Error] this is a test
x100
2022-02-18T13:59:54.940 [Information] Error loop: 6080ms

最佳答案

日志级别的主要区别(如LogInformationLogErrorLogWarningLogCritical等) ) 是当您从应用程序代码写入跟踪时,您应该为跟踪分配日志级别。日志级别为您提供了一种限制从跟踪中收集的数据量的方法。请参阅here

我已经尝试了您所遵循的方法来了解日志级别处理时间。

记录错误取决于应用程序代码故障。如果 azure 函数中存在任何未处理的异常,这些异常仅由 LogError 处理(Exception、ExceptionMsg),但 AppInsights 不会认为这是失败。

  • 为了返回更有意义的完整错误消息,我想这就是您需要时间来执行错误日志的原因。

解决方法如下

在这里,我添加日志级别以了解完成这些日志级别迭代时间所需的确切时间。我看不到日志执行有任何延迟,这取决于我们在日志执行之前编写的业务逻辑。

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log,ExecutionContext context)
        {
            Stopwatch sw = Stopwatch.StartNew();
            log.LogInformation("Time taken:"+ sw.ElapsedMilliseconds.ToString());
            log.LogInformation("C# HTTP Information log");
            log.LogInformation("C# HTTP information log");
            log.LogInformation("Time taken:" + sw.ElapsedMilliseconds.ToString());
            log.LogWarning("C# HTTP Warning log");
            log.LogWarning("C# HTTP warning log.");
            log.LogInformation("Time taken:" + sw.ElapsedMilliseconds.ToString());
            log.LogError("C# HTTP Error log");
            log.LogError("C# HTTP Error log");
            log.LogInformation("Time taken:" + sw.ElapsedMilliseconds.ToString());
            log.LogCritical("C# HTTP Critical log");
            log.LogCritical("C# HTTP Critical log");
            log.LogInformation("Time taken:" + sw.ElapsedMilliseconds.ToString());
            sw.Stop();

结果

关于 azure 函数logError慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71175163/

相关文章:

azure - 如何获取事件版本所基于的分支的名称?

javascript - 直接在 JS 中动态改变 transition 还是使用 css classes 对性能更好?

azure - 将静态 HTML 网站从 Github 部署到 Azure Static Web App

Azure - web.config 中的 customErrors ="off"未在 Azure 应用程序(云服务)中显示详细错误

c# - 调试测试时从 local.setting.json 读取值

c# - 如何调用(执行)一个函数,其名称存储在数据库中?

Python:设置函数的最大值并循环查找数组中下一个值的最大值

javascript - 在 React 中将状态从子组件更新到父组件

Python 3 urllib VS 请求性能

python - 在庞大的数组中寻找最近的数组