我有一个 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
最佳答案
日志级别的主要区别(如LogInformation、LogError、LogWarning、LogCritical等) ) 是当您从应用程序代码写入跟踪时,您应该为跟踪分配日志级别。日志级别为您提供了一种限制从跟踪中收集的数据量的方法。请参阅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/