Exemple #1
0
        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            //apparantly can prevent error:

            /*
             *          Session state has created a session id, but cannot save it because the response was already flushed by the application
             */
            string sessionId = Session.SessionID;

            try
            {
                //Do we want to track the referer somehow??
                string lRefererPage = GetUserReferrer();
                bool   isBot        = false;
                string ipAddress    = this.GetUserIPAddress();
                //check for bots
                //use common method
                string agent = GetUserAgent(ref isBot);
                string url   = "MISSING";
                try
                {
                    HttpContext con = HttpContext.Current;
                    url = con.Request.Url.ToString();
                }
                catch
                {
                    //skip
                }
                if (isBot == false)
                {
                    AppUser user = new AppUser();
                    //not always helpful
                    if (User.Identity.IsAuthenticated)
                    {
                        user = AccountServices.GetCurrentUser(User.Identity.Name);
                    }
                    string userState = user.Id > 0 ? string.Format("User: {0}", user.FullName()) : "none";

                    LoggingHelper.DoTrace(6, string.Format("Session_Start. referrer: {0}, starting Url: {1} agent: {2}, IP Address: {3}, User?: {4}", lRefererPage, url, agent, ipAddress, userState));

                    string startMsg = "Session Started. SessionID: " + sessionId;

                    startMsg += ", IP Address: " + ipAddress;

                    startMsg += ", User: "******", Agent: " + agent;
                    ActivityServices.SessionStartActivity(startMsg, sessionId, ipAddress, lRefererPage, isBot);
                }
                else
                {
                    LoggingHelper.DoBotTrace(8, string.Format("Session_Start. Skipping bot: referrer: {0}, agent: {1}", lRefererPage, agent));
                }
            }
            catch (Exception ex)
            {
                LoggingHelper.LogError(ex, "Session_Start. =================");
            }
        } //
        } //

        private static int SiteActivityAdd(ActivityLog log)
        {
            int    count       = 0;
            string truncateMsg = "";
            bool   isBot       = false;
            string server      = UtilityManager.GetAppKeyValue("serverName", "");

            string agent = GetUserAgent(ref isBot);

            if (log.RelatedTargetUrl == null)
            {
                log.RelatedTargetUrl = "";
            }

            if (log.Referrer == null)
            {
                log.Referrer = "";
            }
            if (log.Comment == null)
            {
                log.Comment = "";
            }
            if (log.SessionId == null || log.SessionId.Length < 10)
            {
                log.SessionId = GetCurrentSessionId();
            }

            if (log.IPAddress == null || log.IPAddress.Length < 10)
            {
                log.IPAddress = GetUserIPAddress();
            }
            if (log.IPAddress.Length > 50)
            {
                log.IPAddress = log.IPAddress.Substring(0, 50);
            }

            //================================
            if (isBot)
            {
                LoggingHelper.DoBotTrace(6, string.Format(".SiteActivityAdd Skipping Bot: activity. Agent: {0}, Activity: {1}, Event: {2}, \r\nRelatedTargetUrl: {3}", agent, log.Activity, log.Event, log.RelatedTargetUrl));
                //should this be added with isBot attribute for referencing when crawled?
                return(0);
            }
            //================================
            if (IsADuplicateRequest(log.Comment))
            {
                return(0);
            }

            StoreLastRequest(log.Comment);

            //----------------------------------------------
            if (log.Referrer == null || log.Referrer.Trim().Length < 5)
            {
                string referrer = GetUserReferrer();
                log.Referrer = referrer;
            }
            //if ( log.Referrer.Length > 1000 )
            //{
            //    truncateMsg += string.Format( "Referrer overflow: {0}; ", log.Referrer.Length );
            //    log.Referrer = log.Referrer.Substring( 0, 1000 );
            //}


            if (log.RelatedTargetUrl != null && log.RelatedTargetUrl.Length > 500)
            {
                truncateMsg         += string.Format("RelatedTargetUrl overflow: {0}; ", log.RelatedTargetUrl.Length);
                log.RelatedTargetUrl = log.RelatedTargetUrl.Substring(0, 500);
            }

            //if ( log.Referrer.Length > 0 )
            //    log.Comment += ", Referrer: " + log.Referrer;

            //log.Comment += GetUserAgent();

            //if ( log.Comment != null && log.Comment.Length > 1000 )
            //{
            //    truncateMsg += string.Format( "Comment overflow: {0}; ", log.Comment.Length );
            //    log.Comment = log.Comment.Substring( 0, 1000 );
            //}

            //the following should not be necessary but getting null related exceptions
            if (log.TargetUserId == null)
            {
                log.TargetUserId = 0;
            }
            if (log.ActionByUserId == null)
            {
                log.ActionByUserId = 0;
            }
            if (log.ActivityObjectId == null)
            {
                log.ActivityObjectId = 0;
            }
            if (log.ObjectRelatedId == null)
            {
                log.ObjectRelatedId = 0;
            }
            if (log.TargetObjectId == null)
            {
                log.TargetObjectId = 0;
            }


            using (var context = new EntityContext())
            {
                try
                {
                    log.CreatedDate = System.DateTime.Now;
                    if (log.ActivityType == null || log.ActivityType.Length < 5)
                    {
                        log.ActivityType = "Audit";
                    }

                    context.ActivityLog.Add(log);

                    // submit the change to database
                    count = context.SaveChanges();

                    if (truncateMsg.Length > 0)
                    {
                        string msg = string.Format("ActivityId: {0}, Message: {1}", log.Id, truncateMsg);

                        EmailManager.NotifyAdmin("ActivityLog Field Overflow", msg);
                    }
                    if (count > 0)
                    {
                        return(log.Id);
                    }
                    else
                    {
                        //?no info on error
                        return(0);
                    }
                }
                catch (Exception ex)
                {
                    LoggingHelper.LogError(ex, thisClassName + ".SiteActivityAdd(EFDAL.ActivityLog) ==> SHOULD ADD RETRY via proc\n\r" + ex.StackTrace.ToString());
                    //call stored proc as backup!

                    return(count);
                }
            }
        } //