System.Diagnostics.StackTrace.GetFrames C# (CSharp) Method

GetFrames() public method

public GetFrames ( ) : System.Diagnostics.StackFrame[]
return System.Diagnostics.StackFrame[]
        public StackFrame[] GetFrames()
        {
            // There are special IPs mixed in to the list that aren't intended to be exposed as StackFrames
            // Deliberately doing this without Linq to keep the dependency set down.
            StackFrame[] frames = new StackFrame[_ips.Length];
            int countNonSpecialIPs = 0;
            for (int i = 0; i < _ips.Length; i++)
            {
                if (_ips[i] != StackTraceHelper.SpecialIP.EdiSeparator)
                {
                    frames[countNonSpecialIPs++] = new StackFrame(_ips[i], _needFileInfo);
                }
            }
            //for compat with CoreCLR, we always return null instead of 0 length array
            if (countNonSpecialIPs == 0)
            {
                return null;
            }
            Array.Resize(ref frames, countNonSpecialIPs);
            return frames;
        }

Same methods

StackTrace::GetFrames ( ) : System.Diagnostics.StackFrame[]

Usage Example

        public void OnException(ExceptionContext context)
        {
            this._logger.LogError("MatterCenterExceptionFilter", context.Exception);
            var stackTrace = new StackTrace(context.Exception, true);
            StackFrame stackFrameInstance = null;

            if(stackTrace.GetFrames().Length>0)
            {
                for(int i=0; i< stackTrace.GetFrames().Length; i++)
                {
                    if(stackTrace.GetFrames()[i].ToString().Contains("Microsoft.Legal.Matter"))
                    {
                        stackFrameInstance = stackTrace.GetFrames()[i];
                        break;
                    }
                }
            }

            var response = new ErrorResponse()
            {
                Message = context.Exception.Message,
                StackTrace = context.Exception.ToString(),
                Description = "Error occured in the system. Please contact the administrator",
                //Exception = context.Exception.ToString(),
                LineNumber = stackFrameInstance?.GetFileLineNumber(),
                MethodName = stackFrameInstance?.GetMethod().Name,
                ClassName = stackFrameInstance?.GetMethod().DeclaringType.Name,
                ErrorCode = ((int)HttpStatusCode.InternalServerError).ToString()
            };
            context.Result = new ObjectResult(response)
            {
                StatusCode = (int)HttpStatusCode.InternalServerError,
                DeclaredType = typeof(ErrorResponse)                
            };
        }
All Usage Examples Of System.Diagnostics.StackTrace::GetFrames