Exemplo n.º 1
0
        internal static async Task LogOperationAsync(this IDiagnosticsDbContext db, LogLevel logLevel, string message, IPrincipalUser?principal)
        {
            var log = new OperationLog {
                LogLevel = logLevel,
                Message  = message
            };

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

            var keyValueManager = principal?.ServiceProvider?.GetService <IKeyValueManager>();
            var seconds         = keyValueManager?.LogOperationAsRepeatedIfSameWithinSeconds() ?? 60;

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

            if (repeating == null)
            {
                db.OperationLogs.Add(log);
            }
            else
            {
                repeating.Repeated++;
                repeating.LastTime = DateTime.Now;
            }
            await db.Normalize().SaveChangesAsync();
        }
Exemplo n.º 2
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();
        }
Exemplo n.º 3
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();
        }
Exemplo n.º 4
0
 public DiagnosticsLogger(IDiagnosticsDbContext db, IHttpContextAccessor httpContextAccessor, IPrincipalUser?principal)
 {
     _me   = principal;
     _db   = db;
     _http = httpContextAccessor;
 }
Exemplo n.º 5
0
 public IndexPageModel(IPrincipalUser principal, IDiagnosticsDbContext db)
 {
     _me = principal;
     _db = db;
 }