/// <summary>
        /// Send an email.
        /// </summary>
        /// <param name="to">Addressee.</param>
        /// <param name="subject">Sunject.</param>
        /// <param name="body">Message.</param>
        /// <param name="isBodyHtml">True if the message is wrote in HTML.</param>
        /// <returns>True if the email is correctly sended, false otherwise.</returns>
        public static bool SendEmail(String to, String subject, String body, bool isBodyHtml)
        {
            try
            {
                ConnectorDataContext db = new ConnectorDataContext();

                MailMessage message = new MailMessage();
                message.To.Add(new MailAddress(to));
                Stopwatch w = Stopwatch.StartNew();
                message.From = new MailAddress(db.Settings.Where(s => s.key == "MailAddress").Single().value, "SocialTFS");
                w.Stop();
                ILog log = LogManager.GetLogger("QueryLogger");
                log.Info(" Elapsed time: " + w.Elapsed + ", select the sending mail address");
                message.Subject = subject;
                message.IsBodyHtml = isBodyHtml;
                message.Body = body;
                Stopwatch w1 = Stopwatch.StartNew();
                SmtpClient smtp = new SmtpClient(db.Settings.Where(s => s.key == "SmtpServer").Single().value, Int32.Parse(db.Settings.Where(s => s.key == "SmtpPort").Single().value));
                w1.Stop();
                ILog log1 = LogManager.GetLogger("QueryLogger");
                log1.Info(" Elapsed time: " + w1.Elapsed + ", select the value of 'SmtpServer' key");
                Stopwatch w2 = Stopwatch.StartNew();
                String smtpSec = db.Settings.Where(s => s.key == "SmtpSecurity").Single().value;
                w2.Stop();
                ILog log2 = LogManager.GetLogger("QueryLogger");
                log2.Info(" Elapsed time: " + w2.Elapsed + ", select the value of 'SmtpSecurity' key");
                switch (smtpSec)
                {
                    case "None":
                        break;
                    case "SSL/TLS":
                        smtp.UseDefaultCredentials = false;
                        smtp.EnableSsl = true;
                        Stopwatch w3 = Stopwatch.StartNew();
                        smtp.Credentials = new NetworkCredential(db.Settings.Where(s => s.key == "MailAddress").Single().value, db.EncDecRc4("key", db.Settings.Where(s => s.key == "MailPassword").Single().value));
                        w3.Stop();
                        ILog log3 = LogManager.GetLogger("QueryLogger");
                        log3.Info(" Elapsed time: " + w3.Elapsed + ", select smtp credentials(SSL/TLS)");
                        break;
                    case "STARTTLS":
                        smtp.UseDefaultCredentials = false;
                        smtp.EnableSsl = true;
                        Stopwatch w4 = Stopwatch.StartNew();
                        smtp.Credentials = new NetworkCredential(db.Settings.Where(s => s.key == "MailAddress").Single().value, db.EncDecRc4("key", db.Settings.Where(s => s.key == "MailPassword").Single().value), "");
                        w4.Stop();
                        ILog log4 = LogManager.GetLogger("QueryLogger");
                        log4.Info(" Elapsed time: " + w4.Elapsed + ", select smtp credentials(STARTTLS)");
                        break;
                }
                smtp.Send(message);
                return true;
            }
            catch
            {
                return false;
            }
        }
        private void ChangeSmtpSettings()
        {
            ConnectorDataContext db = new ConnectorDataContext();

            try
            {
                bool changePassword = true;
                if (ChangeMailPasswordCB.Checked)
                {
                    if (Request.Params["ctl00$MainContent$MailPasswordTB"].Equals(Request.Params["ctl00$MainContent$MailConfirmTB"]))
                    {
                        Stopwatch w2 = Stopwatch.StartNew();
                        db.Settings.Where(s => s.key == "MailPassword").Single().value = db.EncDecRc4("key", Request.Params["ctl00$MainContent$MailPasswordTB"]);
                        w2.Stop();
                        ILog log2 = LogManager.GetLogger("QueryLogger");
                        log2.Info(" Elapsed time: " + w2.Elapsed + ", select the value of 'MailPassword' key from settings");
                    }
                    else
                    {
                        ErrorPA.Attributes.Add("class", "error");
                        ErrorPA.InnerText = "Passwords do not match.";
                        changePassword = false;
                    }
                }
                if (changePassword)
                {
                    Stopwatch w3 = Stopwatch.StartNew();
                    db.Settings.Where(s => s.key == "SmtpServer").Single().value = Request.Params["ctl00$MainContent$SmtpServerTB"];
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", set the value of 'Smtp Server' key");
                    Stopwatch w4 = Stopwatch.StartNew();
                    db.Settings.Where(s => s.key == "SmtpPort").Single().value = Request.Params["ctl00$MainContent$SmtpPortTB"];
                    w4.Stop();
                    ILog log4 = LogManager.GetLogger("QueryLogger");
                    log4.Info(" Elapsed time: " + w4.Elapsed + ", set the value of 'Smtp Port' key");
                    Stopwatch w5 = Stopwatch.StartNew();
                    db.Settings.Where(s => s.key == "SmtpSecurity").Single().value = Request.Params["ctl00$MainContent$SmtpSecuritySE"];
                    w5.Stop();
                    ILog log5 = LogManager.GetLogger("QueryLogger");
                    log5.Info(" Elapsed time: " + w5.Elapsed + ", set the value of 'Smtp Security' key");
                    Stopwatch w6 = Stopwatch.StartNew();
                    db.Settings.Where(s => s.key == "MailAddress").Single().value = Request.Params["ctl00$MainContent$MailAddressTB"];
                    w6.Stop();
                    ILog log6 = LogManager.GetLogger("QueryLogger");
                    log6.Info(" Elapsed time: " + w6.Elapsed + ", set the value of 'MailAddress' key");

                    Stopwatch w7 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w7.Stop();
                    ILog log7 = LogManager.GetLogger("QueryLogger");
                    log7.Info(" Elapsed time: " + w7.Elapsed + ", change smtp settings");
                    ErrorPA.Attributes.Add("class", "confirm");
                    ErrorPA.InnerText = "Data stored.";
                }
            }
            catch
            {
                try
                {
                    Stopwatch w8 = Stopwatch.StartNew();
                    db.Settings.InsertAllOnSubmit(new List<Setting>(){
                        new Setting () {
                            key = "SmtpServer",
                            value = Request.Params["ctl00$MainContent$SmtpServerTB"]
                        },
                        new Setting () {
                            key = "SmtpPort",
                            value = Request.Params["ctl00$MainContent$SmtpPortTB"]
                        },
                        new Setting () {
                            key = "SmtpSecurity",
                            value = Request.Params["ctl00$MainContent$SmtpSecuritySE"]
                        },
                        new Setting () {
                            key = "MailAddress",
                            value = Request.Params["ctl00$MainContent$MailAddressTB"]
                        },
                        new Setting () {
                            key = "MailPassword",
                            value = db.EncDecRc4("key",Request.Params["ctl00$MainContent$MailPasswordTB"])
                        }
                    });
                    db.SubmitChanges();
                    w8.Stop();
                    ILog log8 = LogManager.GetLogger("QueryLogger");
                    log8.Info(" Elapsed time: " + w8.Elapsed + ", insert new settings");
                    ErrorPA.Attributes.Add("class", "confirm");
                    ErrorPA.InnerText = "Data stored.";
                }
                catch
                {
                    ErrorPA.Attributes.Add("class", "error");
                    ErrorPA.InnerText = "Something was wrong. Please try again later.";
                }
            }
        }
        public bool RecordService(string username, string password, int service, string usernameOnService, string passwordOnService, string domain)
        {
            Contract.Requires(!String.IsNullOrEmpty(username));
            Contract.Requires(!String.IsNullOrEmpty(password));
            Contract.Requires(!String.IsNullOrEmpty(usernameOnService));

            ConnectorDataContext db = new ConnectorDataContext();

            User user = CheckCredentials(db, username, password);
            if (user == null)
                return false;

            Stopwatch w = Stopwatch.StartNew();
            ServiceInstance serviceInstance = db.ServiceInstances.Where(s => s.id == service).Single();
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", service id: " + service + ", record a service without OAuth authentication procedure");

            IService iService = ServiceFactory.getService(
                serviceInstance.Service.name,
                usernameOnService,
                passwordOnService,
                domain,
                serviceInstance.host);

            IUser iUser = iService.VerifyCredential();

            return RegisterUserOnAService(db, user, serviceInstance, iUser, db.EncDecRc4("key", passwordOnService), (String)iUser.Get(UserFeaturesType.Domain));
        }
        private void UpdateSuggestion(User user)
        {
            ConnectorDataContext db = new ConnectorDataContext();
            Dictionary<int, HashSet<int>> logFriends = new Dictionary<int, HashSet<int>>();
            bool needToLog = false;

            Stopwatch w = Stopwatch.StartNew();
            IEnumerable<ChosenFeature> chosenFeatures = db.ChosenFeatures.Where(
                cf => (cf.feature.Equals(FeaturesType.Followings.ToString()) ||
                    cf.feature.Equals(FeaturesType.Followers.ToString()) ||
                    cf.feature.Equals(FeaturesType.TFSCollection.ToString()) ||
                    cf.feature.Equals(FeaturesType.TFSTeamProject.ToString())) && cf.user == user.id);
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", select all chosen features of an author and his feature 'followings' or 'followers' or 'TFSCollection' or 'TFSTeamProject'");

            foreach (ChosenFeature chosenFeature in chosenFeatures)
            {
                Stopwatch w2 = Stopwatch.StartNew();
                ChosenFeature temp = db.ChosenFeatures.Where(cf => cf.id == chosenFeature.id).Single();
                w2.Stop();
                ILog log2 = LogManager.GetLogger("QueryLogger");
                log2.Info(" Elapsed time: " + w2.Elapsed + ", chosen feature's id: " + chosenFeature.id + ", select a chosen feature");
                if (temp.lastDownload > DateTime.UtcNow - _suggestionSpan)
                    continue;
                else
                    temp.lastDownload = DateTime.UtcNow;

                try
                {
                    Stopwatch w1 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w1.Stop();
                    ILog log1 = LogManager.GetLogger("QueryLogger");
                    log1.Info(" Elapsed time: " + w1.Elapsed + ", update the chosen feature according to the date(suggestion)");
                    needToLog = true;
                }
                catch
                {
                    continue;
                }

                IService service = null;

                if (chosenFeature.feature.Equals(FeaturesType.Followings.ToString()) ||
                    chosenFeature.feature.Equals(FeaturesType.Followers.ToString()))
                    service = ServiceFactory.getServiceOauth(
                        chosenFeature.Registration.ServiceInstance.Service.name,
                        chosenFeature.Registration.ServiceInstance.host,
                        chosenFeature.Registration.ServiceInstance.consumerKey,
                        chosenFeature.Registration.ServiceInstance.consumerSecret,
                        chosenFeature.Registration.accessToken,
                        chosenFeature.Registration.accessSecret);
                else if (chosenFeature.feature.Equals(FeaturesType.TFSCollection.ToString()) ||
                    chosenFeature.feature.Equals(FeaturesType.TFSTeamProject.ToString()))
                {
                    if (temp.Registration.ServiceInstance.Service.name.Equals("GitHub"))
                    {
                        service = ServiceFactory.getServiceOauth(temp.Registration.ServiceInstance.Service.name, temp.Registration.ServiceInstance.host, temp.Registration.ServiceInstance.consumerKey, temp.Registration.ServiceInstance.consumerSecret, temp.Registration.accessToken, null);
                    }
                    else
                    {
                        service = ServiceFactory.getService(
                            chosenFeature.Registration.ServiceInstance.Service.name,
                            chosenFeature.Registration.nameOnService,
                            db.EncDecRc4("key", chosenFeature.Registration.accessToken),
                            chosenFeature.Registration.accessSecret,
                            chosenFeature.Registration.ServiceInstance.host);
                    }
                }

                string[] friends = null;

                if (chosenFeature.feature.Equals(FeaturesType.Followings.ToString()))
                    friends = (string[])service.Get(FeaturesType.Followings, null);
                else if (chosenFeature.feature.Equals(FeaturesType.Followers.ToString()))
                    friends = (string[])service.Get(FeaturesType.Followers, null);
                else if (chosenFeature.feature.Equals(FeaturesType.TFSCollection.ToString()))
                    friends = (string[])service.Get(FeaturesType.TFSCollection, null);
                else if (chosenFeature.feature.Equals(FeaturesType.TFSTeamProject.ToString()))
                    friends = (string[])service.Get(FeaturesType.TFSTeamProject, null);

                if (friends != null)
                {
                    //Delete suggestions for this chosen feature in the database
                    Stopwatch w3 = Stopwatch.StartNew();
                    db.Suggestions.DeleteAllOnSubmit(db.Suggestions.Where(s => s.chosenFeature == chosenFeature.id));
                    db.SubmitChanges();
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", chosen feature's id: " + chosenFeature.id + ", delete suggestions for this chosen feature");

                    foreach (string friend in friends)
                    {
                        Stopwatch w4 = Stopwatch.StartNew();
                        IEnumerable<User> friendInSocialTfs = db.Registrations.Where(r => r.idOnService == friend &&
                            r.serviceInstance == chosenFeature.serviceInstance).Select(r => r.User);
                        w4.Stop();
                        ILog log4 = LogManager.GetLogger("QueryLogger");
                        log4.Info(" Elapsed time: " + w4.Elapsed + ", user id: " + friend + ", feature's name: " + chosenFeature.serviceInstance + ", select all users that can be possible friends in SocialTFS");
                        if (friendInSocialTfs.Count() == 1)
                        {
                            User suggestedFriend = friendInSocialTfs.First();

                            if (friend != chosenFeature.Registration.idOnService)
                            {
                                Stopwatch w5 = Stopwatch.StartNew();
                                db.Suggestions.InsertOnSubmit(new Suggestion()
                                {
                                    user = suggestedFriend.id,
                                    chosenFeature = chosenFeature.id
                                });
                                w5.Stop();
                                ILog log5 = LogManager.GetLogger("QueryLogger");
                                log5.Info(" Elapsed time: " + w5.Elapsed + ", user id: " + suggestedFriend.id + ", chosen feature: " + chosenFeature.id + ", insert a suggestion in a pending state");

                                if (!logFriends.ContainsKey(suggestedFriend.id))
                                    logFriends[suggestedFriend.id] = new HashSet<int>();
                                logFriends[suggestedFriend.id].Add(temp.Registration.serviceInstance);
                            }
                        }
                    }
                    try
                    {
                        Stopwatch w6 = Stopwatch.StartNew();
                        db.SubmitChanges();
                        w6.Stop();
                        ILog log6 = LogManager.GetLogger("QueryLogger");
                        log6.Info(" Elapsed time: " + w6.Elapsed + ", insert a suggestion");
                    }
                    catch { }
                }
            }

            if (needToLog)
            {
                ILog log7 = LogManager.GetLogger("NetworkLogger");
                log7.Info(user.id + ",S,[" + GetFriendString(user.id, logFriends) + "]");
            }
        }
        private void UpdateInteractiveFriend(User user)
        {
            ConnectorDataContext db = new ConnectorDataContext();

            Stopwatch w = Stopwatch.StartNew();
            List<ChosenFeature> cFeature = db.ChosenFeatures.Where(cf => cf.user == user.id && cf.feature == FeaturesType.InteractiveNetwork.ToString()).ToList();
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", feature's name: " + FeaturesType.InteractiveNetwork.ToString() + ", select all chosen features of an author and his feature 'interactive network'");

            foreach (ChosenFeature chosenFeature in cFeature)
            {
                Stopwatch w2 = Stopwatch.StartNew();
                ChosenFeature temp = db.ChosenFeatures.Where(cf => cf.id == chosenFeature.id).Single();
                w2.Stop();
                ILog log2 = LogManager.GetLogger("QueryLogger");
                log2.Info(" Elapsed time: " + w2.Elapsed + ", chosen feature's id: " + chosenFeature.id + ", select a chosen feature");
                if (temp.lastDownload > DateTime.UtcNow - _interactiveSpan)
                    continue;
                else
                    temp.lastDownload = DateTime.UtcNow;

                try
                {
                    Stopwatch w1 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w1.Stop();
                    ILog log1 = LogManager.GetLogger("QueryLogger");
                    log1.Info(" Elapsed time: " + w1.Elapsed + ", update the chosen feature according to the date(interactive friend)");
                }
                catch { continue; }

                IService service;
                if (temp.Registration.ServiceInstance.Service.name.Equals("GitHub"))
                {
                    service = ServiceFactory.getServiceOauth(temp.Registration.ServiceInstance.Service.name, temp.Registration.ServiceInstance.host, temp.Registration.ServiceInstance.consumerKey, temp.Registration.ServiceInstance.consumerSecret, temp.Registration.accessToken, null);
                }
                else
                {
                    //submit new friendship for the current chosen feature
                    service = ServiceFactory.getService(
                       temp.Registration.ServiceInstance.Service.name,
                       temp.Registration.nameOnService,
                       db.EncDecRc4("key", temp.Registration.accessToken),
                       temp.Registration.accessSecret,
                       temp.Registration.ServiceInstance.host);
                }
                //this line must be before the deleting
                SCollection[] collections = (SCollection[])service.Get(FeaturesType.InteractiveNetwork);
                try
                {
                    //vecchio metodo
                    Stopwatch w3 = Stopwatch.StartNew();
                    db.InteractiveFriends.DeleteAllOnSubmit(db.InteractiveFriends.Where(intFr => intFr.chosenFeature == temp.id));
                    db.SubmitChanges();
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", chosen feature's id: " + temp.id + ", remove all old interactive friends according to the chosen feature");

                    foreach (SCollection collection in collections)
                    {
                        foreach (SWorkItem workitem in collection.WorkItems)
                        {
                            Stopwatch w4 = Stopwatch.StartNew();
                            IEnumerable<int> friendsInDb = db.Registrations.Where(r => workitem.InvolvedUsers.Contains(r.nameOnService) || workitem.InvolvedUsers.Contains(r.accessSecret + "\\" + r.nameOnService)).Select(r => r.user);
                            w4.Stop();
                            ILog log4 = LogManager.GetLogger("QueryLogger");
                            log4.Info(" Elapsed time: " + w4.Elapsed + ", select all users that are working on the same workitem");
                            foreach (int friendInDb in friendsInDb)
                            {
                                Stopwatch w5 = Stopwatch.StartNew();
                                db.InteractiveFriends.InsertOnSubmit(new InteractiveFriend()
                                {
                                    user = friendInDb,
                                    chosenFeature = temp.id,
                                    collection = collection.Uri,
                                    interactiveObject = workitem.Name,
                                    objectType = "WorkItem"
                                });
                                w5.Stop();
                                ILog log5 = LogManager.GetLogger("QueryLogger");
                                log5.Info(" Elapsed time: " + w5.Elapsed + ", user id: " + friendInDb + ", chosen feature: " + temp.id + ", collection uri: " + collection.Uri + ", interactive object: " + workitem.Name + ", insert an interactive friend which is working on a workitem in a pending state");
                            }
                        }
                        foreach (SFile file in collection.Files)
                        {
                            Stopwatch w6 = Stopwatch.StartNew();
                            IEnumerable<int> friendsInDb = db.Registrations.Where(r => file.InvolvedUsers.Contains(r.nameOnService) || file.InvolvedUsers.Contains(r.accessSecret + "\\" + r.nameOnService)).Select(r => r.user);
                            w6.Stop();
                            ILog log6 = LogManager.GetLogger("QueryLogger");
                            log6.Info(" Elapsed time: " + w6.Elapsed + ", select all users that are working on the same file");
                            foreach (int friendInDb in friendsInDb)
                            {
                                Stopwatch w7 = Stopwatch.StartNew();
                                db.InteractiveFriends.InsertOnSubmit(new InteractiveFriend()
                                {
                                    user = friendInDb,
                                    chosenFeature = temp.id,
                                    collection = collection.Uri,
                                    interactiveObject = file.Name,
                                    objectType = "File"
                                });
                                w7.Stop();
                                ILog log7 = LogManager.GetLogger("QueryLogger");
                                log7.Info(" Elapsed time: " + w7.Elapsed + ", user id: " + friendInDb + ", chosen feature: " + temp.id + ", collection uri: " + collection.Uri + ", interactive object: " + file.Name + ", insert an interactive friend which is working on a file in a pending state");
                            }
                        }
                    }

                    Stopwatch w8 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w8.Stop();
                    ILog log8 = LogManager.GetLogger("QueryLogger");
                    log8.Info(" Elapsed time: " + w8.Elapsed + ", insert the interactive friend");
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e.StackTrace);
                }
            }
        }
        private void UpdateDynamicFriend(User user)
        {
            ConnectorDataContext db = new ConnectorDataContext();
            Dictionary<int, HashSet<int>> logFriends = new Dictionary<int, HashSet<int>>();
            bool needToLog = false;

            Stopwatch w = Stopwatch.StartNew();
            List<ChosenFeature> cFeature = db.ChosenFeatures.Where(cf => cf.user == user.id && cf.feature == FeaturesType.IterationNetwork.ToString()).ToList();
            w.Stop();
            ILog log = LogManager.GetLogger("QueryLogger");
            log.Info(" Elapsed time: " + w.Elapsed + ", user id: " + user.id + ", feature's name: " + FeaturesType.IterationNetwork.ToString() + ", select all chosen feature of an author and his feature 'Iteration Network'");

            foreach (ChosenFeature chosenFeature in cFeature)
            {
                Stopwatch w1 = Stopwatch.StartNew();
                ChosenFeature temp = db.ChosenFeatures.Where(cf => cf.id == chosenFeature.id).Single();
                w1.Stop();
                ILog log1 = LogManager.GetLogger("QueryLogger");
                log1.Info(" Elapsed time: " + w1.Elapsed + ", chosen feature's id: " + chosenFeature.id + ", select a chosen feature to update");
                if (temp.lastDownload > DateTime.UtcNow - _dynamicSpan)
                    continue;
                else
                    temp.lastDownload = DateTime.UtcNow;

                try
                {
                    Stopwatch w3 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w3.Stop();
                    ILog log3 = LogManager.GetLogger("QueryLogger");
                    log3.Info(" Elapsed time: " + w3.Elapsed + ", update the chosen feature according to the date(dynamic friend)");
                    needToLog = true;
                }
                catch
                {
                    continue;
                }

                IService service;
                //submit new friendship for the current chosen feature
                if (temp.Registration.ServiceInstance.Service.name.Equals("GitHub"))
                {
                    service = ServiceFactory.getServiceOauth(temp.Registration.ServiceInstance.Service.name, temp.Registration.ServiceInstance.host, temp.Registration.ServiceInstance.consumerKey, temp.Registration.ServiceInstance.consumerSecret, temp.Registration.accessToken, null);
                }
                else
                {
                    service = ServiceFactory.getService(
                        temp.Registration.ServiceInstance.Service.name,
                        temp.Registration.nameOnService,
                        db.EncDecRc4("key", temp.Registration.accessToken),
                        temp.Registration.accessSecret,
                        temp.Registration.ServiceInstance.host);
                }
                //this line must be before the deleting
                String[] dynamicFriends = (String[])service.Get(FeaturesType.IterationNetwork, null);

                //delete old friendship for the current chosen feature
                Stopwatch w4 = Stopwatch.StartNew();
                db.DynamicFriends.DeleteAllOnSubmit(db.DynamicFriends.Where(df => df.chosenFeature == temp.id));
                db.SubmitChanges();
                w4.Stop();
                ILog log4 = LogManager.GetLogger("QueryLogger");
                log4.Info(" Elapsed time: " + w4.Elapsed + ", chosen feature's id: " + temp.id + ", delete old friendship for the current chosen feature");

                foreach (String dynamicFriend in dynamicFriends)
                {
                    Stopwatch w5 = Stopwatch.StartNew();
                    IEnumerable<int> friendsInDb = db.Registrations.Where(r => r.nameOnService == dynamicFriend && r.serviceInstance == temp.serviceInstance).Select(r => r.user);
                    w5.Stop();
                    ILog log5 = LogManager.GetLogger("QueryLogger");
                    log5.Info(" Elapsed time: " + w5.Elapsed + ", : dynamic friend" + dynamicFriend + ", service instance: " + temp.serviceInstance + ", select user to add as dynamic friend");
                    foreach (int friendInDb in friendsInDb)
                    {
                        Stopwatch w6 = Stopwatch.StartNew();
                        db.DynamicFriends.InsertOnSubmit(new DynamicFriend()
                        {
                            chosenFeature = temp.id,
                            user = friendInDb
                        });
                        w6.Stop();
                        ILog log6 = LogManager.GetLogger("QueryLogger");
                        log6.Info(" Elapsed time: " + w6.Elapsed + ", chosen feature's id: " + temp.id + ", friend id: " + friendInDb + ", insert a new dynamic friend in a pending state");

                        if (!logFriends.ContainsKey(friendInDb))
                            logFriends[friendInDb] = new HashSet<int>();
                        logFriends[friendInDb].Add(temp.Registration.serviceInstance);
                    }
                }
                try
                {
                    Stopwatch w7 = Stopwatch.StartNew();
                    db.SubmitChanges();
                    w7.Stop();
                    ILog log7 = LogManager.GetLogger("QueryLogger");
                    log7.Info(" Elapsed time: " + w7.Elapsed + ", insert a dynamic friend");
                }
                catch { }
            }

            if (needToLog)
            {
                ILog log8 = LogManager.GetLogger("NetworkLogger");
                log8.Info(user.id + ",I,[" + GetFriendString(user.id, logFriends) + "]");
            }
        }