public void WriteEntry(EventLogEntryType type, string message, string details = "", string clientAlias = "", bool supportAlert = false)
        {
            //if client alias sent in then add it to the message
            if (string.IsNullOrEmpty(clientAlias)) clientAlias = "";
            else details += "\nClientAlias = " + clientAlias;

            //get the request method and parameters
            details += GetSuffix();

            var error = new BoostError
                {
                    Time = DateTime.Now,
                    Message = message,
                    Details = details,
                    Type = type,
                    Alias = clientAlias,
                    SupportAlert = supportAlert
                };
            ThreadPool.QueueUserWorkItem(e => WriteEntry((BoostError) e), error);

            Debug.WriteLine(message);
        }
        //private Error Log function to be run in a separate thread
        private void WriteEntry(BoostError error)
        {
            if (error == null || error.Message.Length < 1) return; //nothing to do

            //truncate if too long
            const int logLen = 10000;
            error.Message = CheckLength(error.Message, logLen);
            error.Details = CheckLength(error.Details, logLen);

            if ((_logBlockList != null) && (!string.IsNullOrEmpty(error.Alias)))
            {
                //block this message from the log (if any clients are generating too many errors)
                if (_logBlockList.Any(alias => alias.Equals(error.Alias)))
                    return;
            }

            lock (_errWriteLock)
            {
                //log all messages (unless blocked above)
                if (_eventLog != null)
                    _eventLog.WriteEntry(error.Message + Environment.NewLine + error.Details, error.Type);

                //email certain messages using ClientPOC settings per user
                List<UserContact> users = null;
                var subject = "";
                var sendAdmin = true;
                //var sendClient = false;
                switch (error.Type)
                {
                    case EventLogEntryType.Error:
                        subject = "Error";
                        sendAdmin = ((int) _adminReportType <= (int) (ReportType.ServiceError));
                        if (Subscriptions != null)
                            users = Subscriptions.GetUsers(error.Alias, ReportType.ServiceError);
            #if !USAGE_READONLY
                        if (_dataLog != null)
                        {
                            ThreadPool.QueueUserWorkItem(a => _dataLog.AddError((string) a), error.Alias); //add to error tally
                            ThreadPool.QueueUserWorkItem(e => _dataLog.SetLastError((BoostError) e), error); //replace last error
                        }
            #endif
                        break;
                    case EventLogEntryType.Warning:
                        subject = "Warning";
                        sendAdmin = ((int) _adminReportType <= (int) (ReportType.ServiceWarning));
                        if (Subscriptions != null)
                            users = Subscriptions.GetUsers(error.Alias, ReportType.ServiceWarning);
            #if !USAGE_READONLY
                        if (_dataLog != null)
                        {
                            ThreadPool.QueueUserWorkItem(a => _dataLog.AddWarning((string) a), error.Alias); //add to Warning tally
                            ThreadPool.QueueUserWorkItem(e => _dataLog.SetLastError((BoostError) e), error); //replace last error
                        }
            #endif
                        break;
                    case EventLogEntryType.Information:
                        subject = "Status Update";
                        sendAdmin = ((int) _adminReportType <= (int) (ReportType.ServiceInfo));
                        if (Subscriptions != null)
                            users = Subscriptions.GetUsers(error.Alias, ReportType.ServiceInfo);
                        break;
                    default:
                        subject = "Unknown EventType";
                        sendAdmin = true;
                        break;
                }

                if (users != null && users.Any())
                {
                    const string preMessage = "This is an auto-generated email from the 4-Tell Boost service."
                                              + "If you would rather not receive these email notices, please adjust "
                                              + "your configuration settings or contact us at [email protected]\n\n";
                    foreach (var user in users)
                    {
                        try
                        {
            #if !CART_EXTRACTOR_TEST_SITE
                            GmailMessage.SendFromGmail(_gmailUsername, _gmailPassword, user.Email,
                                                             subject, preMessage + error.Message, ServerId, true);
            #endif
                        }
                        catch (Exception ex)
                        {
                            var errMsg = string.Format("Error sending email to {0} <{1}>\n{2}\n\nOriginal message to send:\n{3}{4}",
                                                       user.Name, user.Email, ex.Message, preMessage, error.Message);
                            if (_eventLog != null)
                                _eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
                        }
                    }

                    //always send admin messages that are sent to clients
                    var emailList = "";
                    var first = true;
                    foreach (var user in users)
                    {
                        if (first) first = false;
                        else emailList += ", ";
                        emailList += string.Format("{0} <{1}>", user.Name, user.Email);
                    }
                    error.Message += "\n\nThis message was emailed to: " + emailList;
                    sendAdmin = true;
                }

                subject = string.Format("{0}{1}", string.IsNullOrEmpty(error.Alias) ? "" : error.Alias + " ", subject);
                if (sendAdmin && !string.IsNullOrEmpty(_gmailToAddress))
                {
                    try
                    {
            #if !CART_EXTRACTOR_TEST_SITE
                        GmailMessage.SendFromGmail(_gmailUsername, _gmailPassword, _gmailToAddress,
                                                         subject, error.Message, ServerId, true);
            #endif
                    }
                    catch (Exception ex)
                    {
                        var errMsg = string.Format("Error sending email to {0}\n{1}\n\nOriginal message to send:\n{2}",
                                                                             _gmailToAddress, ex.Message, error.Message);
                        if (_eventLog != null)
                            _eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
                    }
                }
                if (error.SupportAlert)
                {
                    try
                    {
                        var errMsg = string.Format("{0}\n\n{1}", error.Message, error.Details);
            #if !CART_EXTRACTOR_TEST_SITE
                        GmailMessage.SendFromGmail(_gmailUsername, _gmailPassword, _supportAddress,
                                                                                         subject, errMsg, ServerId, true);
            #endif
                    }
                    catch (Exception ex)
                    {
                        var errMsg = string.Format("Error sending email to {0}\n{1}\n\nOriginal message to send:\n{2}",
                                                                             _supportAddress, ex.Message, error.Message);
                        if (_eventLog != null)
                            _eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
                    }
                }
            } //end errWritesLock
        }
 public BoostErrorExternal(BoostError error)
 {
     Alias = error.Alias;
     Message = error.Message;
     Type = error.Type;
     Time = error.Time;
 }