private async Task LogRequest(HttpContext context, APILogEntity logEntity) { string body = string.Empty; //using (var buffer = new MemoryStream()) //{ // context.Request.Body.CopyTo(buffer); // context.Request.Body.Seek(0, SeekOrigin.Begin); // using (var bodyReader = new StreamReader(buffer)) // { // body = await bodyReader.ReadToEndAsync(); // } //} logEntity.Application = "SnowApp"; logEntity.User = _userResolver.GetEmail(); logEntity.Machine = Environment.MachineName; logEntity.RequestContentType = context.Request.ContentType; logEntity.RequestContentBody = body; logEntity.RequestIpAddress = context.Connection.RemoteIpAddress.ToString(); logEntity.RequestMethod = context.Request.Method; logEntity.RequestHeaders = SerializeHeaders(context.Request.Headers); logEntity.RequestTimestamp = DateTime.Now; logEntity.RequestUri = System.Uri.UnescapeDataString(context.Request.Scheme + "://" + context.Request.Host.Value + context.Request.Path + context.Request.QueryString.ToUriComponent()); }
public void Add(APILogEntity entity) { EntityEntry dbEntityEntry = _context.Entry <APILogEntity>(entity); _context.Logs.Add(entity); _context.SaveChanges(); }
private async Task LogResponseAndInvokeNext(HttpContext context, APILogEntity logEntity) { using (var buffer = new MemoryStream()) { //replace the context response with our buffer var stream = context.Response.Body; context.Response.Body = buffer; //invoke the rest of the pipeline await _next(context); //reset the buffer and read out the contents buffer.Seek(0, SeekOrigin.Begin); var reader = new StreamReader(buffer); using (var bufferReader = new StreamReader(buffer)) { string body = await bufferReader.ReadToEndAsync(); //reset to start of stream buffer.Seek(0, SeekOrigin.Begin); //copy our content to the original stream and put it back await buffer.CopyToAsync(stream); context.Response.Body = stream; if (context.Response.ContentType != null && context.Response.ContentType.Contains("application/json")) { logEntity.ResponseContentBody = body; } else { logEntity.ResponseContentBody = "... NOT A JSON ..."; } logEntity.ResponseContentType = context.Response.ContentType; logEntity.ResponseHeaders = SerializeHeaders(context.Response.Headers); logEntity.ResponseStatusCode = (int)context.Response.StatusCode; logEntity.ResponseTimestamp = DateTime.Now; } } }
private async Task <APILogEntity> GetRequestInfo(HttpContext context) { string requestBody = string.Empty; using (var bodyReader = new StreamReader(context.Request.Body)) { requestBody = await bodyReader.ReadToEndAsync(); } APILogEntity logEntity = new APILogEntity(); logEntity.Application = "SnowApp"; logEntity.User = _userResolver.GetEmail(); logEntity.Machine = Environment.MachineName; logEntity.RequestContentType = context.Request.ContentType; logEntity.RequestContentBody = requestBody; logEntity.RequestIpAddress = context.Connection.RemoteIpAddress.ToString(); logEntity.RequestMethod = context.Request.Method; logEntity.RequestHeaders = SerializeHeaders(context.Request.Headers); logEntity.RequestTimestamp = DateTime.Now; logEntity.RequestUri = System.Uri.UnescapeDataString(context.Request.Scheme + "://" + context.Request.Host.Value + context.Request.Path + context.Request.QueryString.ToUriComponent()); return(logEntity); }
public async Task Invoke(HttpContext context) { if (context.Request.Path.HasValue && context.Request.Path.Value.Contains("/api/")) { APILogEntity logEntity = new APILogEntity(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); await LogRequest(context, logEntity); await LogResponseAndInvokeNext(context, logEntity); stopwatch.Stop(); logEntity.ResponseTime = stopwatch.Elapsed; _logService.SaveLog(logEntity); } else { await _next(context); } }
public void SaveLog(APILogEntity log) { _logRepository.Add(log); }