示例#1
0
 public ErrorLogViewModel( ErrorLog ErrorLog )
 {
     // FRAGILE: ASSUME: we've copied all the properties
     this.ErrorLogId = ErrorLog.ErrorLogId;
     this.CreatedDate = ErrorLog.CreatedDate;
     this.Message = ErrorLog.Message;
     this.ExceptionDetails = ErrorLog.ExceptionDetails;
     this.UserId = ErrorLog.UserId;
     this.User = ErrorLog.User;
     this.ClientIp = ErrorLog.ClientIp;
     this.HttpMethod = ErrorLog.HttpMethod;
     this.PageUrl = ErrorLog.PageUrl;
     this.ReferrerUrl = ErrorLog.ReferrerUrl;
     this.UserAgent = ErrorLog.UserAgent;
 }
示例#2
0
        public void SendErrorEmail(ErrorLog ErrorLog)
        {
            if (!this.settingRepository.SendErrorEmails) {
                return;
            }
            #if DEBUG
            if (HostingEnvironment.IsHosted && HttpContext.Current != null && HttpContext.Current.Request.IsLocal) {
                return; // No need to email locally run requests
            }
            if (Debugger.IsAttached) {
                return; // No need to email when debugging
            }
            #endif

            string br = "<br />";

            string subject = ErrorLog.Message;
            if (string.IsNullOrWhiteSpace(subject)) {
                subject = ErrorLog.ExceptionDetails;
            }
            subject = subject.TrimToLength(40);

            MailMessage mail = new MailMessage();
            mail.To.Add(this.settingRepository.ErrorEmailAddress);
            mail.From = new MailAddress(this.settingRepository.ErrorEmailAddress);
            mail.Subject = String.Format("Error {0}: {1}", ErrorLog.ErrorLogId, subject);

            StringBuilder sb = new StringBuilder();
            sb.Append("<b>Date</b>: " + ErrorLog.CreatedDate);
            sb.Append(string.Format("<b>ErrorLogId</b>: <a href=\"{0}/ErrorLog/Detail/{1}\">{1}</a>{2}", this.settingRepository.SiteUrl, ErrorLog.ErrorLogId, br));
            sb.Append("<b>PageUrl</b>: " + HttpUtility.HtmlEncode(ErrorLog.PageUrl ?? "") + br + br);
            sb.Append("<b>User</b>: " + HttpUtility.HtmlEncode(ErrorLog.UserId) + br + br);
            sb.Append("<b>UserAgent</b>: " + HttpUtility.HtmlEncode(ErrorLog.UserAgent ?? "") + br + br);
            sb.Append("<b>Message</b>: " + HttpUtility.HtmlEncode(ErrorLog.Message ?? "") + br + br);
            sb.Append("<b>Exception</b>: " + HttpUtility.HtmlEncode(ErrorLog.ExceptionDetails ?? "").Replace("\n", "<br />"));

            mail.Body = sb.ToString();
            mail.IsBodyHtml = true;

            using (SmtpClient client = new SmtpClient()) {
                try {
                    client.Send(mail);
                } catch {
                    // Don't error trying to error
                }
            }
        }
示例#3
0
        private int? PrivateLog( string Message, Exception ex = null, string RequestUrlOverride = null )
        {
            ErrorLog err = new ErrorLog {
                Message = Message
            };

            // Date, UserId, etc are inherint in IEntity and Repository<T>

            if (HostingEnvironment.IsHosted) {
                HttpContext context = HttpContext.Current;
                if (context != null && context.Request != null) {
                    err.HttpMethod = context.Request.HttpMethod;
                    err.PageUrl = context.Request.Url.PathAndQuery; // .OriginalString;
                    err.UserAgent = context.Request.UserAgent;
                    err.ClientIp = context.Request.UserHostAddress;
                    if (context.Request.UrlReferrer != null) {
                        err.ReferrerUrl = context.Request.UrlReferrer.OriginalString;
                    }
                }
                if (context != null && context.User != null) {
                    err.UserId = null; // TODO: get the current user id, e.g. this.userCurrentService.CurrentUserId;
                }
            }

            if (!string.IsNullOrEmpty(RequestUrlOverride)) {
                err.PageUrl = RequestUrlOverride;
            }

            if (ex != null) {
                ExceptionInfo exInfo = this.GetExceptionInfo(ex);
                err.ExceptionDetails = JsonConvert.SerializeObject(exInfo);
            }

            // TODO: try/catch around each, perhaps the other will succeed
            this.errorLogRepository.Save(err);
            this.logErrorEmailer.SendErrorEmail(err);

            if (ex is ThreadAbortException) {
                throw ex; // Can't just "throw" because we aren't necessarily inside a catch
                // FRAGILE: you'll lose the original stack trace
            }

            return err.ErrorLogId;
        }