예제 #1
0
        public int SubmitLocalErrorLog(LocalErrorLog errorLog, string authToken)
        {
            Authentication auth = new Authentication();

            if (auth.IsValidKey(authToken) == true)
            {
                OsbideUser authUser = GetActiveUser(authToken);
                LogUserTransaction(authUser);

                //replace the error log's sender information with what we obtained from the auth key
                errorLog.Sender   = null;
                errorLog.SenderId = authUser.Id;

                //add to the db and give it a try
                Db.LocalErrorLogs.Add(errorLog);
                try
                {
                    Db.SaveChanges();
                }
                catch (Exception)
                {
                    return((int)Enums.ServiceCode.DatabaseError);
                }
                return(errorLog.Id);
            }
            else
            {
                return((int)Enums.ServiceCode.AuthenticationError);
            }
        }
예제 #2
0
        public int SubmitLocalErrorLog(LocalErrorLog errorLog)
        {
            //check to see if the user exists in the database.  If not, reset sender and try again
            //(next IF statement)
            if (errorLog.SenderId != 0)
            {
                OsbideUser userCheck = Db.Users.Find(errorLog.SenderId);
                if (userCheck == null)
                {
                    errorLog.SenderId = 0;
                }
            }

            //reset the sender if necessary
            if (errorLog.SenderId == 0)
            {
                errorLog.Sender   = SaveUser(errorLog.Sender);
                errorLog.SenderId = errorLog.Sender.Id;
            }
            errorLog.Sender = null;
            Db.LocalErrorLogs.Add(errorLog);
            try
            {
                Db.SaveChanges();
            }
            catch (Exception)
            {
                return((int)Enums.ServiceCode.Error);
            }
            return(errorLog.Id);
        }
예제 #3
0
        private void SendLocalErrorLogs()
        {
            var dataRoot     = StringConstants.DataRoot;
            var logExtension = StringConstants.LocalErrorLogExtension;
            var today        = StringConstants.LocalErrorLogFileName;

            //find all log files
            var files = Directory.GetFiles(dataRoot);

            foreach (var file in files)
            {
                if (Path.GetExtension(file) != logExtension ||
                    Path.GetFileNameWithoutExtension(file) == today)
                {
                    continue;
                }

                var  log = LocalErrorLog.FromFile(file);
                bool result;
                lock (_cache)
                {
                    var webServiceKey = _cache[StringConstants.AuthenticationCacheKey] as string;

                    var task = AsyncServiceClient.SubmitLocalErrorLog(new LocalErrorLogRequest {
                        Log = log, AuthToken = webServiceKey
                    });
                    result = task.Result;
                }

                //remove if file successfully sent
                if (!result)
                {
                    continue;
                }
                try
                {
                    File.Delete(file);
                }
                catch (Exception)
                {
                    // ignored
                }
            }
        }
