Ejemplo n.º 1
0
        /// <summary><para>
        /// Several routines within the trace implementation require that the line and module and method information are
        /// generated for the statement that they are on.  This routine generates a stack trace from its current location
        /// then walks back up the trace until it finds the first stack frame that is not related to the trace library.
        /// this frame is the frame that the calling code was and this information is then returned in the ref parameters.</para>
        /// </summary>
        /// <param name="className">The classname where the code was called from</param>
        /// <param name="methodName">The method name where the code was called from</param>
        /// <param name="fileName">The filename containing the code where this was called from</param>
        /// <param name="lineNumber">The line number that the calling line of code was on relative to the filename</param>
        /// <param name="parameters">The parameter information</param>
        /// <param name="getParameters">Determines whether or not extra work should be done to populate the parameters field.  If this is false parameters returns an empty string</param>
        internal void GetStackInfoBeforeTraceClass(out string className, out string methodName, out string fileName, out string lineNumber, out string parameters, bool getParameters)
        {
            /* This method is called a lot for all of the stack information that is written.  It is surprisingly slow and is the single biggest
             * overhead in all of the logging.  During Tex 2.0.x.x there were methods which hard coded starting at Stacktrace(4) but that made
             * the code significantly more complex and added only a tiny performance gain.  Therefore I reverted this code back to its current
             * state. */

            parameters = string.Empty;
            lineNumber = string.Empty;
            fileName   = string.Empty;
            methodName = string.Empty;
            className  = string.Empty;

            bool queryFileInformation = AllowedFileIOPermission();;

            // We skip the first two frames as we know there are always at least two Tex methods above this call.
            StackTrace currentStack = new StackTrace(2, queryFileInformation);

            for (int frameIdx = 0; frameIdx < currentStack.FrameCount; frameIdx++)    // go through the stack frames
            {
                StackFrame nextFrame = currentStack.GetFrame(frameIdx);

                MethodBase theMethod = nextFrame.GetMethod();
                className = InternalUtil.GetClassNameFromMethodbase(theMethod);

                if (theMethod.ReflectedType.Assembly == InternalUtil.TraceAssemblyCache)
                {
                    // skip anything in this assembly.
                    continue;
                }

                methodName = theMethod.Name;

                if (queryFileInformation)
                {
                    lineNumber = nextFrame.GetFileLineNumber().ToString();
                    fileName   = Path.GetFileName(nextFrame.GetFileName());
                }
                else
                {
                    fileName   = string.Empty;
                    lineNumber = "0";
                }

                if (getParameters)
                {
                    // Retested this a couple of times, the stringbuilder is consistantly slower.
                    ParameterInfo[] prams          = theMethod.GetParameters();
                    int             parameterIndex = 0;
                    parameters = "( ";
                    while (parameterIndex < prams.Length)
                    {
                        parameters += prams[parameterIndex].ParameterType.Name + " " + prams[parameterIndex].Name;
                        parameterIndex++;
                        if (parameterIndex != prams.Length)
                        {
                            parameters += ", ";
                        }
                    }
                    parameters += " )";
                }
                return;
            }
        }