async Task <int?> CreateTestRun() { var requestMessage = new Dictionary <string, object?> { { "name", $"xUnit Runner Test Run on {DateTime.UtcNow:o}" }, { "build", new Dictionary <string, object?> { { "id", buildId } } }, { "isAutomated", true } }; var bodyString = JsonSerializer.Serialize(requestMessage); var url = $"{baseUri}?api-version=1.0"; var responseString = default(string); try { var bodyBytes = Encoding.UTF8.GetBytes(bodyString); var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new ByteArrayContent(bodyBytes) }; request.Content.Headers.ContentType = JsonMediaType; request.Headers.Accept.Add(JsonMediaType); using var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(30)); var response = await client.SendAsync(request, tcs.Token).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { logger.LogWarning($"When sending 'POST {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); previousErrors = true; } responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); var responseJson = JsonSerializer.Deserialize <JsonElement>(responseString); if (responseJson.ValueKind != JsonValueKind.Object) { throw new InvalidOperationException($"Response was not a JSON object"); } if (!responseJson.TryGetProperty("id", out var idProp) || !(idProp.TryGetInt32(out var id))) { throw new InvalidOperationException($"Response JSON did not have an integer 'id' property"); } return(id); } catch (Exception ex) { logger.LogError($"When sending 'POST {url}' with body '{bodyString}'\nexception was thrown: {ex.Message}\nresponse string:\n{responseString}"); throw; } }
async Task <int> CreateTestRun() { var requestMessage = new Dictionary <string, object> { { "name", $"xUnit Runner Test Run on {DateTime.UtcNow.ToString("o")}" }, { "build", new Dictionary <string, object> { { "id", buildId } } }, { "isAutomated", true } }; var bodyString = requestMessage.ToJson(); var url = $"{baseUri}?api-version=1.0"; var respString = default(string); try { var bodyBytes = Encoding.UTF8.GetBytes(bodyString); var request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new ByteArrayContent(bodyBytes) }; request.Content.Headers.ContentType = JsonMediaType; request.Headers.Accept.Add(JsonMediaType); using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(30))) { var response = await client.SendAsync(request, tcs.Token).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { logger.LogWarning($"When sending 'POST {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); previousErrors = true; } respString = await response.Content.ReadAsStringAsync().ConfigureAwait(false); using (var sr = new StringReader(respString)) { var resp = JsonDeserializer.Deserialize(sr) as JsonObject; var id = resp.ValueAsInt("id"); return(id); } } } catch (Exception ex) { logger.LogError($"When sending 'POST {url}' with body '{bodyString}'\nexception was thrown: {ex.Message}\nresponse string:\n{respString}"); throw; } }
public static void SendRequest(IRunnerLogger logger, string url, string method, object body) { using (var finished = new ManualResetEvent(false)) { var request = WebRequest.CreateHttp(url); request.Method = method; request.ContentType = "application/json"; request.Accept = "application/json"; request.BeginGetRequestStream(requestResult => { var bodyString = ToJson(body); var bodyBytes = Encoding.UTF8.GetBytes(bodyString); using (var postStream = request.EndGetRequestStream(requestResult)) { postStream.Write(bodyBytes, 0, bodyBytes.Length); postStream.Flush(); } request.BeginGetResponse(responseResult => { var response = (HttpWebResponse)request.EndGetResponse(responseResult); if ((int)response.StatusCode >= 300) logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); }, null); }, null); finished.WaitOne(); } }
public static void SendRequest(IRunnerLogger logger, string url, string method, object body) { using (var finished = new ManualResetEvent(false)) { var request = WebRequest.CreateHttp(url); request.Method = method; request.ContentType = "application/json"; request.Accept = "application/json"; request.BeginGetRequestStream(requestResult => { var bodyString = ToJson(body); var bodyBytes = Encoding.UTF8.GetBytes(bodyString); using (var postStream = request.EndGetRequestStream(requestResult)) { postStream.Write(bodyBytes, 0, bodyBytes.Length); postStream.Flush(); } request.BeginGetResponse(responseResult => { var response = (HttpWebResponse)request.EndGetResponse(responseResult); if ((int)response.StatusCode >= 300) { logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); } }, null); }, null); finished.WaitOne(); } }
/// <summary> /// Logs a warning message. /// </summary> /// <param name="logger">The logger</param> /// <param name="message">The message to be logged</param> public static void LogWarning( this IRunnerLogger logger, string message) { Guard.ArgumentNotNull(logger); Guard.ArgumentNotNull(message); logger.LogWarning(StackFrameInfo.None, message); }
/// <summary> /// Logs a formatted warning message. /// </summary> /// <param name="logger">The logger</param> /// <param name="messageFormat">The format of the message to be logged</param> /// <param name="args">The format arguments</param> public static void LogWarning( this IRunnerLogger logger, string messageFormat, params object?[] args) { Guard.ArgumentNotNull(logger); Guard.ArgumentNotNull(messageFormat); logger.LogWarning(StackFrameInfo.None, string.Format(messageFormat, args)); }
public static void SendRequest(IRunnerLogger logger, string url, HttpMethod method, object body) { if (previousErrors) { return; } lock (jsonMediaType) { using (var finished = new ManualResetEvent(false)) { XunitWorkerThread.QueueUserWorkItem(async() => { var bodyString = ToJson(body); try { var bodyBytes = Encoding.UTF8.GetBytes(bodyString); var request = new HttpRequestMessage(method, url); request.Content = new ByteArrayContent(bodyBytes); request.Content.Headers.ContentType = jsonMediaType; request.Headers.Accept.Add(jsonMediaType); using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(10))) { var response = await client.SendAsync(request, tcs.Token); if (!response.IsSuccessStatusCode) { logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); previousErrors = true; } } } catch (Exception ex) { logger.LogError($"When sending '{method} {url}' with body '{bodyString}', exception was thrown: {ex.Message}"); previousErrors = true; } finally { finished.Set(); } }); finished.WaitOne(); } } }
public static void SendRequest(IRunnerLogger logger, string url, HttpMethod method, object body) { if (previousErrors) return; lock (jsonMediaType) { using (var finished = new ManualResetEvent(false)) { XunitWorkerThread.QueueUserWorkItem(async () => { var bodyString = ToJson(body); try { var bodyBytes = Encoding.UTF8.GetBytes(bodyString); var request = new HttpRequestMessage(method, url); request.Content = new ByteArrayContent(bodyBytes); request.Content.Headers.ContentType = jsonMediaType; request.Headers.Accept.Add(jsonMediaType); using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(10))) { var response = await client.SendAsync(request, tcs.Token); if (!response.IsSuccessStatusCode) { logger.LogWarning($"When sending '{method} {url}', received status code '{response.StatusCode}'; request body: {bodyString}"); previousErrors = true; } } } catch (Exception ex) { logger.LogError($"When sending '{method} {url}' with body '{bodyString}', exception was thrown: {ex.Message}"); previousErrors = true; } finally { finished.Set(); } }); finished.WaitOne(); } } }
async Task SendRequest( HttpMethod method, ICollection <IDictionary <string, object?> > body) { if (body.Count == 0) { return; } var bodyString = ToJson(body); try { var bodyBytes = Encoding.UTF8.GetBytes(bodyString); var request = new HttpRequestMessage(method, baseUri) { Content = new ByteArrayContent(bodyBytes) }; request.Content.Headers.ContentType = jsonMediaType; request.Headers.Accept.Add(jsonMediaType); using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(30))) { var response = await client.SendAsync(request, tcs.Token).ConfigureAwait(false); if (!response.IsSuccessStatusCode) { logger.LogWarning($"When sending '{method} {baseUri}', received status code '{response.StatusCode}'; request body: {bodyString}"); previousErrors = true; } } } catch (Exception ex) { logger.LogError($"When sending '{method} {baseUri}' with body '{bodyString}', exception was thrown: {ex.Message}"); throw; } }
/// <summary> /// Logs a formatted warning message. /// </summary> /// <param name="logger">The logger</param> /// <param name="messageFormat">The format of the message to be logged</param> /// <param name="args">The format arguments</param> public static void LogWarning(this IRunnerLogger logger, string messageFormat, params object[] args) { logger.LogWarning(StackFrameInfo.None, string.Format(messageFormat, args)); }
/// <summary> /// Logs a warning message. /// </summary> /// <param name="logger">The logger</param> /// <param name="message">The message to be logged</param> public static void LogWarning(this IRunnerLogger logger, string message) { logger.LogWarning(StackFrameInfo.None, message); }
public static void Transform(IRunnerLogger logger, string outputDisplayName, string resourceName, XNode xml, ITaskItem outputFile) => logger.LogWarning($"Skipping '{outputDisplayName}=\"{outputFile.ItemSpec}\"' because XSL-T is not supported on .NET Core");