Windows Azure - 清理WADLogsTable

RMD*_*RMD 15 azure-storage azure-table-storage wadslogtable

我读过有关Windows Azure中DiagnosticMonitor使用的WADLogsTable表是否会自动删除旧日志条目的相互矛盾的信息.

我猜它没有,而是会永远长大 - 花钱我.:)

如果是这种情况,是否有人有一个很好的代码示例,如何手动清除此表中的旧日志条目?也许基于时间戳?我会定期从worker角色运行此代码.

cor*_*vus 9

Windows Azure诊断创建的表中的数据不会自动删除.

但是,Windows Azure PowerShell Cmdlet包含专门针对此情况的cmdlet.

PS D:\> help Clear-WindowsAzureLog

名称Clear-WindowsAzureLog

大纲从存储帐户中删除Windows Azure跟踪日志数据.

语法Clear-WindowsAzureLog [-DeploymentId] [-From] [-To] [-StorageAccountName] [-StorageAccountKey] [-UseD evelopmentStorage] [-StorageAccountCredentials] []

Clear-WindowsAzureLog [-DeploymentId <String>] [-FromUtc <DateTime>] [-ToUt
c <DateTime>] [-StorageAccountName <String>] [-StorageAccountKey <String>]
[-UseDevelopmentStorage] [-StorageAccountCredentials <StorageCredentialsAcc
ountAndKey>] [<CommonParameters>]
Run Code Online (Sandbox Code Playgroud)

您需要指定-ToUtc参数,并删除该日期之前的所有日志.

如果需要在辅助角色中的Azure上执行清理任务,则可以重用C#cmdlet代码.PowerShell Cmdlet在许可的MS Public License下发布.

基本上,只需要3个文件而没有其他外部依赖项:DiagnosticsOperationException.cs,WadTableExtensions.cs,WadTableServiceEntity.cs.

  • 我似乎无法找到这个命令?我已经安装了https://www.windowsazure.com/en-us/downloads/?fb=en-us上的CLI和PS命令,但是当我尝试运行时,我仍然得到一个"无法识别的cmdlet".即使是帮助也无法返回任何信息(并建议我更新帮助,我有).救命! (3认同)

Rom*_*kij 5

更新了Chriseyre2000的功能.这为需要删除数千条记录的情况提供了更多性能:通过PartitionKey搜索和分块逐步过程.请记住,最好选择在存储附近运行它(在云服务中).

public static void TruncateDiagnostics(CloudStorageAccount storageAccount, 
    DateTime startDateTime, DateTime finishDateTime, Func<DateTime,DateTime> stepFunction)
{
        var cloudTable = storageAccount.CreateCloudTableClient().GetTableReference("WADLogsTable");

        var query = new TableQuery();
        var dt = startDateTime;
        while (true)
        {
            dt = stepFunction(dt);
            if (dt>finishDateTime)
                break;
            var l = dt.Ticks;
            string partitionKey =  "0" + l;
            query.FilterString = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThan, partitionKey);
            query.Select(new string[] {});
            var items = cloudTable.ExecuteQuery(query).ToList();
            const int chunkSize = 200;
            var chunkedList = new List<List<DynamicTableEntity>>();
            int index = 0;
            while (index < items.Count)
            {
                var count = items.Count - index > chunkSize ? chunkSize : items.Count - index;
                chunkedList.Add(items.GetRange(index, count));
                index += chunkSize;
            }
            foreach (var chunk in chunkedList)
            {
                var batches = new Dictionary<string, TableBatchOperation>();
                foreach (var entity in chunk)
                {
                    var tableOperation = TableOperation.Delete(entity);
                    if (batches.ContainsKey(entity.PartitionKey))
                        batches[entity.PartitionKey].Add(tableOperation);
                    else
                        batches.Add(entity.PartitionKey, new TableBatchOperation {tableOperation});
                }

                foreach (var batch in batches.Values)
                    cloudTable.ExecuteBatch(batch);
            }
        }
}
Run Code Online (Sandbox Code Playgroud)