繁体   English   中英

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

[英]Python Logger exception is logged as trace in Azure Application Insights (Azure Function)

我尝试在 Application Insights 中将 Python Azure Function 中的已处理异常记录为异常。 无论我做什么,它总是写为具有错误严重性级别的轨道。 本地控制台中的 Output 为“红色”,但除外。

我尝试了什么:

  • 将 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()
  • 清除和添加处理程序到记录器

编辑:

举例说明我的意思:图片

感谢 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 中的异常并解决了双重日志记录问题

将异常记录为纯文本而不是记录完整的跟踪如下所示:

handler = AzureLogHandler(connection_string = "instrmental key here")
handler.setLevel(logging.ERROR)
logger = logging.getLogger()
logger.addHandler(handler)
try:
    raise Exception("test")
except Exception as e:
    # Logging as string
    logger.debug('The following error has occured: {0}'.format(e))
    # Logging as full trace
    # logger.debug('The following error was traced:', exc_info=True)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM