<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Get an exceptions full stack trace with inner ex</Title>
      <Shortcut>Getanexceptionsfullstacktracewithinnerex</Shortcut>
      <Description>Get an exceptions full stack trace with inner ex [C#]</Description>
      <Author>Zepho Zep</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=5f98ddf1-ffdc-42d4-9833-3848145a2da2</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="csharp"><![CDATA[    'Returns a string representing a full stack for a passed in exception and all inner exceptions
    'will only get the first 50 inner exceptions to prevent stack overflow
    Private Function fullStackTrace(ByVal ex As Exception) As String
        If ex Is Nothing Then
            Throw New ArgumentException("ex can not be null", "ex")
        End If
        Dim outputStack As New System.Text.StringBuilder
        outputStack.Append(ex.StackTrace)
        Dim innerReferences As Byte = 0 'used to ensure memory does not run out
        'during stack looping
        Dim innerException As Exception = ex.InnerException
        While Not innerException Is Nothing _
            AndAlso innerReferences < 50
            outputStack.Insert(0, innerException.StackTrace)
            innerException = innerException.InnerException
            innerReferences += CByte(1)
        End While
        Return outputStack.ToString
    End Function]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>