edit-icon download-icon

Set logging

Last Updated: Oct 27, 2017

Introduction to access logging

You can configure bucket access logging to store the bucket access logs in a specified bucket for future analysis. The logs are stored as objects in a specified bucket. A text object is generated each hour. The object name format is as follows:

  1. <TargetPrefix><SourceBucket>-YYYY-mm-DD-HH-MM-SS-UniqueString

The ‘TargetPrefix’ needs to be specified by the user during configuration.

The logging configuration consists of the following parts:

  • TargetBucket: The name of the target bucket. The log objects generated are stored in this bucket.

  • TargetPrefix: The prefix of the log object name. It can be null.

For more information about the log object name format and log format, see Set access logging.

Enable logging

The following code enables the logging feature, stores logs in the ‘TargetBucket’, and requires that the log objects are prefixed with ‘logging/‘:

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. // Enable access logging
  5. var request = new SetBucketLoggingRequest(bucketName, targetBucketName, "logging-");
  6. client.SetBucketLogging(request);

Note:

  • The bucketName and the targetBucketName can be the same bucket.

  • For complete code, see GitHub

View logging settings

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. var result = client.GetBucketLogging(bucketName);
  5. Console.WriteLine("Get bucket:{0} Logging, prefix:{1}, target bucket:{2}",
  6. bucketName, result.TargetPrefix, result.TargetBucket);

For complete code, see GitHub

Disable logging

  1. using Aliyun.OSS;
  2. // Initialize an OSSClient
  3. var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
  4. client.DeleteBucketLogging(bucketName);

Note:

  • Once the logging feature is disabled, the generated log objects cannot be deleted.

  • For complete code, see GitHub

Thank you! We've received your feedback.