Ejemplo n.º 1
0
        /// <summary>
        /// Handle a request to log an event. This method is private to
        /// standardize the layout of the stack frames.
        /// </summary>
        private static void privLog(int severity, String msg, bool isLoggingException)
        {
            // Return early if we are not logging.
            if (m_loggingLevel == LoggingLevel.None)
            {
                return;
            }

            // Generate the log event.
            String callerStr    = "Unknown";
            String lineStr      = "?";
            String callStackStr = "";

            if (m_loggingLevel == LoggingLevel.Debug)
            {
                FormatStackFrame(new StackFrame(2, true), out callerStr, out lineStr);
                StackTrace st = new StackTrace(true);
                for (int i = 2; i < st.FrameCount; i++)
                {
                    String name, line;
                    FormatStackFrame(st.GetFrame(i), out name, out line);
                    callStackStr += name + " at line " + line + Environment.NewLine;
                }
            }

            LogEventArgs evt = new LogEventArgs(severity, callerStr, callStackStr, lineStr, msg,
                                                DateTime.Now.ToLocalTime());

            // For ordering and consistency purpose the following has to be
            // done in mutual exclusion.
            lock (Mutex)
            {
                // Overflow, remove some events.
                if (m_evtList.Count >= m_maxNbEvt)
                {
                    m_evtList.RemoveRange(0, m_evtList.Count / 2);
                }

                // Add the event to the list.
                m_evtList.Add(evt);

#if DEBUG
                Debug.WriteLine(evt.Timestamp.ToString("") + " | " + evt.Severity + " | " +
                                evt.Caller + "::line " + evt.Line + " | " + evt.Message);
#endif

                // Dispatch the event to the listeners.
                if (m_onLogEvent != null && !m_firingFlag)
                {
                    try
                    {
                        m_firingFlag = true;
                        m_onLogEvent(null, evt);
                        m_firingFlag = false;
                    }

                    catch (Exception ex)
                    {
                        Base.HandleError(ex.Message, true);
                    }
                }
            }
        }