Compartilhar via


LINQ code to query Windows Azure “WADLogsTable” to get rows which are stored after a specific DateTime

Following is LINQ code to query Windows Azure “WADLogsTable” in which you can get rows which are stored after a specific DateTime.

 public static CloudTableQuery<TEntity> GetDiagnosticEntities<TEntity>(
 this TableServiceContext tsc, String tableName,
 DateTime startTime, DateTime endTime,
 String deploymentId = null, String roleName = null, 
 String roleInstanceId = null) where TEntity : WadTableEntity {
 
 const String TickFormat = "D19"; // 19-digit, 0-padded
 String startTimeStr = startTime.Ticks.ToString(TickFormat);
 String endTimeStr = endTime.Ticks.ToString(TickFormat);
 
 String startRoleInstance = AzureDiagnosticsRowKey(deploymentId, roleName, roleInstanceId);
 String endRoleInstance = startRoleInstance.GetNextKey();
 tsc.MergeOptions = MergeOptions.NoTracking; 
 return (from e in tsc.CreateQuery<TEntity>(tableName)
 where (e.PartitionKey.CompareTo(startTimeStr) >= 0 
 && e.PartitionKey.CompareTo(endTimeStr) < 0) 
 && (e.RowKey.CompareTo(startRoleInstance) >= 0
 && e.RowKey.CompareTo(endRoleInstance) < 0)
 select e).AsTableServiceQuery();
 }
 
 private static String AzureDiagnosticsRowKey(String deploymentId = null, String roleName = null, String roleInstanceId = null) {
 var sb = new StringBuilder();
 if (!String.IsNullOrWhiteSpace(deployment)) {
 sb.Append(deployment).Append("___");
 if (!String.IsNullOrWhiteSpace(role)) {
 sb.Append(role).Append("___");
 if (!String.IsNullOrWhiteSpace(roleInstance))
 sb.Append(roleInstance);
 }
 }
 return sb.ToString();
 }
 
 private static String GetNextKey(String @string) {
 var sb = new StringBuilder(@string); 
 sb[sb.Length - 1]++; 
 return sb.ToString();
 }
 

Note: If you will not use tsc.MergeOptions = MergeOption.NoTracking, the more Iogs entries you scan, your application will consume more and more memory.