日志管理

最近更新时间:2024-09-14 18:11:11

我的收藏

简介

本文档提供关于日志管理的 API 概览以及 SDK 示例代码。

相关示例

功能名称
描述
示例代码
设置日志管理
为源存储桶开启日志记录
查询日志管理
查询源存储桶的日志配置信息

前期准备:初始化 COS 服务实例

public class DataMangeModel { private CosXml cosXml; //将服务用户设置成数据成员 // 初始化COS服务实例 private void InitCosXml() { string region = Environment.GetEnvironmentVariable("COS_REGION"); CosXmlConfig config = new CosXmlConfig.Builder() .SetRegion(region) // 设置默认的地域, COS 地域的简称请参照 https://cloud.tencent.com/document/product/436/6224 .Build(); string secretId = Environment.GetEnvironmentVariable("SECRET_ID"); // 云 API 密钥 SecretId, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi string secretKey = Environment.GetEnvironmentVariable("SECRET_KEY"); // 云 API 密钥 SecretKey, 获取 API 密钥请参照 https://console.cloud.tencent.com/cam/capi long durationSecond = 600; //每次请求签名有效时长,单位为秒 QCloudCredentialProvider qCloudCredentialProvider = new DefaultQCloudCredentialProvider(secretId, secretKey, durationSecond); this.cosXml = new CosXmlServer(config, qCloudCredentialProvider); } }

使用案例

设置日志管理

PUT Bucket logging 用于为源存储桶开启日志记录,将源存储桶的访问日志保存到指定的目标存储桶中。
public void PutBucketLogging() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; PutBucketLoggingRequest request = new PutBucketLoggingRequest(bucket); // 设置保存日志的目标路径 request.SetTarget("targetbucket-1250000000", "logs/"); //执行请求 PutBucketLoggingResult result = cosXml.PutBucketLogging(request); //请求成功 Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

查询日志管理

GET Bucket logging 用于查询指定存储桶的日志配置信息。
public void GetBucketLogging() { try { // 存储桶名称,此处填入格式必须为 bucketname-APPID, 其中 APPID 获取参考 https://console.cloud.tencent.com/developer string bucket = "examplebucket-1250000000"; GetBucketLoggingRequest request = new GetBucketLoggingRequest(bucket); //执行请求 GetBucketLoggingResult result = cosXml.GetBucketLogging(request); //请求成功 BucketLoggingStatus status = result.bucketLoggingStatus; if (status != null && status.loggingEnabled != null) { string targetBucket = status.loggingEnabled.targetBucket; string targetPrefix = status.loggingEnabled.targetPrefix; } Console.WriteLine(result.GetResultInfo()); } catch (COSXML.CosException.CosClientException clientEx) { Console.WriteLine("CosClientException: " + clientEx); } catch (COSXML.CosException.CosServerException serverEx) { Console.WriteLine("CosServerException: " + serverEx.GetInfo()); } }

API 操作

关于设置日志管理的 API 接口说明,请参见 PUT Bucket logging 文档。
关于查询日志管理的 API 接口说明,请参见 GET Bucket logging 文档。