问题描述
在App Service的门户页面,很容易就可以查看到“Web 服务器日志记录”是否配置。
但是,如何通过Python Code获取到呢?
问题解答
在使用 Python SDK 时获取 web app 的日志?这是一个常见的问题,尤其是在调试和监控应用程序时。可以通过 Web App ( App Service ) 提供的官方管理站点的SDK来实现:azure-mgmt-web
执行步骤
第一步:初始化 WebSiteManagementClient 对象
第二步:调用client.web_apps.get_configuration 就可以获取到 Web App的配置信息
第三步:查看 http_logging_enabled 的设置值
- web服务器日志记录 - 关闭状态
http_logging_enabled : False
- web服务器日志记录 - 存储
http_logging_enabled : True , WEBSITE_HTTPLOGGING_CONTAINER_URL: 存储容器, WEBSITE_HTTPLOGGING_RETENTION_DAYS: 保留 天数
- web服务器日志记录 - 文件系统
http_logging_enabled : True ,logs_directory_size_limit : log quota (MB)大小, WEBSITE_HTTPLOGGING_RETENTION_DAYS: 保留 天数
示例代码
from azure.identity import DefaultAzureCredential from azure.mgmt.web import WebSiteManagementClient from azure.identity import ClientSecretCredential, AzureAuthorityHosts client_id = 'xx-x-x-x-xxx' azure_secret = "xxxxxx" client_tenant = 'xx-x-x-x-xxx' credential_scopes = ["https://management.chinacloudapi.cn/.default"] base_url = 'https://management.chinacloudapi.cn/' credentials = ClientSecretCredential(tenant_id=client_tenant, client_id=client_id, client_secret=azure_secret, authority=AzureAuthorityHosts.AZURE_CHINA) # Replace with your subscription ID subscription_id = 'xx-x-x-x-xxx' # Replace with your resource group name and app service name resource_group_name = 'xxxx' app_service_name = 'xxxx' # Create a WebSiteManagementClient client = WebSiteManagementClient(credentials, subscription_id,base_url=base_url, credential_scopes=credential_scopes) # Get the app settings app_settings = client.web_apps.list_application_settings(resource_group_name, app_service_name) # Print the app settings for key, value in app_settings.properties.items(): print(f"{key}: {value}") print("----------------------------------\n") # Get the app service configuration app_service_config = client.web_apps.get_configuration(resource_group_name, app_service_name) # Print the app service configuration print(app_service_config)
执行结果
参考资料
Gets the configuration of an app, such as platform version and bitness, default documents, virtual applications, Always On, etc.
Description for Gets the configuration of an app, such as platform version and bitness, default documents, virtual applications, Always On, etc.
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!