public async Task <IActionResult> Create([Bind("Id,LogDate,TimeSpent,LogDescription,JobTaskId,DoneById")] LogDTO logDTO) { if (ModelState.IsValid) { await _logManager.CreateAsync(logDTO); return(RedirectToAction(nameof(Index))); } ViewData["JobTaskId"] = new SelectList(_context.JobTasks, "Id", "TaskName", logDTO.JobTaskId); ViewData["DoneById"] = new SelectList(_context.Users, "Id", "UserName", logDTO.DoneById); return(View(logDTO)); }
public async Task Invoke(HttpContext context, ILogManager logManager) { var userId = (context.User.FindFirst(ClaimTypes.NameIdentifier)?.Value).ToNullableInt(); var requestBodyStream = new MemoryStream(); var originalRequestBody = context.Request.Body; await context.Request.Body.CopyToAsync(requestBodyStream); requestBodyStream.Seek(0, SeekOrigin.Begin); var url = UriHelper.GetDisplayUrl(context.Request); var requestBodyText = new StreamReader(requestBodyStream).ReadToEnd(); var log = new Log { LoggedUserId = userId, RequestBody = requestBodyText, RequestMethod = context.Request.Method, RequestUrl = url, RequestTime = DateTime.Now, }; requestBodyStream.Seek(0, SeekOrigin.Begin); context.Request.Body = requestBodyStream; var bodyStream = context.Response.Body; var responseBodyStream = new MemoryStream(); context.Response.Body = responseBodyStream; await _next(context); responseBodyStream.Seek(0, SeekOrigin.Begin); var responseBody = new StreamReader(responseBodyStream).ReadToEnd(); var responseLogText = $"RESPONSE BODY: {responseBody}"; responseBodyStream.Seek(0, SeekOrigin.Begin); await responseBodyStream.CopyToAsync(bodyStream); log.ResponseBody = responseLogText; log.ResponseTime = DateTime.Now; await logManager.CreateAsync(log); }