示例#1
0
        private void LogEvent(string sText, eloglevel loglevel)
        {
            EventLogEntryType EventType;

            switch (loglevel)
            {
            case eloglevel.error:
                EventType = EventLogEntryType.Error;
                break;

            case eloglevel.warn:
                EventType = EventLogEntryType.Warning;
                break;

            case eloglevel.info:
                EventType = EventLogEntryType.Information;
                break;

            default:
                EventType = EventLogEntryType.Information;
                break;
            }
            //open and write to event log.
            System.Diagnostics.EventLog oEV = new System.Diagnostics.EventLog();
            oEV.Source = m_ProcessName;
            oEV.WriteEntry(sText, EventType);
            oEV.Close();
        }
示例#2
0
 public static void UseSensibleDefaults(eloglevel logLevel)
 {
     /*
      * Uses default values for the log filename and path.
      *
      */
     string[] logFile = DefaultLogFileAndLocation();
     UseSensibleDefaults(logFile[1], logFile[0], logLevel);
 }
示例#3
0
 public static void LogThis(string logtext, eloglevel loglevel, elogprefix logprefix)
 {
     if (m_DefaultToPrimaryProfile)
     {
         LogThis(elogprofile.primary, logtext, loglevel, logprefix);
     }
     else             //log to current profile
     {
         m_LogMethods.LogThis(logtext, loglevel, logprefix);
     }
 }
示例#4
0
        public void LogThis(string logtext, eloglevel loglevel, elogprefix logprefix)
        {
            if (m_loglevel >= loglevel)
            {
                string sFilePath = LogPath;
                if (sFilePath == "")
                {
                    SetLogPath();
                    sFilePath = LogPath;
                }
                TruncateLogFile(sFilePath);
                DateTime dt = DateTime.Now;
                switch (logprefix)
                {
                case elogprefix.dt:
                    logtext = dt.ToString("yyyy.MM.dd") + "-" + dt.ToString("hh.mm.ss") + ": " + logtext;
                    break;

                case elogprefix.loglevel:
                    logtext = loglevel.ToString() + ": " + logtext;
                    break;

                case elogprefix.dt_loglevel:
                    logtext = dt.ToString("yyyy.MM.dd") + "-" + dt.ToString("hh.mm.ss") + ":" + loglevel + ": " + logtext;
                    break;
                }

                //log it
                switch (m_logwhere)
                {
                case elogwhere.file_and_console:
                    LogConsole(logtext);
                    AppendToFile(sFilePath, logtext);
                    break;

                case elogwhere.file:
                    AppendToFile(sFilePath, logtext);
                    break;

                case elogwhere.eventlog:
                    LogEvent(logtext, loglevel);
                    break;

                case elogwhere.eventlog_and_file:
                    AppendToFile(sFilePath, logtext);
                    LogEvent(logtext, loglevel);
                    break;
                }
            }
        }
示例#5
0
 public static void LogThis(elogprofile logprofile, string logtext, eloglevel loglevel, elogprefix logprefix)
 {
     ((LogMethods)m_Logs[Convert.ToString(logprofile)]).LogThis(logtext, loglevel, logprefix);
 }
示例#6
0
        public static void UseSensibleDefaults(string logFileName, string logLocation, eloglevel logLevel)
        {
            //create an instance of Log()
            Log Mylog = new Log();

            //init the profiles you intend to use.

            /*
             *      The profile concept is the only complicated thing about LogThis and unless
             *      you have some need to log in multiple different ways *in the same project*
             *      then just default to the primary profile.  All the settings you see below are
             *      configuring the primary profile.  To have multiple profiles you would
             *      call Mylog.Init(elogprofile.your_made_up_profile) and then set these same
             *      settings for that profile also.
             *
             *      One possible scenerio for using multiple profiles might be having different
             *      parts of the code log to different files with different quota settings
             *      or one part to a file and the other part to the event log.
             *      It's a lame example but you get the gist hopefully.
             */
            Mylog.Init();              //This is the same as Mylog.Init(elogprofile.primary) .

            /*	You only need the Mylog instance just long enough to run Init.  Now
             *      you can just use the static methods to perform all logging actions,
             *      which is the most of the reason I wrote LogThis.
             */
            Mylog = null;

            /*
             *      Set the properties for the primary log profile.
             *      Log.Profile sets the *active* profile, and it remains the active profile
             *      unless you set it explicitly.  Any calls to Log.LogThis or any other Log
             *      methods will use the active profile.
             */
            Log.Profile = elogprofile.primary;
            //I prefer a simple text file log but you can change this
            //to use the event log also.
            Log.LogWhere = elogwhere.file;

            if (Convert.ToInt32(Log.LogLevel) < 1)
            {
                Log.LogLevel = eloglevel.error;                 //errors show at the minimum.
            }
            else
            {
                Log.LogLevel = logLevel;
            }
            string[] fileDefaults = DefaultLogFileAndLocation();
            if (logFileName == string.Empty)
            {
                Log.LogName = fileDefaults[1];
            }
            else
            {
                Log.LogName = logFileName;
            }

            if (logLocation == string.Empty)
            {
                Log.LogBasePath = fileDefaults[0];
            }
            else
            {
                Log.LogBasePath = logLocation;
            }

            //evaluate the quota size as meaning kbytes.
            Log.LogQuotaFormat = elogquotaformat.kbytes;
            //So, max logfile size = (Log.LogQuotaFormat * Log.LogSizeMax)
            Log.LogSizeMax = 100;
            //Every Log.LogPeriod the log file will roll over to a new file.
            Log.LogPeriod = elogperiod.month;
            //The log filename will be formatted this way.
            Log.LogNameFormat = elognameformat.date_name;
            Log.SetLogPath();

            /*
             *      ok, all things required as init configuration have now
             *      been set.  Simply call Log.LogThis or Log.LogHeader
             *      anywhere in your code.
             *
             *                              Example:
             *
             *      Log.LogHeader(Log.LogName + ": Starting up",elogheaderlevel.Level_1);
             *      ...
             *      Log.LogThis("Main: " + e.Message,eloglevel.error);
             *      ...
             *      Log.LogHeader(Log.LogName + ": Exiting",elogheaderlevel.Level_1);
             */
        }
