예제 #1
0
        /*
         * EventID conventions
         * 0    - service manager messages
         * 1xxx - general messages
         * 2xxx - file events
         * 3xxx - recoverable errors/warnings
         * 4xxx - fatal errors
         * 5xxx - email notification events
         * 9xxx - debugging messages
         */
        /// <summary>
        /// write log message
        /// </summary>
        /// <param name="message">text of message</param>
        /// <param name="eventID"></param>
        /// <param name="type"></param>
        public void Write(string message, int eventID, EventLogEntryType type)
        {
            if (eventID >= 9000 && !Debug)  /* only log debugging messages if Debug is true */
            {
                return;
            }

            string entry = string.Format("{0} {1}\t{2} {3} {4}\n",
                                         DateTime.Now.ToShortDateString(),
                                         DateTime.Now.ToLongTimeString(),
                                         eventID,
                                         EventTypeString(type),
                                         message);

            if (NotifyOnMessages != null)  // don't attempt check for notificatons if the list has not been built
            {                              //    this may be the case for early messages on startup
                if (NotifyOnMessages.Contains(eventID))
                {
                    if (mailer != null)
                    {
                        mailer.notify(entry);
                    }
                    else
                    {
                        WriteEventLog(EventSource, "RCheckd attempted to send an email but does not have a proper mail configuration", EventLogEntryType.Error, 5901);
                    }
                }
            }


            if (Logfile.ToLower() == "eventlog")
            {
                WriteEventLog(EventSource, message, type, eventID);
            }
            else
            {
                if (Logfile.ToLower() == "console")
                {
                    Console.Write(entry);
                }
                else
                {
                    //  TODO - check to see if directory needs to be created
                    FileInfo fi = new FileInfo(Logfile);
                    if (!fi.Exists) // new log file, write a header
                    {
                        File.WriteAllText(Logfile, string.Format("{0} {1}\t{2} {3} {4}\n", "Date     ", "Time   ", "Event", "Type", "Message"));
                    }

                    System.IO.File.AppendAllText(Logfile, entry);
                }
            }
        }