internal IReadOnlyList <T> BindResults <T>(LogsQueryResult response) { List <T> results = new List <T>(); if (typeof(IDictionary <string, object>).IsAssignableFrom(typeof(T))) { foreach (var table in response.Tables) { foreach (var row in table.Rows) { IDictionary <string, object> rowObject = typeof(T).IsInterface ? new Dictionary <string, object>() : (IDictionary <string, object>)Activator.CreateInstance <T>(); for (var i = 0; i < row.Count; i++) { rowObject[table.Columns[i].Name] = row.GetObject(i); } results.Add((T)rowObject); } } } else { foreach (var table in response.Tables) { foreach (var row in table.Rows) { results.Add(Deserialize <T>(row)); } } } return(results); }
internal static IReadOnlyList <T> BindResults <T>(LogsQueryResult response) { // TODO: this is very slow List <T> results = new List <T>(); if (typeof(IDictionary <string, object>).IsAssignableFrom(typeof(T))) { foreach (var table in response.Tables) { foreach (var row in table.Rows) { IDictionary <string, object> rowObject = typeof(T).IsInterface ? new Dictionary <string, object>() : (IDictionary <string, object>)Activator.CreateInstance <T>(); for (var i = 0; i < row.Count; i++) { rowObject[table.Columns[i].Name] = row.GetObject(i); } results.Add((T)rowObject); } } } else if (typeof(T).IsValueType || typeof(T) == typeof(string)) { foreach (var table in response.Tables) { foreach (var row in table.Rows) { // TODO: Validate results.Add((T)Convert.ChangeType(row.GetObject(0), typeof(T), CultureInfo.InvariantCulture)); } } } else { foreach (var table in response.Tables) { var columnMap = table.Columns .Select((column, index) => (Property: typeof(T).GetProperty(column.Name, BindingFlags.Instance | BindingFlags.Public), index)) .Where(columnMapping => columnMapping.Property?.SetMethod != null) .ToArray(); foreach (var row in table.Rows) { T rowObject = Activator.CreateInstance <T>(); foreach (var(property, index) in columnMap) { property.SetValue(rowObject, Convert.ChangeType(row.GetObject(index), property.PropertyType, CultureInfo.InvariantCulture)); } results.Add(rowObject); } } } // TODO: Maybe support record construction return(results); }
public async Task QueryLogsWithPartialSuccess() { var client = new LogsQueryClient(new DefaultAzureCredential()); #region Snippet:QueryLogsWithPartialSuccess Response <LogsQueryResult> response = await client.QueryWorkspaceAsync( TestEnvironment.WorkspaceId, "My Not So Valid Query", new QueryTimeRange(TimeSpan.FromDays(1)), new LogsQueryOptions { AllowPartialErrors = true }); LogsQueryResult result = response.Value; if (result.Status == LogsQueryResultStatus.PartialFailure) { var errorCode = result.Error.Code; var errorMessage = result.Error.Message; // code omitted for brevity } #endregion }
private async Task <Response <LogsQueryResult> > ExecuteAsync(string workspaceId, string query, DateTimeRange timeRange, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default) { if (workspaceId == null) { throw new ArgumentNullException(nameof(workspaceId)); } QueryBody queryBody = CreateQueryBody(query, timeRange, options, out string prefer); using var message = _queryClient.CreateExecuteRequest(workspaceId, queryBody, prefer); // TODO: https://github.com/Azure/azure-sdk-for-net/issues/20859 // if (options?.Timeout != null) // { // message.NetworkTimeout = options.Timeout; // } if (async) { await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); } else { _pipeline.Send(message, cancellationToken); } switch (message.Response.Status) { case 200: { using var document = async ? await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false) : JsonDocument.Parse(message.Response.ContentStream, default); LogsQueryResult value = LogsQueryResult.DeserializeLogsQueryResult(document.RootElement); return(Response.FromValue(value, message.Response)); } default: { if (async) { throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); } else { throw _clientDiagnostics.CreateRequestFailedException(message.Response); } } } }
private async Task <Response <LogsQueryResult> > ExecuteAsync(string workspaceId, string query, DateTimeRange timeRange, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default) { if (workspaceId == null) { throw new ArgumentNullException(nameof(workspaceId)); } QueryBody queryBody = CreateQueryBody(query, timeRange, options, out string prefer); using var message = _queryClient.CreateExecuteRequest(workspaceId, queryBody, prefer); if (options?.ServerTimeout != null) { // Offset the service timeout a bit to make sure we have time to receive the response. message.NetworkTimeout = options.ServerTimeout.Value.Add(_networkTimeoutOffset); } if (async) { await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); } else { _pipeline.Send(message, cancellationToken); } switch (message.Response.Status) { case 200: { using var document = async ? await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false) : JsonDocument.Parse(message.Response.ContentStream, default); LogsQueryResult value = LogsQueryResult.DeserializeLogsQueryResult(document.RootElement); return(Response.FromValue(value, message.Response)); } default: { if (async) { throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false); } else { throw _clientDiagnostics.CreateRequestFailedException(message.Response); } } } }
public async Task <Response <LogsQueryResult> > GetAsync(string workspaceId, string query, TimeSpan?timespan = null, CancellationToken cancellationToken = default) { if (workspaceId == null) { throw new ArgumentNullException(nameof(workspaceId)); } if (query == null) { throw new ArgumentNullException(nameof(query)); } using var message = CreateGetRequest(workspaceId, query, timespan); await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false); switch (message.Response.Status) { case 200: { LogsQueryResult value = default; using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false); value = LogsQueryResult.DeserializeLogsQueryResult(document.RootElement); return(Response.FromValue(value, message.Response)); }
internal LogQueryResponse(string id, int?status, LogsQueryResult body) { Id = id; Status = status; Body = body; }