Exemple #1
0
        internal static async Task LogRequestAsync(this IDiagnosticsDbContext db, HttpContext?http, IPrincipalUser?principal)
        {
            if (http == null)
            {
                return;
            }

            principal ??= http.RequestServices.GetService <IPrincipalUser>();

            var log = new RequestLog();

            if (principal != null)
            {
                log.ReadValuesFromPrincipal(principal);
            }
            log.ReadValuesFromHttpContext(http);
            log.ComputeMd5Comparison();

            var keyValueManager = http.RequestServices.GetService <IKeyValueManager>();
            var seconds         = keyValueManager?.LogRequestAsRepeatedIfSameWithinSeconds() ?? 60;

            var repeating = db.RequestLogs
                            .OrderByDescending(x => x.Id)
                            .Where(x => x.Md5Comparison == log.Md5Comparison)
                            .Where(x => x.LastTime > DateTime.Now.AddSeconds(-seconds))
                            .FirstOrDefault();

            if (repeating == null)
            {
                db.RequestLogs.Add(log);
            }
            else
            {
                repeating.Repeated++;
                repeating.LastTime = DateTime.Now;
            }
            await db.Normalize().SaveChangesAsync();
        }