首页 > 解决方案 > 在 Azure Service Fabric 中托管时,使用 AppInsights 从 Python 中的 Flask 日志不显示

问题描述

在本地运行我的烧瓶应用程序时,我可以使用端点并且这些日志消息出现在我的 Azure 门户的 Application Insights 资源中。

在 Service Fabric 容器中运行我的烧瓶应用程序时,我可以使用本地计算机中的端点并获得工作响应,请求信息确实出现在 Application Insights 中,但日志消息没有。

示例代码:


from flask import Flask, jsonify
from flasgger import Swagger
from applicationinsights.flask.ext import AppInsights
from config.AppConfig import configure_app

# create Flask app
app = Flask(__name__)

# configure app from settings
configure_app(app)

# initiate Application Insights - allowing requests, exceptions and logs to be examined in Azure Portal
appinsights = AppInsights(app)

Swagger(app)


# force flushing application insights handler after each request
@app.after_request
def after_request(response):
    appinsights.flush()
    return response


# api routes
@app.route('/diagnostics', methods=['GET'])
def get_diagnostics():
    """This is a diagnostics endpoint to test connectivity.
    ---
    operationId: get_diagnostics
    produces:
      - application/json
    responses:
      200:
        description: returns ok
        examples:
          {
              "status": "ok"
          }
    """

    app.logger.info('Diagnostics endpoint was used.')

    app.logger.debug('This is a debug log message')
    app.logger.info('This is an information log message')
    app.logger.warn('This is a warning log message')
    app.logger.error('This is an error message')
    app.logger.critical('This is a critical message')

    return jsonify({'status': "ok"}), 200


if __name__ == "__main__":

    host = app.config['SERVER_HOST']
    port = int(app.config['SERVER_PORT'])
    app.logger.info('DataScience starting on port %s ... ', port)
    app.run(host, port)

检测密钥在应用配置中设置,如前所述,在非本地运行时请求仍显示在 Azure 门户中。

有什么建议么?谢谢你。

标签: loggingflaskazure-application-insights

解决方案


推荐阅读