public async Task Invoke(HttpContext httpContext) { if (httpContext == null) { throw new ArgumentNullException(nameof(httpContext)); } var httpLog = new HttpLog(httpContext); try { await _next(httpContext); httpLog.Stop(); _logger.Info(httpLog.ToString()); } catch (Exception ex) { httpLog.Stop(); //TODO Cratea a corrilationId and show that to the client for esier support tickets _logger.Error(httpLog.ToString(), ex); httpContext.Response.StatusCode = 500; await httpContext.Response.WriteAsync("UNKOWN ERROR"); } }
private void WriteHttpLog(RequestTypeEnum requestType, string traceId, string requestBody, string operation, int deviceId) { string DeviceUuid = _deviceService.GetDeviceById_Pref(deviceId).ConfigureAwait(false).GetAwaiter().GetResult()?.DeviceUuid; var now = DateTime.Now; var group = now.ToString("yyyyMMddHHmm"); long.TryParse(group, out long requestGroup); HttpLog _log = new HttpLog() { TraceId = traceId, RequestTime = now, RequestType = (int)requestType, RequestGroup = requestGroup, RemoteIpAddress = CommonHelper.GetClientIpAddress(), RemotePort = CommonHelper.GetClientIpPort(), IdentityIsAuthenticated = true, IdentityName = DeviceUuid, RequestBody = requestBody, RequestContentLength = requestBody == null ? 0 : Encoding.Default.GetByteCount(requestBody), Operation = operation, ServerName = CommonHelper.GetServerName() }; this._logStore.AddQueue(_log); }
private void WriteHttpLog(DateTime now, RequestTypeEnum requestType, string traceId, string requestBody, string operation) { if (_settings.Value.UseMonitor) { var group = now.ToString("yyyyMMddHHmm"); long.TryParse(group, out long requestGroup); _monitorTimeDataManager.AddQueue(new TimeData(group, MonitorContextKeys.signalr, 1)); HttpLog _log = new HttpLog() { TraceId = traceId, RequestTime = now, RequestType = (int)requestType, RequestGroup = requestGroup, RemoteIpAddress = CommonHelper.GetClientIpAddress(), RemotePort = CommonHelper.GetClientIpPort(), IdentityIsAuthenticated = Current.Device != null, IdentityName = Current.Device?.DeviceUuid, RequestBody = requestBody, RequestContentLength = requestBody == null ? 0 : Encoding.Default.GetByteCount(requestBody), Operation = operation, ServerName = CommonHelper.GetServerName() }; this._logStore.AddQueue(_log); } }
private static void WriteResponse(TextWriter writer, string message, params object[] args) { string line = string.Format(message, args); HttpLog.WriteHttpOut(line); writer.WriteLine(line); }
private async Task SetHttpResponse(HttpContext context, HttpLog log) { HttpResponse _response = context.Response; if (_response == null) { return; } //HttpResponse Stream _responseStream = _response.Body; MemoryStream _stream = new MemoryStream(); _response.Body = _stream; await this._next(context); _stream.Position = 0; string _Body = await new StreamReader(_stream).ReadToEndAsync(); _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_body, _Body); _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_statuscode, _response.StatusCode); if (_response.StatusCode == 200 && !_Body.StartsWith("{\"code\":")) { _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_success, true); } else if (_response.StatusCode == 200 && _Body.StartsWith("{\"code\":")) { if (_Body.StartsWith("{\"code\":0")) { _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_success, true); } else { _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_success, false); _monitorTimeDataManager.AddQueue(new TimeData(log.RequestGroup.ToString(), MonitorContextKeys.fault, 1)); } } else { _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_success, false); _monitorTimeDataManager.AddQueue(new TimeData(log.RequestGroup.ToString(), MonitorContextKeys.fault, 1)); } _stream.Position = 0; await _stream.CopyToAsync(_responseStream); _response.Body = _responseStream; if (_stream != null) { _stream.Close(); } }
private void Deserialize(BsonDocument document) { _data = _webDataManager_v4.DataSerializer.Deserialize(document); HttpLog httpLog = _webDataManager_v4.WebRequestSerializer.Deserialize(document); _result_v2 = new HttpResult <string> { Http = new Http_v2(httpLog) }; }
public void Start() { listener.Start(); HttpLog?.Invoke("HTTP Server Startet", null); while (true) { TcpClient client = listener.AcceptTcpClient(); HttpLog?.Invoke("HTTP Server Connected", null); HttpServerHandler handler = new HttpServerHandler(client); new Thread(handler.HandleRequest).Start(); } }
public async Task InvokeAsync(HttpContext context) { var ip = context.Connection.RemoteIpAddress.ToString(); var url = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}{context.Request.QueryString}"; var requestedAt = DateTime.Now; var stopWatch = new Stopwatch(); var response = string.Empty; stopWatch.Start(); var originalBodyStream = context.Response.Body; using (var responseBody = new MemoryStream()) { context.Response.Body = responseBody; await _requestDelegate(context); response = await FormatResponse(context.Response); await responseBody.CopyToAsync(originalBodyStream); } stopWatch.Stop(); var model = new HttpLog { IP = ip, URL = url, Method = context.Request.Method, RequsetHeader = (from t in context.Request.Headers select t).ToDictionary(x => x.Key, x => x.Value.ToArray()), ReqestedAt = requestedAt, Request = (await FormatRequest(context.Request)).Replace("\n\t", string.Empty), ResponseHeader = (from t in context.Response.Headers select t).ToDictionary(x => x.Key, x => x.Value.ToArray()), ResponsedAt = DateTime.Now, Response = response, Duration = stopWatch.ElapsedMilliseconds }; //try { if (AppSettings.MongoLogging) { await _httpLogService.InsertAsync(model, 5000); } if (AppSettings.FileLogging) { Log.Debug(JsonConvert.SerializeObject(model)); } if (AppSettings.SqlLogging) { } //} //catch (Exception ex) { // await _exceptionService.InsertAsync(ex, url, ip); //} await _requestDelegate(context); }
public async Task Invoke(HttpContext context) { var now = DateTime.Now; var group = now.ToString("yyyyMMddHHmm"); long.TryParse(group, out long requestGroup); HttpLog _log = new HttpLog() { TraceId = _tracer.CurrentSpan.Context.TraceId.ToHexString(), RequestTime = now, RequestType = (int)RequestTypeEnum.Http, RequestGroup = requestGroup, ServerName = CommonHelper.GetServerName() }; _monitorTimeDataManager.AddQueue(new TimeData(group, MonitorContextKeys.http, 1)); try { _tracer.CurrentSpan.SetAttribute(MonitorKeys.request_type, RequestTypeEnum.Http.ToString().ToLowerInvariant()); Stopwatch _stopwatch = new Stopwatch(); _stopwatch.Reset(); _stopwatch.Start(); await this.SetConnectionInfo(context, _log); await this.SetIdentity(context, _log); await this.SetHttpRequest(context, _log); this._logStore.AddQueue(_log); // response await this.SetHttpResponse(context, _log); _stopwatch.Stop(); _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_elapsed, _stopwatch.ElapsedMilliseconds); //_tracer.CurrentSpan.SetAttribute( MonitorKeys.response_success, true ); } catch (Exception ex) { _tracer.CurrentSpan.SetAttribute(MonitorKeys.response_success, false); _tracer.CurrentSpan.SetAttribute(MonitorKeys.request_error, ex.Source); _tracer.CurrentSpan.SetAttribute(MonitorKeys.request_errordetail, ex.Message); _monitorTimeDataManager.AddQueue(new TimeData(group, MonitorContextKeys.fault, 1)); throw; } }
private string GetNextLine() { StringBuilder builder = new StringBuilder(); int readByte; while ((readByte = _stream.ReadByte()) != '\n' && readByte != -1) { if (readByte != '\r') { builder.Append((char)readByte); } } string line = builder.ToString(); HttpLog.WriteHttpIn(line); return(line); }
public async Task <int> InsertAsync(HttpLog model, int timeoutMS) { var startTime = DateTime.UtcNow; try { using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeoutMS))) { await _mongoDBContext.HttpLogs.InsertOneAsync(model, cancellationToken : timeoutCancellationTokenSource.Token); } return(1); } catch (OperationCanceledException ex) { await _exceptionService.InsertAsync(ex, MethodBase.GetCurrentMethod().Name, GeneralVariables.LocalIP); var elapsed = DateTime.UtcNow - startTime; return(0); } }
public async Task InvokeAsync(HttpContext context) { if (AppSettings.MongoLogging) { var ip = context.Connection.RemoteIpAddress.ToString(); var url = $"{context.Request.Scheme}://{context.Request.Host}{context.Request.Path}{context.Request.QueryString}"; var requestedAt = DateTime.Now; var stopWatch = new Stopwatch(); try { var response = string.Empty; stopWatch.Start(); var originalBodyStream = context.Response.Body; using (var responseBody = new MemoryStream()) { context.Response.Body = responseBody; await _requestDelegate(context); response = await FormatResponse(context.Response); await responseBody.CopyToAsync(originalBodyStream); } stopWatch.Stop(); var model = new HttpLog { IP = ip, URL = url, Method = context.Request.Method, RequsetHeader = (from t in context.Request.Headers select t).ToDictionary(x => x.Key, x => x.Value.ToArray()), ReqestedAt = requestedAt, Request = (await FormatRequest(context.Request)).Replace("\n\t", string.Empty), ResponseHeader = (from t in context.Response.Headers select t).ToDictionary(x => x.Key, x => x.Value.ToArray()), ResponsedAt = DateTime.Now, Response = response, Duration = stopWatch.ElapsedMilliseconds }; } catch (Exception ex) { Log.Error(ex, MethodBase.GetCurrentMethod().Name); } } if (AppSettings.FileLogging) { } await _requestDelegate(context); }
private async Task SetHttpRequest(HttpContext context, HttpLog log) { HttpRequest _request = context.Request; if (_request == null) { return; } log.RequestMethod = _request.Method; log.RequestScheme = _request.Scheme; log.RequestPath = _request.Path; log.RequestQueryString = _request.QueryString.ToString(); if (_request.ContentType != null) { log.RequestContentType = _request.ContentType; } log.RequestContentLength = _request.ContentLength; log.RequestHost = _request.Host.Value; if (this.CaptureRequestBody(_request.ContentType) == true) { _request.EnableBuffering(); _request.Body.Position = 0; log.RequestBody = await new StreamReader(_request.Body).ReadToEndAsync(); _request.Body.Position = 0; } log.Operation = $"Http#{context.GetRouteValue( "controller" )}#{context.GetRouteValue( "action" )}"; if (log.Operation == "Http##") { log.Operation = "Http#" + log.RequestPath; } var heads = JsonHelper.ToJson2(context.Request.Headers.ToDictionary(h => h.Key, h => h.Value.ToString())); log.RequestHead = heads; }
public async Task <int> InsertAsync(HttpLog model, int timeoutMS) { var startTime = DateTime.UtcNow; try { using (var timeoutCancellationTokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(timeoutMS))) { await _mongoDBContext.HttpLogs.InsertOneAsync(model, cancellationToken : timeoutCancellationTokenSource.Token); } return(1); } catch (OperationCanceledException ex) { await _exceptionService.InsertAsync(ex, "Domain.Application.ServicesHttpLogService.Insert", ""); var elapsed = DateTime.UtcNow - startTime; return(0); } }
private async Task SetIdentity(HttpContext context, HttpLog log) { await Task.Run(() => { if (context.User == null) { return; } //Identity log.IdentityIsAuthenticated = context.User.Identity.IsAuthenticated; log.IdentityName = context.User.Identity.Name; try { if (string.IsNullOrEmpty(log.IdentityName) && context.Request.Headers.ContainsKey("Authorization")) { if (!context.Request.Headers.TryGetValue("Authorization", out Microsoft.Extensions.Primitives.StringValues auth)) { return; } var token = auth.ToString().Replace("Bearer", "").Trim(); var principal = _tokenService.GetPrincipalFromExpiredToken(token, out bool exp); var deviceId = principal.Claims.FirstOrDefault(claim => claim.Type == "deviceId"); var deviceUuid = principal.Claims.FirstOrDefault(claim => claim.Type == "deviceUuid"); var username = principal.Claims.FirstOrDefault(claim => claim.Type == "username"); log.IdentityIsAuthenticated = principal.Identity.IsAuthenticated && !exp; log.IdentityName = username != null ? username.Value : (deviceUuid != null ? deviceUuid.Value : (deviceId != null ? deviceId.Value : "")); } } catch (Exception ex) { ; } }); }
protected void Deserialize(WebData_v2 <TSource, TData> webData, BsonDocument document) { webData.Data = _dataSerializer.Deserialize(document); string itemName = _webRequestSerializer.ItemName; if (itemName == null) { throw new PBException("web request serializer item name is not defined"); } if (!document.Contains(itemName)) { throw new PBException($"deserialize web request : serializer document does'nt contain element \"{itemName}\""); } var element = document[itemName]; if (element == null) { throw new PBException($"deserialize web request : element \"{itemName}\" is null"); } if (!(element is BsonDocument)) { throw new PBException($"deserialize web request : element \"{itemName}\" is not a document"); } BsonDocument httpDocument = element as BsonDocument; foreach (BsonElement httpElement in httpDocument) { if (!(httpElement.Value is BsonDocument)) { throw new PBException($"deserialize web request : element \"{httpElement.Name}\" is not a document"); } HttpLog httpLog = _webRequestSerializer.DeserializeData((BsonDocument)httpElement.Value); webData.HttpResults.Add(httpElement.Name, new HttpResult <string> { Http = new Http_v2(httpLog) }); } }
private async Task SetConnectionInfo(HttpContext context, HttpLog log) { await Task.Run(() => { ConnectionInfo _connection = context.Connection; if (_connection == null) { return; } //ConnectionInfo if (_connection.RemoteIpAddress != null) { log.RemoteIpAddress = _connection.RemoteIpAddress.ToString(); } if (_connection.RemotePort != 0) { log.RemotePort = _connection.RemotePort; } }); }
public Task <HttpResponseMessage> LogAsync( HttpRequestMessage request, CancellationToken token, Func <HttpRequestMessage, CancellationToken, Task <HttpResponseMessage> > sendAsync) { Stopwatch watch = Stopwatch.StartNew(); return(sendAsync(request, token).ContinueWith( t => { HttpResponseMessage response = t.Result; watch.Stop(); var log = new HttpLog( request.RequestUri, request.Method.Method, response.StatusCode, watch.Elapsed); logger.Log(log); return response; }, token, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.Current)); }
public override void OnActionExecuting(HttpActionContext actionContext) { HttpLog log = GetLog(actionContext); ApiEventSource.Log.GetBegin(log.UserName, log.Uri.AbsoluteUri); }
public async Task Log(HttpResponseMessage resp) { var httpLog = await HttpLog.FromHttpResponse(resp); _Log.Information("User: {@User}; ApiCall: {@ApiCall}", _UserLog, httpLog); }
public Http_v2(HttpLog httpLog) : base(httpLog) { }
private async Task <T> SendWebRequestAsync <T>(string httpMethod, string absoluteUrl, object request, CancellationToken token, bool recall = false) { if (httpMethod == null) { throw new ArgumentNullException(nameof(httpMethod)); } this.PopulateRequestMetadata(request); var requestUri = absoluteUrl; var hasQueryString = request != null && !HttpUtils.HasRequestBody(httpMethod); if (hasQueryString) { var queryString = QueryStringSerializer.SerializeToString(request); if (!string.IsNullOrEmpty(queryString)) { requestUri += "?" + queryString; } } var webReq = this.CreateHttpWebRequest(requestUri); if (webReq != null && Proxy != null) { webReq.Proxy = Proxy; } var timedOut = false; ITimer timer = null; timer = PclExportClient.Instance.CreateTimer(state => { timedOut = true; webReq?.Abort(); webReq = null; timer?.Cancel(); timer = null; }, this.Timeout.GetValueOrDefault(DefaultTimeout), this); Exception ResolveException(Exception ex) { if (token.IsCancellationRequested) { return(new OperationCanceledException(token)); } if (timedOut) { return(PclExportClient.Instance.CreateTimeoutException(ex, "The request timed out")); } return(ex); } bool returningWebResponse = false; HttpWebResponse webRes = null; T Complete(T response) { timer.Cancel(); PclExportClient.Instance.SynchronizeCookies(this); ResultsFilterResponse?.Invoke(webRes, response, httpMethod, absoluteUrl, request); return(response); } webReq.Accept = ContentType; if (this.EmulateHttpViaPost) { webReq.Method = "POST"; webReq.Headers[HttpHeaders.XHttpMethodOverride] = httpMethod; } else { webReq.Method = httpMethod; } PclExportClient.Instance.AddHeader(webReq, Headers); PclExport.Instance.Config(webReq, userAgent: UserAgent); if (this.authInfo != null && !string.IsNullOrEmpty(this.UserName)) { webReq.AddAuthInfo(this.UserName, this.Password, authInfo); } else if (this.BearerToken != null) { webReq.Headers[HttpHeaders.Authorization] = "Bearer " + this.BearerToken; } else if (this.Credentials != null) { webReq.Credentials = this.Credentials; } else if (this.AlwaysSendBasicAuthHeader) { webReq.AddBasicAuth(this.UserName, this.Password); } if (!DisableAutoCompression) { PclExport.Instance.AddCompression(webReq); } ApplyWebRequestFilters(webReq); try { if (HttpUtils.HasRequestBody(webReq.Method)) { webReq.ContentType = ContentType; if (RequestCompressionType != null) { webReq.Headers[HttpHeaders.ContentEncoding] = RequestCompressionType; } if (HttpLog != null) { webReq.AppendHttpRequestHeaders(HttpLog, new Uri(BaseUri)); } using var requestStream = await webReq.GetRequestStreamAsync().ConfigAwait(); token.ThrowIfCancellationRequested(); if (request != null) { StreamSerializer(null, request, requestStream); } } else { if (HttpLog != null) { webReq.AppendHttpRequestHeaders(HttpLog, new Uri(BaseUri)); } } HttpLog?.AppendLine(); } catch (Exception ex) { if (Log.IsDebugEnabled) { Log.Debug($"Error Sending Request: {ex.Message}", ex); } throw HandleResponseError <T>(ResolveException(ex), requestUri, request); } try { webRes = (HttpWebResponse)await webReq.GetResponseAsync().ConfigAwait(); { token.ThrowIfCancellationRequested(); ApplyWebResponseFilters(webRes); returningWebResponse = typeof(T) == typeof(HttpWebResponse); if (returningWebResponse) { return(Complete((T)(object)webRes)); } var responseStream = webRes.ResponseStream(); var responseBodyLength = webRes.ContentLength; var bufferRead = new byte[BufferSize]; var totalRead = 0; int read; var ms = new MemoryStream(); // can get returned, do not dispose while ((read = await responseStream.ReadAsync(bufferRead, 0, bufferRead.Length, token).ConfigAwait()) != 0) { await ms.WriteAsync(bufferRead, 0, read, token).ConfigAwait(); totalRead += read; OnDownloadProgress?.Invoke(totalRead, responseBodyLength); } try { ms.Position = 0; if (HttpLog != null) { webRes.AppendHttpResponseHeaders(HttpLog); if (webRes.ContentLength != 0 && webRes.StatusCode != HttpStatusCode.NoContent) { var isBinary = typeof(T) == typeof(Stream) || typeof(T) == typeof(byte[]) || ContentType.IsBinary(); if (isBinary) { HttpLog.Append("(base64) "); HttpLog.AppendLine(Convert.ToBase64String(ms.ReadFully())); } else { HttpLog.AppendLine(ms.ReadToEnd()); } HttpLog.AppendLine().AppendLine(); ms.Position = 0; } } if (typeof(T) == typeof(Stream)) { return(Complete((T)(object)ms)); } else { var stream = ms; try { if (typeof(T) == typeof(string)) { return(Complete((T)(object)await stream.ReadToEndAsync().ConfigAwait())); } else if (typeof(T) == typeof(byte[])) { return(Complete((T)(object)stream.ToArray())); } else { return(Complete((T)this.StreamDeserializer(typeof(T), stream))); } } finally { if (stream.CanRead) { stream.Dispose(); // Not yet disposed, but could've been. } } } } catch (Exception ex) { if (Log.IsDebugEnabled) { Log.Debug($"Error Reading Response Error: {ex.Message}", ex); } throw; } finally { if (HttpLog != null) { HttpLogFilter?.Invoke(HttpLog); } responseStream.Close(); } } } catch (Exception ex) { var webEx = ex as WebException; var firstCall = !recall; var hasRefreshTokenCookie = this.CookieContainer.GetRefreshTokenCookie(BaseUri) != null; var hasRefreshToken = RefreshToken != null || hasRefreshTokenCookie; if (firstCall && WebRequestUtils.ShouldAuthenticate(webEx, (!string.IsNullOrEmpty(UserName) && !string.IsNullOrEmpty(Password)) || Credentials != null || BearerToken != null || hasRefreshToken || OnAuthenticationRequired != null)) { try { if (EnableAutoRefreshToken && hasRefreshToken) { var refreshRequest = new GetAccessToken { RefreshToken = hasRefreshTokenCookie ? null : RefreshToken, }; var uri = this.RefreshTokenUri ?? this.BaseUri.CombineWith(refreshRequest.ToPostUrl()); this.BearerToken = null; this.CookieContainer?.DeleteCookie(new Uri(BaseUri), "ss-tok"); GetAccessTokenResponse tokenResponse; try { var httpReq = WebRequest.CreateHttp(uri); tokenResponse = (await ServiceClientBase.SendStringToUrlAsync(httpReq, method: HttpMethods.Post, requestFilter: req => { if (hasRefreshTokenCookie) { req.CookieContainer = CookieContainer; } }, requestBody: refreshRequest.ToJson(), accept: MimeTypes.Json, contentType: MimeTypes.Json, token: token) .ConfigAwait()).FromJson <GetAccessTokenResponse>(); } catch (WebException refreshEx) { var webServiceEx = ServiceClientBase.ToWebServiceException(refreshEx, stream => StreamDeserializer(typeof(T), stream), ContentType); if (webServiceEx != null) { throw new RefreshTokenException(webServiceEx); } throw new RefreshTokenException(refreshEx.Message, refreshEx); } var accessToken = tokenResponse?.AccessToken; var refreshClient = webReq = (HttpWebRequest)WebRequest.Create(requestUri); var tokenCookie = this.CookieContainer.GetTokenCookie(BaseUri); if (!string.IsNullOrEmpty(accessToken)) { refreshClient.AddBearerToken(this.BearerToken = accessToken); } else if (tokenCookie != null) { refreshClient.CookieContainer = CookieContainer; refreshClient.CookieContainer.SetTokenCookie(BaseUri, tokenCookie); } else { throw new RefreshTokenException("Could not retrieve new AccessToken from: " + uri); } return(await SendWebRequestAsync <T>(httpMethod, absoluteUrl, request, token, recall : true).ConfigAwait()); } OnAuthenticationRequired?.Invoke(); var newReq = (HttpWebRequest)WebRequest.Create(requestUri); if (StoreCookies) { newReq.CookieContainer = CookieContainer; } HandleAuthException(ex, webReq); return(await SendWebRequestAsync <T>(httpMethod, absoluteUrl, request, token, recall : true).ConfigAwait()); } catch (WebServiceException) { throw; } catch (Exception /*subEx*/) { throw HandleResponseError <T>(ResolveException(ex), requestUri, request); } } if (ExceptionFilter != null && webEx?.Response != null) { var cachedResponse = ExceptionFilter(webEx, webEx.Response, requestUri, typeof(T)); if (cachedResponse is T variable) { return(variable); } } throw HandleResponseError <T>(ResolveException(ex), requestUri, request); } finally { if (!returningWebResponse) { webRes?.Dispose(); } } }
private static void WriteResponse(TextWriter writer, string message) { HttpLog.WriteHttpOut(message); writer.WriteLine(message); }