示例#7
0
 public void LogThis(string logtext, eloglevel loglevel)
 {
     LogThis(logtext, loglevel, m_logprefix);
 }
示例#8
0
        public void LogThis(string logtext,eloglevel loglevel, elogprefix logprefix)
        {
            if (m_loglevel >= loglevel)
            {

                string sFilePath = LogPath;
                if (sFilePath == "" )
                {
                    SetLogPath();
                    sFilePath = LogPath;
                }
                TruncateLogFile(sFilePath);
                DateTime dt = DateTime.Now;
                switch (logprefix)
                {
                    case elogprefix.dt:
                        logtext = dt.ToString("yyyy.MM.dd") + "-" + dt.ToString("hh.mm.ss") + ": " + logtext;
                        break;
                    case elogprefix.loglevel:
                        logtext = loglevel.ToString() + ": " + logtext;
                        break;
                    case elogprefix.dt_loglevel:
                        logtext = dt.ToString("yyyy.MM.dd") + "-" + dt.ToString("hh.mm.ss") + ":" + loglevel + ": " + logtext;
                        break;
                }

                //log it
                switch (m_logwhere)
                {
                    case elogwhere.file:
                        AppendToFile(sFilePath,logtext);
                        break;
                    case elogwhere.eventlog:
                        LogEvent(logtext,loglevel);
                        break;
                    case elogwhere.eventlog_and_file:
                        AppendToFile(sFilePath,logtext);
                        LogEvent(logtext,loglevel);
                        break;
                }

            }
        }
示例#9
0
 public void LogThis(string logtext,eloglevel loglevel)
 {
     LogThis(logtext, loglevel, m_logprefix);
 }
示例#10
0
        public static void UseSensibleDefaults(string logFileName, string logLocation, eloglevel logLevel)
        {
            //create an instance of Log()
            Log Mylog = new Log();
            //init the profiles you intend to use.
            /*
                The profile concept is the only complicated thing about LogThis and unless
                you have some need to log in multiple different ways *in the same project*
                then just default to the primary profile.  All the settings you see below are
                configuring the primary profile.  To have multiple profiles you would
                call Mylog.Init(elogprofile.your_made_up_profile) and then set these same
                settings for that profile also.

                One possible scenerio for using multiple profiles might be having different
                parts of the code log to different files with different quota settings
                or one part to a file and the other part to the event log.
                It's a lame example but you get the gist hopefully.
            */
            Mylog.Init();  //This is the same as Mylog.Init(elogprofile.primary) .
            /*	You only need the Mylog instance just long enough to run Init.  Now
                you can just use the static methods to perform all logging actions,
                which is the most of the reason I wrote LogThis.
            */
            Mylog = null;
            /*
                Set the properties for the primary log profile.
                Log.Profile sets the *active* profile, and it remains the active profile
                unless you set it explicitly.  Any calls to Log.LogThis or any other Log
                methods will use the active profile.
            */
            Log.Profile = elogprofile.primary;
            //I prefer a simple text file log but you can change this
            //to use the event log also.
            Log.LogWhere = elogwhere.file;

            if (Convert.ToInt32(Log.LogLevel) < 1)
            {
                Log.LogLevel = eloglevel.error; //errors show at the minimum.
            }
            else
            {
                Log.LogLevel = logLevel;
            }
            string[] fileDefaults = DefaultLogFileAndLocation();
            if (logFileName == string.Empty)
                Log.LogName = fileDefaults[1];
            else
                Log.LogName = logFileName;

            if (logLocation == string.Empty)
                Log.LogBasePath = fileDefaults[0];
            else
                Log.LogBasePath = logLocation;

            //evaluate the quota size as meaning kbytes.
            Log.LogQuotaFormat = elogquotaformat.kbytes;
            //So, max logfile size = (Log.LogQuotaFormat * Log.LogSizeMax)
            Log.LogSizeMax = 100;
            //Every Log.LogPeriod the log file will roll over to a new file.
            Log.LogPeriod = elogperiod.month;
            //The log filename will be formatted this way.
            Log.LogNameFormat = elognameformat.date_name;
            Log.SetLogPath();
            /*
                ok, all things required as init configuration have now
                been set.  Simply call Log.LogThis or Log.LogHeader
                anywhere in your code.

                            Example:

                Log.LogHeader(Log.LogName + ": Starting up",elogheaderlevel.Level_1);
                ...
                Log.LogThis("Main: " + e.Message,eloglevel.error);
                ...
                Log.LogHeader(Log.LogName + ": Exiting",elogheaderlevel.Level_1);
            */
        }
