Example #1
0
        public void AllAlternateMitigationPathsFailure(CustomObjectToAuditAuditPoint CustomObjectToAuditAuditPoint)
        {
            //clear the CustomObjectToAudit audit point from the dictionary
            //log a fatal

            _log.Fatal("Attempts to Write Audit Point to Audit Service have been exhausted, Multiple System Failure");
            QueueHelper <CustomObjectToAuditAuditPoint> queueHelper = new QueueHelper <CustomObjectToAuditAuditPoint>();

            _log.Fatal("Message That was Not Able to Be Processed:  " + queueHelper.GetStringMessage(CustomObjectToAuditAuditPoint));
        }
        public string SubmitAuditMessageToDBWriterQ(CustomObjectToAuditAuditPoint CustomObjectToAuditAuditPoint, out MessageWrapper wrapper)
        {
            string returnMessage = "Audit Point Received - Unsuccessful";

            string queuePath = ConfigurationManager.AppSettings["dbWriterQueueName"];
            string messageThresholdString = ConfigurationManager.AppSettings["messageQueueThreshold"];
            int    messageThreshold       = Convert.ToInt32((messageThresholdString == null) ? "1000" : messageThresholdString);

            MessageQueue q = null;

            try
            {
                q = new MessageQueue(queuePath);
            }
            catch (Exception ex)
            {
                _log.Fatal("Cannot Receive Audit Messages due to MSMQ, Fatal, Mitigations Being Attempted, QueuePath:  " + queuePath, ex);
                throw new ApplicationException("Cannot Receive Audit Messages due to MSMQ, Fatal, Mitigations Being Attempted, QueuePath:  " + queuePath, ex);
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }

            try
            {
                if (IsMSMQOverThreshold(q, messageThreshold))
                {
                    _log.Fatal("Audit MSMQ Message Threshold Exceeded, Please Check MSMQ " + q.QueueName);
                }
                try
                {
                    WriteMessage(q, CustomObjectToAuditAuditPoint, out wrapper);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException("Cannot Write Audit Messages to MSMQ: " + q.QueueName);
                }
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }

            returnMessage = "Audit Point Received - Successful";
            return(returnMessage);
        }
        /// <summary>
        /// This is called when we have a failure to put on to the receiver Q
        /// </summary>
        private void AlternateWritePathDueToError(CustomObjectToAuditAuditPoint CustomObjectToAuditAuditPoint, out MessageWrapper wrapper)
        {
            _log.Info("Alternate Receive Audit Message Path Initiated - Cause:  AuditService Receiver MSMQ Error");

            wrapper = null;

            try
            {
                SubmitAuditMessageToDBWriterQ(CustomObjectToAuditAuditPoint, out wrapper);
                _log.Info("Alternate Receive Audit Message Path Successful:  Written to Audit DB Writer MSMQ");
            }
            catch (Exception exSubmitToDBWriterQ)
            {
                _log.Fatal("Cannot Receive Audit Messages due to MSMQ, Fatal, Mitigations Being Attempted", exSubmitToDBWriterQ);

                try
                {
                    WriteMessageToDatabase(CustomObjectToAuditAuditPoint);
                    _log.Info("Alternate Receive Audit Message Path Successful:  Written to Central Audit Database Directly");
                }
                catch (Exception exSubmitToDbDirect)
                {
                    _log.Fatal("Writing Message Directly to Central Audit Database Failed, Last Ditch Effort For Recovery, Attempting to Write to Error MSMQ", exSubmitToDbDirect);
                    try
                    {
                        DatabaseDownMitigation(CustomObjectToAuditAuditPoint);
                        _log.Info("Alternate Receive Audit Message Path Successful:  Written to Audit Error MSMQ");
                    }
                    catch (Exception exWriteToErrorQ)
                    {
                        _log.Fatal(exWriteToErrorQ);
                        _log.Info("Alternate Receive Audit Message Path Not Successful:  Message was not Received by Audit Service");

                        AllAlternateMitigationPathsFailure(CustomObjectToAuditAuditPoint);
                        throw new ApplicationException("Cannot Accept Your Message At this time, Last Ditch Effort For Recovery failed, Multiple System Failure", exWriteToErrorQ);
                    }
                }
            }
        }
