【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息

简介: 本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。

问题描述

在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)

 

执行结果

 

参考资料

get_configuration : https://learn.microsoft.com/en-us/python/api/azure-mgmt-web/azure.mgmt.web.v2024_04_01.operations.webappsoperations?view=azure-python#azure-mgmt-web-v2024-04-01-operations-webappsoperations-get-configuration

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.

 


 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关实践学习
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
【涂鸦即艺术】基于云应用开发平台CAP部署AI实时生图绘板
相关文章
|
8月前
|
Java 应用服务中间件 API
【App Service】部署War包到Azure云上遇404错误
Java应用部署至Azure App Service for Windows后报404,本地运行正常。经排查,日志提示类文件版本不兼容:应用由Java 17(class file version 61.0)编译,但环境仅支持到Java 11(55.0)。错误根源为Java版本不匹配。调整App Service的Java版本至17后问题解决,成功访问接口。
880 3
|
8月前
|
存储 Linux 网络安全
【Azure App Service】Root CA on App Service
Azure App Service for Windows应用连接外部SSL服务时,需确保其证书由受信任的根CA颁发。多租户环境下无法修改根证书,但ASE(单租户)可加载自定义CA证书。若遇证书信任问题,可更换为公共CA证书或将应用部署于ASE并导入私有CA证书。通过Kudu的PowerShell(Windows)或SSH(Linux)可查看当前受信任的根证书列表。
180 13
|
9月前
|
API 网络架构 容器
【Azure Container App】查看当前 Container App Environment 中的 CPU 使用情况的API
在扩展 Azure Container Apps 副本时,因 Container App Environment 的 CPU 核心数已达上限(500 cores),导致扩展失败。本文介绍如何使用 `az rest` 命令调用 Azure China Cloud 管理 API,查询当前环境的 CPU 使用情况,并提供具体操作步骤及示例。
325 17
|
9月前
|
运维 监控 安全
EventLog Analyzer:高效的Web服务器日志监控与审计解决方案
ManageEngine EventLog Analyzer是一款企业级Web服务器日志监控与审计工具,支持Apache、IIS、Nginx等主流服务器,实现日志集中管理、实时威胁检测、合规报表生成及可视化分析,助力企业应对安全攻击与合规挑战,提升运维效率。
437 1
|
9月前
|
数据安全/隐私保护
【Azure Function App】PowerShell Function 执行 Get-AzAccessToken 的返回值类型问题:System.String 与 System.Security.SecureString
将PowerShell Function部署到Azure Function App后,Get-AzAccessToken返回值类型在不同环境中有差异。正常为SecureString类型,但部分情况下为System.String类型,导致后续处理出错。解决方法是在profile.ps1中设置环境变量$env:AZUREPS_OUTPUT_PLAINTEXT_AZACCESSTOKEN=false,以禁用明文输出。
233 1
|
前端开发 Python
8. Python3源码—Code对象与pyc文件
# 8.1. Python程序的执行过程 Python解释器在执行任何一个Python程序文件时,首先进行的动作都是先对文件中的Python源代码进行编译,编译的主要结果是产生一组Python的byte code(字节码),然后将编译的结果交给Python的虚拟机(Virtual Machine),由虚拟机按照顺序一条一条地执行字节码,从而完成对Python程序的执行动作。 对于Pytho
7915 0
|
9月前
|
数据采集 机器学习/深度学习 人工智能
Python:现代编程的首选语言
Python:现代编程的首选语言
1475 102
|
9月前
|
数据采集 机器学习/深度学习 算法框架/工具
Python:现代编程的瑞士军刀
Python:现代编程的瑞士军刀
483 104
|
9月前
|
人工智能 自然语言处理 算法框架/工具
Python:现代编程的首选语言
Python:现代编程的首选语言
373 103
|
9月前
|
机器学习/深度学习 人工智能 数据挖掘
Python:现代编程的首选语言
Python:现代编程的首选语言
399 82