예제 #4
0
 public static int SubmitLocalErrorLog(LocalErrorLog errorLog)
 {
     try
     {
         //execute sql batch insert statements
         using (var connection = new SqlConnection(StringConstants.ConnectionString))
         {
             using (var cmd = errorLog.GetInsertCommand())
             {
                 cmd.Connection = connection;
                 connection.Open();
                 var errLogId = Convert.ToInt32(cmd.ExecuteScalar());
                 connection.Close();
                 return(errLogId);
             }
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
예제 #5
0
        private void SendLocalErrorLogs()
        {
            string dataRoot = StringConstants.DataRoot;
            string logExtension = StringConstants.LocalErrorLogExtension;
            string today = StringConstants.LocalErrorLogFileName;

            //find all log files
            string[] files = Directory.GetFiles(dataRoot);
            foreach (string file in files)
            {
                if (Path.GetExtension(file) == logExtension)
                {
                    //ignore today's log
                    if (Path.GetFileNameWithoutExtension(file) != today)
                    {
                        LocalErrorLog log = LocalErrorLog.FromFile(file);
                        int result = 0;
                        lock (_cache)
                        {
                            string webServiceKey = _cache[StringConstants.AuthenticationCacheKey] as string;
                            result = _webServiceClient.SubmitLocalErrorLog(log, webServiceKey);
                        }

                        //remove if file successfully sent
                        if (result != -1)
                        {
                            try
                            {
                                File.Delete(file);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
            }
        }
예제 #6
0
        public EventLog SubmitLog(EventLog log, OsbideUser user)
        {
            LocalErrorLog errorLogger = new LocalErrorLog();

            errorLogger.SenderId = user.Id;
            errorLogger.Content  = "About to save log " + log.LogType + " to DB.  ";
            errorLogger.LogDate  = DateTime.Now;

            log.Sender       = null;
            log.SenderId     = user.Id;
            log.DateReceived = DateTime.UtcNow;
            log.EventTypeId  = Convert.ToInt32(Enum.Parse(typeof(EventTypes), log.LogType));

            //insert into the DB
            Db.EventLogs.Add(log);
            try
            {
                Db.SaveChanges();
                errorLogger.Content += "Item saved.  ";
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                errorLogger.Content += "Error saving:  ";
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        errorLogger.Content += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
                Db.LocalErrorLogs.Add(errorLogger);
                Db.SaveChanges();
                return(null);
            }
            catch (Exception ex)
            {
                errorLogger.Content += "Error saving: " + ex.Message;
                System.Diagnostics.Trace.TraceInformation(ex.Message);
                Db.LocalErrorLogs.Add(errorLogger);
                Db.SaveChanges();
                return(null);
            }

            //Tease apart log information and insert into the appropriate DB table
            IOsbideEvent evt = null;

            try
            {
                errorLogger.Content += "About to unzip event.";
                evt            = EventFactory.FromZippedBinary(log.Data.BinaryData, new OsbideDeserializationBinder());
                evt.EventLogId = log.Id;
            }
            catch (Exception)
            {
                errorLogger.Content += "Error unzipping event.";
                Db.LocalErrorLogs.Add(errorLogger);
                Db.SaveChanges();
                return(null);
            }

            var hashtags = string.Empty;
            var usertags = string.Empty;

            if (log.LogType == AskForHelpEvent.Name)
            {
                Db.AskForHelpEvents.Add((AskForHelpEvent)evt);
                AskForHelpEvent ask = evt as AskForHelpEvent;

                //send email to interested parties
                //find all of this user's subscribers and send them an email
                List <OsbideUser> observers = new List <OsbideUser>();

                observers = (from subscription in Db.UserSubscriptions
                             join dbUser in Db.Users on
                             new { InstitutionId = subscription.ObserverInstitutionId, SchoolId = subscription.ObserverSchoolId }
                             equals new { InstitutionId = user.InstitutionId, SchoolId = user.SchoolId }
                             where subscription.SubjectSchoolId == user.SchoolId &&
                             subscription.SubjectInstitutionId == user.InstitutionId &&
                             dbUser.ReceiveEmailOnNewFeedPost == true
                             select dbUser).ToList();
                if (observers.Count > 0)
                {
                    string url  = StringConstants.GetActivityFeedDetailsUrl(log.Id);
                    string body = "Greetings,<br />{0} asked for help regarding the following item:<br />\"{1}\"<br />To view this "
                                  + "conversation online, please visit {2} or visit your OSBIDE user profile.<br /><br />Thanks,<br />OSBIDE<br /><br />"
                                  + "These automated messages can be turned off by editing your user profile.";
                    body = string.Format(body, user.FirstAndLastName, ask.UserComment, url);
                    List <MailAddress> to = new List <MailAddress>();
                    foreach (OsbideUser observer in observers)
                    {
                        to.Add(new MailAddress(observer.Email));
                    }
                    Email.Send("[OSBIDE] Someone has asked for help!", body, to);
                }
            }
            else if (log.LogType == BuildEvent.Name)
            {
                BuildEvent build = (BuildEvent)evt;
                Db.BuildEvents.Add(build);

                string pattern = "error ([^:]+)";

                //strip out non-critical errors
                List <BuildEventErrorListItem> errorItems = build.ErrorItems.Where(e => e.ErrorListItem.CriticalErrorName.Length > 0).ToList();
                build.ErrorItems.Clear();
                build.ErrorItems = errorItems;

                //log all errors in their own DB for faster search
                List <string> errors = new List <string>();
                Dictionary <string, ErrorType> errorTypes = new Dictionary <string, ErrorType>();
                foreach (BuildEventErrorListItem item in build.ErrorItems)
                {
                    Match match = Regex.Match(item.ErrorListItem.Description, pattern);

                    //ignore bad matches
                    if (match.Groups.Count == 2)
                    {
                        string    errorCode = match.Groups[1].Value.ToLower().Trim();
                        ErrorType type      = Db.ErrorTypes.Where(t => t.Name == errorCode).FirstOrDefault();
                        if (type == null)
                        {
                            if (errorTypes.ContainsKey(errorCode) == false)
                            {
                                type = new ErrorType()
                                {
                                    Name = errorCode
                                };
                                Db.ErrorTypes.Add(type);
                            }
                            else
                            {
                                type = errorTypes[errorCode];
                            }
                        }
                        if (errorCode.Length > 0 && errors.Contains(errorCode) == false)
                        {
                            errors.Add(errorCode);
                        }
                        errorTypes[errorCode] = type;
                    }
                }
                Db.SaveChanges();
                foreach (string errorType in errors)
                {
                    Db.BuildErrors.Add(new BuildError()
                    {
                        BuildErrorTypeId = errorTypes[errorType].Id,
                        LogId            = log.Id
                    });
                }
            }
            else if (log.LogType == CutCopyPasteEvent.Name)
            {
                Db.CutCopyPasteEvents.Add((CutCopyPasteEvent)evt);
            }
            else if (log.LogType == DebugEvent.Name)
            {
                Db.DebugEvents.Add((DebugEvent)evt);
            }
            else if (log.LogType == EditorActivityEvent.Name)
            {
                Db.EditorActivityEvents.Add((EditorActivityEvent)evt);
            }
            else if (log.LogType == ExceptionEvent.Name)
            {
                Db.ExceptionEvents.Add((ExceptionEvent)evt);
            }
            else if (log.LogType == FeedPostEvent.Name)
            {
                hashtags = string.Join(",", ParseHashtags(((FeedPostEvent)evt).Comment));
                usertags = string.Join(",", ParseUserTags(((FeedPostEvent)evt).Comment));
                Db.FeedPostEvents.Add((FeedPostEvent)evt);
            }
            else if (log.LogType == HelpfulMarkGivenEvent.Name)
            {
                Db.HelpfulMarkGivenEvents.Add((HelpfulMarkGivenEvent)evt);
            }
            else if (log.LogType == LogCommentEvent.Name)
            {
                Db.LogCommentEvents.Add((LogCommentEvent)evt);
            }
            else if (log.LogType == SaveEvent.Name)
            {
                Db.SaveEvents.Add((SaveEvent)evt);
            }
            else if (log.LogType == SubmitEvent.Name)
            {
                errorLogger.Content += "Submit event detected.  ";
                Db.SubmitEvents.Add((SubmitEvent)evt);
            }
            try
            {
                errorLogger.Content += "Attempting to save to DB.  ";
                Db.SaveChanges();

                /*
                 * if(hashtags.Length >= 0 || usertags.Length >= 0 )
                 *
                 * using (var context = new OsbideProcs())
                 * {
                 *  context.InsertPostTags(log.Id, usertags, hashtags);
                 * }
                 * */
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
            {
                errorLogger.Content += "Error saving to DB:  ";
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        System.Diagnostics.Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        errorLogger.Content += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                        Db.LocalErrorLogs.Add(errorLogger);
                        Db.SaveChanges();
                    }
                }
                return(null);
            }
            catch (Exception ex)
            {
                errorLogger.Content += "Error saving to DB: " + ex.Message;
                Db.LocalErrorLogs.Add(errorLogger);
                Db.SaveChanges();
                System.Diagnostics.Trace.TraceInformation(ex.Message);
                return(null);
            }

            //Db.LocalErrorLogs.Add(errorLogger);
            //Db.SaveChanges();
            return(log);
        }