public async Task Invoke(HttpContext context, LogServiceRepository logServiceRepository) { int?userId = null; LogServiceViewModel logModel = new LogServiceViewModel(); logModel.RelativePath = context.Request.Path.Value.ToLower(); logModel.Method = context.Request.Method; logModel.QueryString = context.Request.QueryString.Value; logModel.RequestContentLength = context.Request.ContentLength; Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); await Next(context); stopwatch.Stop(); logModel.Elapsed = stopwatch.Elapsed; logModel.ResponseContentLength = context.Response.ContentLength; logModel.StatusCode = context.Response.StatusCode; logModel.RequestIp = context.Connection.RemoteIpAddress.ToString(); if (context.User.Identity.IsAuthenticated) { userId = context.User.GetUserId(); } logModel.UserId = userId; logServiceRepository.Log(logModel); }
public void Log(LogServiceViewModel model) { var entity = new LogService { Method = model.Method, QueryString = model.QueryString, RelativePath = model.RelativePath, RequestContentLength = model.RequestContentLength, ResponseContentLength = model.ResponseContentLength, RequestIp = model.RequestIp, StatusCode = model.StatusCode, UserId = model.UserId, Elapsed = model.Elapsed, }; _context.LogServices.Add(entity); _context.SaveChanges(); }