示例#11
0
 public static void UseSensibleDefaults(eloglevel logLevel)
 {
     /*
      * Uses default values for the log filename and path.
      *
     */
     string[] logFile = DefaultLogFileAndLocation();
     UseSensibleDefaults(logFile[1],logFile[0],logLevel);
 }
示例#12
0
 public static void LogThis(string logtext,eloglevel loglevel, elogprefix logprefix)
 {
     if (m_DefaultToPrimaryProfile)
     {
         LogThis(elogprofile.primary,logtext,loglevel,logprefix);
     }
     else //log to current profile
     {
         m_LogMethods.LogThis(logtext,loglevel, logprefix);
     }
 }
示例#13
0
 public static void LogThis(elogprofile logprofile, string logtext,eloglevel loglevel, elogprefix logprefix)
 {
     ((LogMethods)m_Logs[Convert.ToString(logprofile)]).LogThis(logtext,loglevel,logprefix);
 }
示例#14
0
 private void LogEvent(string sText, eloglevel loglevel)
 {
     EventLogEntryType EventType;
     switch(loglevel)
     {
         case eloglevel.error:
             EventType = EventLogEntryType.Error;
             break;
         case eloglevel.warn:
             EventType = EventLogEntryType.Warning;
             break;
         case eloglevel.info:
             EventType = EventLogEntryType.Information;
             break;
         default:
             EventType = EventLogEntryType.Information;
             break;
     }
     //open and write to event log.
     System.Diagnostics.EventLog oEV = new System.Diagnostics.EventLog();
     oEV.Source = m_ProcessName;
     oEV.WriteEntry (sText, EventType);
     oEV.Close();
 }
示例#15
0
        /// <summary>
        /// Outputs the message to the log file.
        /// If 'showMsgBox' is set to true, then the message box will be shown.
        /// </summary>
        /// <param name="showMsgBox">if set to <c>true</c> [show message box].</param>
        /// <param name="level">The level to be shown.</param>
        /// <param name="messages">The messages.</param>
        public static void OutputMessage(bool showMsgBox, MsgLevel level, string location, params string[] messages)
        {
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(location))
            {
                sb.Append("[Location: ");
                sb.Append(location);
                sb.Append("] ");
            }

            foreach (string s in messages)
            {
                sb.Append(s);
                sb.Append(", ");
            }

            string message = string.Empty;

            if (sb.Length > 0)
            {
                // Removes all trailing occurrences of a set of characters
                // specified in an array from the current String object.
                message = sb.ToString().TrimEnd(',', ' ');
            }

            eloglevel      logLevel   = eloglevel.info;
            string         caption    = string.Empty;
            MessageBoxIcon msgBoxIcon = MessageBoxIcon.Information;

            switch (level)
            {
            case MsgLevel.Debug:
                logLevel   = eloglevel.info;
                caption    = "Debug Message";
                msgBoxIcon = MessageBoxIcon.Information;
                break;

            case MsgLevel.Error:
                logLevel   = eloglevel.error;
                caption    = "Error Message";
                msgBoxIcon = MessageBoxIcon.Error;
                break;

            case MsgLevel.Warn:
                logLevel   = eloglevel.warn;
                caption    = "Warn Message";
                msgBoxIcon = MessageBoxIcon.Warning;
                break;

            case MsgLevel.Info:
                logLevel   = eloglevel.info;
                caption    = "Information";
                msgBoxIcon = MessageBoxIcon.Information;
                break;

            default:
                break;
            }

            // Writes the message to the log file.
            Log.LogThis(message, logLevel);

            if (showMsgBox)
            {
                // Displays a message box with specified text, caption, buttons, and icon.
                MessageBox.Show(message, caption, MessageBoxButtons.OK, msgBoxIcon);
            }

            if (level == MsgLevel.Debug)
            {
                // Writes a message followed by a line terminator to
                // the trace listeners in the Listeners collection.
                Debug.WriteLine(message);
            }
        }