gpt4 book ai didi

Python 记录器异常在 Azure Application Insights(Azure Function)中记录为跟踪

转载 作者:行者123 更新时间:2023-12-02 08:30:56 25 4
gpt4 key购买 nike

我尝试将 Application Insights 中的 Python Azure 函数处理的异常记录为异常。无论我做什么,它总是被写成具有错误严重级别的轨道。本地控制台中的输出为“红色”(异常(exception))。

我尝试过的:

  • 将 function.json 日志记录部分更改为:

    "ApplicationInsights": {
    "LogLevel": {
    "Default": "Error"
    }
  • 异常方法的各种调用

    except Exception as e:
logger.exception("An error has occured",exc_info=e)
logger.exception("An error has occured")
logger.exception()
  • 尝试直接调用遥测客户端
    tc.track_exception()
  • 清除处理程序并将其添加到记录器

编辑:

举例说明我的意思: image

最佳答案

感谢 Orsiris de Jong,经过几个小时的战斗,我找到了解决方案:

handler = AzureLogHandler(connection_string = "instrmental key here")
handler.setLevel(logging.ERROR)
logger = logging.getLogger()
logger.propagate = False
if(handler not in logger.handlers):
logger.addHandler(handler)
try:
raise Exception("test")
except Exception as e:
logger.exception('An error has occured', exc_info=True)

此处理程序将 Application Insights 中的跟踪更改为异常,并解决了双重日志记录的问题

关于Python 记录器异常在 Azure Application Insights(Azure Function)中记录为跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61076450/

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