コード例 #1
0
ファイル: Logger.cs プロジェクト: JackieWang/Exercise.WP7
 public static void Log(LogLevel level, string message)
 {
     Write(level, message, null);
 }
コード例 #2
0
ファイル: Logger.cs プロジェクト: JackieWang/Exercise.WP7
        /// <summary>
        /// Writes a composite format string to the text stream.
        /// </summary>
        /// <param name="level">Log level</param>
        /// <param name="message">A composite format string.</param>
        /// <param name="args">Arguments to the message.</param>
		//should change this function name with LogToFile
        private static void Write(LogLevel level, string message, object[] args)
        {
			//FIXME::
			//no resource sharing, no need to use mutex
            //myMutex.WaitOne();

            if (level < LogLevel)
            {
                myMutex.ReleaseMutex();
                return;
            }

            if (string.IsNullOrEmpty(LogFile) && !LogToConsole && !LogToConsoleError && LogWriter == null)
            {
                myMutex.ReleaseMutex();
                return;
            }

            try
            {
                StackFrame frame = new StackTrace().GetFrame(2);
				//FIXME::
				//local variable not comply with naming rules
                string _function_ = frame.GetMethod().DeclaringType.FullName;

                //StackFrame sf = new StackFrame(true);

                //StackTrace st = new StackTrace(new StackFrame(true));
                //string _line_ = st.GetFrame(2).GetFileLineNumber().ToString();
                //string _file_ = st.GetFrame(2).GetFileName();
       
                string formattedMessage = message;
                if (args != null)
                {
                    formattedMessage = string.Format(CultureInfo.InvariantCulture, message, args);
                }

                var builder = new StringBuilder(message.Length + 32);
                if (IncludeTimestamp)
                {
                    builder.Append(CurrentTimeGetter.Now.ToString("[yyyy-MM-dd HH:mm:ss.ffff]", CultureInfo.InvariantCulture));
                }

                if (IncludeLevel)
                {
                    builder.Append("[");
                    builder.Append(level.ToString());
                    builder.Append("]");
                }

                if (IncludeFunction)
                {
                    builder.Append("[");
                    builder.Append(_function_);
                    builder.Append("] ");
                }

                builder.Append(formattedMessage);
                string msg = builder.ToString();

                // log to file
                LogToFile(msg);

                myMutex.WaitOne();
                
                // log to LogWriter
                var writer = LogWriter;
                if (writer != null)
                {                                      
                    writer.WriteLine(msg);                    
                }

                // log to console
                if (LogToConsole)
                {
                    Console.WriteLine(msg);
                }

                // log to console error
                if (LogToConsoleError)
                {
                    Console.Error.WriteLine(msg);
                }
                
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.Message);
                // we have no place to log the message to so we ignore it
            }

            myMutex.ReleaseMutex();
        }
コード例 #3
0
ファイル: Logger.cs プロジェクト: JackieWang/Exercise.WP7
 public static void Log(LogLevel level, string message, params object[] args)
 {
     Write(level, message, args);
 }