Example #4
0
 private void WriteMessageToDatabase(CustomObjectToAuditAuditPoint auditPoint)
 {
     AuditServicesDataAccess.AuditDatabase _db = new AuditServicesDataAccess.AuditDatabase();
     _db.WriteAudit(auditPoint);
 }
        /// <summary>
        /// Submits the audit to auditmessagequeue
        /// </summary>
        /// <param name="auditPoint">The audit point.</param>
        /// <returns>Status of Queueing</returns>
        public string SubmitAudit(CustomObjectToAuditAuditPoint auditPoint)
        {
            string         returnMessage          = "Audit Point Received - Unsuccessful";
            string         queuePath              = ConfigurationManager.AppSettings["queueName"];
            string         messageThresholdString = ConfigurationManager.AppSettings["messageQueueThreshold"];
            int            messageThreshold       = Convert.ToInt32((messageThresholdString == null) ? "1000" : messageThresholdString);
            MessageQueue   q       = null;
            MessageWrapper wrapper = null;

            try
            {
                q = new MessageQueue(queuePath);
            }
            catch (Exception exQueueInstantiation)
            {
                _log.Fatal("Cannot Receive Audit Messages due to MSMQ " + q.QueueName, exQueueInstantiation);
                try
                {
                    AlternateWritePathDueToError(auditPoint, out wrapper);
                }
                catch (Exception exAlternatePathError)
                {
                    _log.Fatal("Cannot Receive Audit Messages and Alternate Path Mitigations also Failed, Major multiple Systems Outage", exAlternatePathError);
                    throw new ApplicationException("Cannot Receive Audit Messages and Alternate Path Mitigations also Failed, Major multiple Systems Outage", exAlternatePathError);
                }
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }
            try
            {
                if (IsMSMQOverThreshold(q, messageThreshold))
                {
                    _log.Fatal("Audit MSMQ Message Threshold Exceeded, Please Check MSMQ, still accepting Messages But Unfixed Could use up Maximum Memory on Machine" + q.QueueName);
                }
                try
                {
                    WriteMessage(q, auditPoint, out wrapper);
                }
                catch (Exception exWriteToReceiverQ)
                {
                    _log.Fatal("Cannot Receive Audit Messages due to MSMQ " + q.QueueName, exWriteToReceiverQ);
                    try
                    {
                        AlternateWritePathDueToError(auditPoint, out wrapper);
                    }
                    catch (Exception exAlternatePathError)
                    {
                        _log.Fatal("Cannot Receive Audit Messages and Alternate Path Mitigations also Failed, Major multiple Systems Outage", exAlternatePathError);
                        throw new ApplicationException("Cannot Receive Audit Messages and Alternate Path Mitigations also Failed, Major multiple Systems Outage", exAlternatePathError);
                    }
                }
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }
            returnMessage = "Audit Point Received - Successful";
            return(returnMessage);
        }
        private string DatabaseDownMitigation(CustomObjectToAuditAuditPoint auditPoint)
        {
            MessageWrapper wrapper       = null;
            string         returnMessage = "Audit Point Received - Unsuccessfully";

            string messageThresholdString = ConfigurationManager.AppSettings["messageQueueThreshold"];
            int    messageThreshold       = Convert.ToInt32((messageThresholdString == null) ? "1000" : messageThresholdString);

            string messageQueueSetting = ConfigurationManager.AppSettings["errorQueueName"];
            string errorQueueName      = messageQueueSetting == null ? "no message queue defined in config" : messageQueueSetting;

            //this is our last ditch effort to keep this audit , we put it on an error
            //Q which then should be processed when the database is back up.            string queuePath = ConfigurationManager.AppSettings["dbWriterQueueName"];

            MessageQueue q = null;

            try
            {
                if (!MessageQueue.Exists(errorQueueName))
                {
                    MessageQueue.Create(errorQueueName, true);
                }

                q = new MessageQueue(errorQueueName);
            }
            catch (Exception exQueueInstantiation)
            {
                _log.Fatal("Cannot Accept Any Audit Messages At this time, All Mitigations Have Failed, Multiple Systems Failure has Occurred, Error Queue Name:  " + errorQueueName, exQueueInstantiation);
                throw new ApplicationException("Cannot instantiate or create MSMQ " + errorQueueName, exQueueInstantiation);
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }

            try
            {
                if (IsMSMQOverThreshold(q, messageThreshold))
                {
                    _log.Fatal("Audit MSMQ Message Threshold Exceeded, Please Check MSMQ, still accepting Messages But Unfixed Could use up Maximum Memory on Machine" + q.QueueName);
                }

                try
                {
                    WriteMessage(q, auditPoint, out wrapper);
                }
                catch (Exception ex)
                {
                    _log.Fatal("Cannot Accept Any Audit Messages At this time, All Mitigations Have Failed, Multiple Systems Failure has Occurred, Error Queue Name: " + q.QueueName, ex);
                    throw new ApplicationException("Cannot Accept Any Audit Messages At this time, All Mitigations Have Failed, Multiple Systems Failure has Occurred, Error Queue Name: " + q.QueueName, ex);
                }
            }
            finally
            {
                if (q != null)
                {
                    q.Dispose();
                }
            }
            returnMessage = "Audit Point Written to Error Q - Successful";

            return(returnMessage);
        }
        public Guid WriteAudit(CustomObjectToAuditAuditPoint auditPoint)
        {
            Guid returnValue = Guid.Empty;

            //it is extremely important to sort the parameters in the parameters dictionary so that we can do group by
            //clauses in the sproc for easy finding of duplicate jobs.  just use the default sort in the sorted dictionary, asc i think
            //by key

            SortedDictionary <string, string> sortedParameters = new SortedDictionary <string, string>(auditPoint.Parameters);

            try
            {
                SqlDateTime auditDateTimeStampSql = (auditPoint.AuditDateTimeStamp == DateTime.MaxValue ||
                                                     auditPoint.AuditDateTimeStamp == DateTime.MinValue) ? SqlDateTime.MinValue : new System.Data.SqlTypes.SqlDateTime(auditPoint.AuditDateTimeStamp);

                string referenceID = (string.IsNullOrEmpty(auditPoint.ReferenceID)) ? null : auditPoint.ReferenceID;

                Guid ApplicationID   = GetApplication(auditPoint.ApplicationName.ToString());
                Guid AuditCategoryID = GetAuditCategory(auditPoint.AuditingCategory.ToString());
                Guid OriginationID   = GetOrigination(auditPoint.OriginationID.ToString());
                Guid StatusID        = GetStatus(auditPoint.ReportedStatus.ToString());

                SqlParameter auditIDParam           = SqlHelper.PrepareParameter("AuditID", Guid.NewGuid(), SqlDbType.UniqueIdentifier, ParameterDirection.InputOutput);
                SqlParameter auditCategoryIDParam   = SqlHelper.PrepareParameter("AuditCategoryID", AuditCategoryID);
                SqlParameter auditCategoryNameParam = SqlHelper.PrepareParameter("AuditCategoryName", auditPoint.AuditingCategory.ToString());
                SqlParameter OriginationIDParam     = SqlHelper.PrepareParameter("OriginationID", OriginationID);
                SqlParameter statusIDParam          = SqlHelper.PrepareParameter("StatusID", StatusID);
                SqlParameter dateTimeStampParam     = SqlHelper.PrepareParameter("DateTimeStamp", auditDateTimeStampSql);
                SqlParameter messageParam           = SqlHelper.PrepareParameter("Message", auditPoint.Message);
                SqlParameter applicationIDParam     = SqlHelper.PrepareParameter("ApplicationID", ApplicationID);
                SqlParameter referenceIDParam       = SqlHelper.PrepareParameter("ReferenceID", referenceID);

                QueueHelper <SortedDictionary <string, string> > queueHelper = new QueueHelper <SortedDictionary <string, string> >();

                SqlParameter parametersDictionary =
                    SqlHelper.PrepareParameter("ParameterDictionary", queueHelper.GetStringMessage(sortedParameters));

                SqlParameter[] parameters = new SqlParameter[] {
                    auditIDParam,
                    auditCategoryIDParam,
                    auditCategoryNameParam,
                    OriginationIDParam,
                    statusIDParam,
                    dateTimeStampParam,
                    messageParam,
                    applicationIDParam,
                    parametersDictionary,
                    referenceIDParam
                };

                using (SqlDataReader reader = _databaseHelper.getDataReaderFromSP(_sqlConnection.ConnectionString,
                                                                                  DatabaseConstants.INSERT_AUDIT,
                                                                                  parameters))
                {
                    returnValue = (Guid)auditIDParam.Value;
                }

                //insert the individual parameters in addition to the dictionary of those parameters
                foreach (KeyValuePair <string, string> kvpParameters in auditPoint.Parameters)
                {
                    SqlParameter[] auditparams = new SqlParameter[] { SqlHelper.PrepareParameter("AuditID", (Guid)auditIDParam.Value)
                                                                      , SqlHelper.PrepareParameter("ParamName", kvpParameters.Key),
                                                                      SqlHelper.PrepareParameter("ParamValue", kvpParameters.Value) };

                    using (SqlDataReader reader = _databaseHelper.getDataReaderFromSP(_sqlConnection.ConnectionString,
                                                                                      DatabaseConstants.INSERT_AUDIT_PARAMETER,
                                                                                      auditparams))
                    {
                        returnValue = (Guid)auditIDParam.Value;
                    }
                }
            }
            catch (Exception ex)
            {
                _log.Fatal(ex);
                throw new ApplicationException("Exception Trying to Write Audit Record to Database", ex);
            }

            return(returnValue);
        }