//add a client to the list of client logs
 public void AddClient(string alias, ClientPOC poc = null, StreamReader file = null)
 {
     ClientLog client;
     GetClient(alias, out client); //if already in list, use ref to existing otherwise add
     if (poc != null)
         client.POC = poc; //set poc
     if (file != null)
         client.ReadUsage(file); //read usage from log file
 }
        //main constructor
        public BoostLog(string source)
        {
            //TODO: read client log data from file so accumulation can continue

            string logMsg = "4-Tell Event Log started for " + source;
            try
            {
                //start logging to system event log
                m_eventLog = new EventLog("4-Tell");
                m_eventLog.Source = source;

                //setup the data path
                //DataPath path = DataPath.Instance;
                //m_dataPath = path.Root;
                m_dataPath = DataPath.Instance.Root;

                //Read Gmail settings from web.config
                m_boostVersion = ConfigurationManager.AppSettings.Get("BoostVersion");
                m_gmailUsername = ConfigurationManager.AppSettings.Get("GmailUsername");
                m_gmailPassword = ConfigurationManager.AppSettings.Get("GmailPassword");
                m_gmailToAddress = ConfigurationManager.AppSettings.Get("GmailToAdress");
                string level = ConfigurationManager.AppSettings.Get("AdminReportLevel");
                m_adminReportLevel = GetReportLevel(level);
                level = ConfigurationManager.AppSettings.Get("CustomerReportLevel");
                m_clientReportLevel = GetReportLevel(level);
                ClientPOC admin = new ClientPOC();
                admin.Name = "Admin";
                admin.Email = m_gmailToAddress;
                admin.Report = m_adminReportLevel;
                AddClient("4Tell", admin); //create 4-tell client to record errors that don't relate to a client

                //usage log settings
                ReadUsage(); //Try to read past usage data from log
                CheckDate(); //See if we need to move to a new day or new year
                //set a timer to check the date/time each hour and update accumulator indices
                m_dateCheckTimer.Interval = 60 * 60 * 1000; // converted to ms
                m_dateCheckTimer.Elapsed += new ElapsedEventHandler(OnDateCheckTimer);
                m_dateCheckTimer.Enabled = true;

                //get value (in minutes) for transaction log write timer
                string logFrequency = ConfigurationManager.AppSettings.Get("LogFrequency");
                int frequency;
                try
                {
                    frequency = Convert.ToInt32(logFrequency);
                }
                catch
                {
                    WriteEntry("Warning--Illegal timer frequency: " + logFrequency, EventLogEntryType.Warning);
                    frequency = 15; //default
                }
                m_transactionWriteTimer.Interval = frequency * 60 * 1000; //converted to ms
                m_transactionWriteTimer.Elapsed += new ElapsedEventHandler(OnTransactionWriteTimer);
                m_transactionWriteTimer.Enabled = true;

                //Block logging for certain clients
                string blockList = ConfigurationManager.AppSettings.Get("LogBlockList");
                if (blockList != null)
                {
                    logMsg += "\nLogging will be blocked for the following clients:\n";
                    m_logBlockList = blockList.Split(',',' '); //convert comma separated list to array
                    foreach (string alias in m_logBlockList)
                        logMsg += alias + "\n";
                }

                WriteEntry(logMsg, EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                string errMsg = "Initialization Error for " + source + ": " + ex.Message;
                if (ex.InnerException != null)
                    errMsg += "\nInner Exception: " + ex.InnerException.Message;
                if (m_eventLog != null)
                    m_eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
            }
        }
 public ClientLog(string alias)
 {
     Alias = alias;
     POC = new ClientPOC();
     LastUpload = DateTime.MinValue;
     LastGenerator = DateTime.MinValue;
     LastError = new BoostError();
     Calls = new Accumulator(alias, "Calls");
     Errors = new Accumulator(alias, "Errors");
     Warnings = new Accumulator(alias, "Warns");
     ServiceCalls = new ServiceCallLog(alias);
     Actions = new ActionsLog(alias);
     Clicks = new ClickStreamLog(alias);
 }
        //private Error Log function to be run in a separate thread
        private void WriteEntry(BoostError error)
        {
            if (error.Message.Length < 1)
                return; //nothing to do

            bool blockLog = false;
            ClientPOC poc = new ClientPOC();
            if ((error.Alias != null) && (error.Alias.Length > 0))
            {
                foreach (ClientLog c in m_clients)
                    if (c.Alias.Equals(error.Alias))
                    {
                        poc = c.POC;
                        break;
                    }

                if (m_logBlockList != null)
                {
                    //block this message from the log (if any clients are generating too many errors)
                    foreach (string alias in m_logBlockList)
                        if (alias.Equals(error.Alias))
                        {
                            blockLog = true;
                            break;
                        }
                }
            }

            lock (m_errWriteLock)
            {
                //log all messages (unless blocked above)
                if (!blockLog && (m_eventLog != null))
                    m_eventLog.WriteEntry(error.Message, error.Type);

                //email certain messages
                //NOTE: no longer using m_clientReportLevel ---using ClientPOC from ConfigBoost
                string subject = "";
                bool sendAdmin = true;
                bool sendClient = false;
                switch (error.Type)
                {
                    case EventLogEntryType.Error:
                        subject = "Error";
                        sendAdmin = ((int)m_adminReportLevel <= (int)(ReportLevel.Error));
                        sendClient = ((int)(poc.Report) <= (int)(ReportLevel.Error));
                        AddError(error.Alias); //add to error tally
                        AddLastError(error);	//replace last error
                        break;
                    case EventLogEntryType.Warning:
                        subject = "Warning";
                        sendAdmin = ((int)m_adminReportLevel <= (int)(ReportLevel.Warrning));
                        sendClient = ((int)(poc.Report) <= (int)(ReportLevel.Warrning));
                        AddWarning(error.Alias); //add to warning tally
                        AddLastError(error);	//replace last error
                        break;
                    case EventLogEntryType.Information:
                        subject = "Status Update";
                        sendAdmin = ((int)m_adminReportLevel <= (int)(ReportLevel.Information));
                        sendClient = ((int)(poc.Report) <= (int)(ReportLevel.Information));
                        break;
                    default:
                        subject = "Unknown EventType";
                        sendAdmin = true;
                        sendClient = false;
                        break;
                }

                if (sendClient && (poc.Email.Length > 0))
                {
                    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";
                    try
                    {
                        Gmail.GmailMessage.SendFromGmail(m_gmailUsername, m_gmailPassword, poc.Email,
                                                                                            subject, preMessage + error.Message, m_ServerId, true);
                    }
                    catch (Exception ex)
                    {
                        string errMsg = "Error sending email to " + poc.Name + " <" + poc.Email + ">\n"
                            + ex.Message + "\n\nOriginal message to send:\n" + preMessage + error.Message;
                        if (!blockLog && (m_eventLog != null))
                            m_eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
                    }

                    //always send admin messages that are sent to clients
                    error.Message += "\n\nThis message was emailed to client: " + poc.Name + " <" + poc.Email + ">";
                    sendAdmin = true;
                }

                if (sendAdmin && (m_gmailToAddress.Length > 0))
                {
                    try
                    {
                        Gmail.GmailMessage.SendFromGmail(m_gmailUsername, m_gmailPassword, m_gmailToAddress,
                                                                                            subject, error.Message, m_ServerId, true);
                    }
                    catch (Exception ex)
                    {
                        string errMsg = "Error sending email to " + m_gmailToAddress + "\n"
                            + ex.Message + "\n\nOriginal message to send:\n" + error.Message;
                        if (!blockLog && (m_eventLog != null))
                            m_eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
                    }
                }
            } //end errWritesLock
        }
        //main constructor is private because BoostLog is a singleton
        //use BoostLog.Instance instead of new BoostLog()
        private BoostLog()
        {
            string source = "4-Tell Boost";

            try
            {
                //start logging to system event log
                source = ConfigurationManager.AppSettings.Get("LogSource");
                if (source == null) source = "Unidentified";
                string logMsg = "4-Tell Event Log started for " + source;
                m_eventLog = new EventLog("4-Tell");
                m_eventLog.Source = source;

                //log any replicator startup issues
                m_replicator = Replicator.Instance;
                if (m_replicator.ErrorText.Length > 0)
                    WriteEntry(m_replicator.ErrorText, EventLogEntryType.Warning);
                if (m_replicator.DebugText.Length > 0)
                    WriteEntry(m_replicator.DebugText, EventLogEntryType.Information);
                m_replicator.ErrorText = "";
                m_replicator.DebugText = "";

                //Read Gmail settings from web.config
                m_gmailUsername = ConfigurationManager.AppSettings.Get("GmailUsername");
                m_gmailPassword = ConfigurationManager.AppSettings.Get("GmailPassword");
                m_gmailToAddress = ConfigurationManager.AppSettings.Get("GmailToAdress");
                string level = ConfigurationManager.AppSettings.Get("AdminReportLevel");
                m_adminReportLevel = GetReportLevel(level);
                level = ConfigurationManager.AppSettings.Get("CustomerReportLevel");
                m_clientReportLevel = GetReportLevel(level);
                ClientPOC admin = new ClientPOC();
                admin.Name = "Admin";
                admin.Email = m_gmailToAddress;
                admin.Report = m_adminReportLevel;

                //log any usage log issues
                m_usageLog = UsageLog.Instance;
                if (m_usageLog.ErrorText.Length > 0)
                    WriteEntry(m_usageLog.ErrorText, EventLogEntryType.Warning);
                m_usageLog.ErrorText = "";
                m_usageLog.AddClient("4Tell", admin); //create 4-tell client to record errors that don't relate to a client

                //Block logging for certain clients
                string blockList = ConfigurationManager.AppSettings.Get("LogBlockList");
                if ((blockList != null) && (blockList.Length > 0))
                {
                    logMsg += "\nLogging will be blocked for the following clients:\n";
                    m_logBlockList = blockList.Split(',',' '); //convert comma separated list to array
                    foreach (string alias in m_logBlockList)
                        logMsg += alias + "\n";
                }

                WriteEntry(logMsg, EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                string errMsg = "Initialization Error for " + source + " Log: " + ex.Message;
                if (ex.InnerException != null)
                    errMsg += "\nInner Exception: " + ex.InnerException.Message;
                if (m_eventLog != null)
                    m_eventLog.WriteEntry(errMsg, EventLogEntryType.Error);
            }
        }