Esempio n. 1
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            if (!IsEnabled(logLevel))
            {
                return;
            }
            try
            {
                var httpContextAccessor = ServiceLocator.Resolve <IHttpContextAccessor>();
                var httpContext         = httpContextAccessor?.HttpContext;

                // Nothing to log
                if (httpContext == null)
                {
                    return;
                }

                var error = new Error();

                error.Message     = exception?.Message ?? "No message";
                error.Exception   = exception?.ToString();
                error.UserAgent   = httpContext.Request.Headers["User-Agent"];
                error.IpAddress   = httpContext.Connection?.LocalIpAddress?.ToString();
                error.Url         = httpContext.Request.Path;
                error.HttpReferer = httpContext.Request.Headers["Referer"];

                error.CreatedOn = error.ChangedOn = error.ErrorDate = DateTime.Now;
                try
                {
                    var userId = (httpContext.RequestServices.GetService(typeof(ICurrentUser)) as CurrentUser).Id;
                    if (userId != 0)
                    {
                        error.UserId = error.CreatedBy = error.ChangedBy = userId;
                    }
                }
                catch { }

                // Extract connectionstring from potentially unstable dbcontext.
                var unstable         = ServiceLocator.Resolve <JelloContext>();
                var connectionString = unstable.Database.GetDbConnection().ConnectionString;

                // Use brand new context
                using (var db = new ErrorContext(connectionString))
                {
                    db.Error.Add(error);
                    db.SaveChanges();
                }
            }
            catch
            {
                /* Possibly notify by email about logging error */
            }
        }
Esempio n. 2
0
 public void ReportError(Ticket ticket)
 {
     errorContext.Tickets.Add(ticket);
     errorContext.SaveChanges();
 }