public async Task AddToDatabaseAsync(WebTraffic traffic) { await using var db = GetContext(); await db.Traffics.AddAsync(traffic); await db.SaveChangesAsync(); }
internal async Task Run(HttpContext context, Func <Task> next) { var identity = context.UserIdentity(); //Pass the command to the next task in the pipeline await next.Invoke(); _logger?.LogInformation("Requested Server Analytics"); //This request should be filtered out ? if (_exclude?.Any(x => x(context)) ?? false) { return; } var geoData = await _geoDataExtractor.RetrieveGeographicDataAsync(context.Connection.RemoteIpAddress); var traffic = new WebTraffic(geoData) { RemoteIpAddress = context.Connection.RemoteIpAddress.MapToIPv4().ToString(), Identity = identity, Method = context.Request.Method, UserAgent = context.Request.Headers["User-Agent"], Path = context.Request.Path.Value, }; _logger?.LogInformation($"Connected client ip: {traffic.RemoteIpAddress}"); await _repository.AddToDatabaseAsync(traffic); _logger?.LogInformation("Saved web request to database"); }