예제 #1
0
        internal static async Task LogExceptionAsync(this IDiagnosticsDbContext db, Exception e, HttpContext?http, IPrincipalUser?principal)
        {
            principal ??= http?.RequestServices?.GetService <IPrincipalUser>();
            http ??= principal?.ServiceProvider?.GetService <IHttpContextAccessor>()?.HttpContext;

            var log = new ExceptionLog {
                ExceptionType = e.GetType().Name,
                Message       = e.Message,
                Source        = e.Source,
                StackTrace    = e.StackTrace,
            };

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

            var repeating = db.ExceptionLogs
                            .OrderByDescending(x => x.Id)
                            .Where(x => x.Md5Comparison == log.Md5Comparison)
                            .FirstOrDefault();

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