Example #1
0
        /// <summary>
        /// log a single line of text to the log file
        /// </summary>
        /// <param name="text">text to log</param>
        public static void Log(string text)
        {
            var frame  = new System.Diagnostics.StackTrace().GetFrame(1);
            var method = frame.GetMethod();

            write(string.Format("[{0}] {2}.{3}() [{4}({5},{6})]: {1}", DateTime.Now.ToString("dd.MM.yyyy HH:MM:ss"), text,
                                method.ReflectedType.FullName, method.Name,
                                frame.GetFileName(), frame.GetFileLineNumber(), frame.GetFileColumnNumber()));
        }
            private void ReportException(Exception e)
            {
                var stack = new System.Diagnostics.StackTrace(e).GetFrame(0);
                var line  = stack.GetFileLineNumber();
                var col   = stack.GetFileColumnNumber();
                var file  = stack.GetFileName();

                parent.ErrorMessage = string.Format("行{0,-3} 列{1,-3} 错误CCE0002: 比较器发生异常,源文件是 {2},{3}", line, col, file, e.ToString());
            }
Example #3
0
		public static Error.IError Create(Error.Level level, string title, string message)
		{
			System.Diagnostics.StackFrame frame = new System.Diagnostics.StackTrace().GetFrame(2);
			System.Reflection.MethodBase method = frame.GetMethod();
			Type type = method.DeclaringType;
			System.Reflection.AssemblyName assembly = type.Assembly.GetName();
			return new Entry() {
				Time = DateTime.Now,
				Level = level,
				Title = title,
				Message = message,
				AssemblyName = assembly.Name,
				AssemblyVersion = assembly.Version.ToString(),
				Type = type.FullName,
				Method = method.Name,
				Filename = frame.GetFileName(),
				Line = frame.GetFileLineNumber(),
				Column = frame.GetFileColumnNumber(),
			};
		}
Example #4
0
 /// <summary>
 /// Log the message to the log file, provided the logging was
 /// initialized.
 /// </summary>
 /// <param name="message">The message being logged.</param>
 /// <param name="noTimeStamp">Used to override the default inclusion
 /// of date/time stamp in each logged line.</param>
 /// <param name="staticOutputLocation">Used to set console output to a
 /// fixed location.  This is useful for output such as progress
 /// percentage etc.</param>
 /// <returns>true if logging was successful, false if not.</returns>
 public static bool Log(string message, bool noTimeStamp = false,
                        bool staticOutputLocation        = false, bool includeStackFrame = false,
                        bool overLoaded = false)
 {
     // No logging code should EVER terminate it's parent program
     // through exceptions.
     try
     {
         // Check if caller wants StackFrame info included.
         if (includeStackFrame)
         {
             int frameOffset = 1;
             // Check if an overloaded method is used to call Log().
             if (overLoaded)
             {
                 frameOffset++;
             }
             // Get the StackFrame including FileInfo.
             var stackFrame = new System.Diagnostics.StackTrace(true)
                              .GetFrame(frameOffset);
             // Append the StackFrame info to the message.
             message = string.Format(message +
                                     " --->>> Called from {0} in {1} line {2} column {3}.",
                                     stackFrame.GetMethod().ToString(),
                                     stackFrame.GetFileName(),
                                     stackFrame.GetFileLineNumber(),
                                     stackFrame.GetFileColumnNumber());
         }
         // We use a single & because if we're not logging to the
         // console, it does not matter to what value
         // staticOutputLocation is set.  The use of single & prevents
         // run time evaluation of the value of staticOutputLocation
         // if logToConsole is false thus improving runtime execution
         // performance.
         if (logToConsole & staticOutputLocation)
         {
             // We want static location output so we capture the current
             // cursor location.
             _cursorLeft = Console.CursorLeft;
             _cursorTop  = Console.CursorTop;
         }
         // Write to the log file.
         _writer.WriteLine(prependTimeStamp(message, noTimeStamp));
         // Flush the writer so no messages are stuck in the buffer.
         _writer.Flush();
         // Check if console output is required.
         if (logToConsole)
         {
             // Are we keeping output in a static location?
             if (staticOutputLocation)
             {
                 // Reset the cursor before output.
                 Console.CursorTop  = _cursorTop;
                 Console.CursorLeft = _cursorLeft;
                 // Output the message with a large trailing
                 // blank to clear previous output from the static
                 // console line.
                 Console.WriteLine(
                     prependTimeStamp(message, noTimeStamp) +
                     "                                               " +
                     "                                               " +
                     "                         ");
                 // Reset the cursor after output.
                 Console.CursorTop  = _cursorTop;
                 Console.CursorLeft = _cursorLeft;
             }
             // No static output location is required.
             else
             {
                 // Simply write the message to the console.
                 Console.WriteLine(prependTimeStamp(
                                       message, noTimeStamp));
             }
         }
         if (logToEventLog)
         {
             // Write to Event Log's Application log
             System.Diagnostics.EventLog.WriteEntry("Application",
                                                    prependTimeStamp(message, noTimeStamp),
                                                    logToEventLogEntryType);
         }
     }
     // Something went wrong.
     catch (Exception ex)
     {
         System.Diagnostics.EventLog.WriteEntry("Application",
                                                ex.ToString(), logToEventLogEntryType);
         _outputColor            = Console.ForegroundColor;
         Console.ForegroundColor = ConsoleColor.Red;
         // Check if output is going to the console.
         if (logToConsole)
         {
             // Write console output, stamped if needed.
             Console.WriteLine(
                 prependTimeStamp(ex.ToString(), noTimeStamp));
         }
         // No logging code should EVER terminate it's parent program
         // through exceptions.  Wrap this file output in a try/catch
         // in case file writing throws an exception.
         try
         {
             // Reset the writer to open log file in append mode.
             using (System.IO.StreamWriter writer =
                        new System.IO.StreamWriter(_logPath, true))
             {
                 // Write the exception to the log file, stamped if
                 // needed.
                 writer.WriteLine(
                     prependTimeStamp(ex.ToString(), noTimeStamp));
                 // Flush the file writer buffer.
                 writer.Flush();
             }
         }
         // Have to distinguish between inner and outer exceptions.
         catch (Exception ex2)
         {
             System.Diagnostics.EventLog.WriteEntry("Application",
                                                    ex2.ToString(), logToEventLogEntryType);
             // Write the inner exception error.
             Quix.Core.writeConsoleError(ex2.ToString());
         }
         // Reset the console foreground color.
         Console.ForegroundColor = _outputColor;
         // Return false since something went wrong during logging.
         return(false);
     }
     // Logging succeeded.
     return(true);
 }
Example #5
0
        private void addSpec(string name, Action operation, bool enabled, SpecType specType)
        {
            if (name == String.Empty)
              {
              throw new Exception("Please specify a name for the spec");
              }
              var index = 2;
              var stackFrame = new System.Diagnostics.StackTrace(true).GetFrame(index);
              var codeBase = stackFrame.GetFileName();
              var lineNumber = stackFrame.GetFileLineNumber();
              var columnNumber = stackFrame.GetFileColumnNumber();
              var fileName = Path.GetFileName(codeBase);
              var className = this.GetType().FullName;
              //var className = this.GetType().Name;

              var spec = Registry.SpecFactory(name, operation, Registry.CurrentSuite, codeBase, lineNumber, columnNumber,
              fileName, className);
              spec.Enabled = enabled && spec.Parent.Enabled;
              Registry.CurrentSuite.AddChild(spec);
        }