2009-03-28 22:33:18 +00:00

109 lines
3.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
namespace Volian.Controls.Library
{
public static class vlnStackTrace
{
public static string GetStack(string str, params object[] objects)
{
return string.Format(str, objects) + StackToString();
}
public static void ShowStack(string str, params object[] objects)
{
Console.WriteLine(string.Format(str, objects) + StackToString());
}
public static string GetStack()
{
return StackToString();
}
public static void ShowStack()
{
Console.WriteLine(StackToString());
}
private static string StackToString()
{
StringBuilder sb = new StringBuilder();
StackTrace st = new StackTrace(true);
StackFrame[] sfs = st.GetFrames();
int ii = 0;
string sLast = "";
string sThis = "";
int imax = MatchingStackFrame(sfs);
foreach (StackFrame sf in sfs)
{
if (imax == 0)
sb.Append(string.Format("\r\n{0}---------", "".PadLeft(ii++ * 2)));
imax--;
if (ii < 2) ii++;
else
{
string sMethod = sf.GetMethod().Name;
string sNamespace = sf.GetMethod().ReflectedType.Namespace;
string sType = sf.GetMethod().ReflectedType.Name;
if (sf.GetFileLineNumber() != 0)
{
sMethod += string.Format(" {0}[{1}]", sf.GetFileName(), sf.GetFileLineNumber());
sThis = "";
}
else
{
sThis = sNamespace;
}
if (sLast == sThis && sThis != "")
sb.Append(string.Format("\t*.{0}.{1}", sType, sMethod));
else
sb.Append(string.Format("\r\n{0}{1}.{2}.{3}", "".PadLeft(ii++ * 2), sNamespace, sType, sMethod));
}
sLast = sThis;
}
return sb.ToString();
}
private static StackFrame [] _LastSFS;
private static int MatchingStackFrame(StackFrame[] sfs)
{
int iFound = -1;
if (_LastSFS != null)
{
// start at the far end and work backward
for (int i = 1; iFound < 0 && i <= sfs.Length && i <= _LastSFS.Length; i++)
{
if (!Match(sfs[sfs.Length - i],_LastSFS[_LastSFS.Length - i]))
iFound = 1+ sfs.Length - i;
}
}
if (iFound < 0) iFound = sfs.Length;
_LastSFS = (StackFrame[])sfs.Clone();
return iFound;
}
private static bool Match(StackFrame stackFrame1, StackFrame stackFrame2)
{
if (stackFrame1.GetMethod().Name != stackFrame2.GetMethod().Name) return false;
if (stackFrame1.GetMethod().DeclaringType.FullName != stackFrame2.GetMethod().DeclaringType.FullName) return false;
if (stackFrame1.GetILOffset() != stackFrame2.GetILOffset()) return false;
return true;
}
public static bool ScrollInStack()
{
StackTrace st = new StackTrace(true);
StackFrame[] sfs = st.GetFrames();
bool retval = false;
foreach (StackFrame sf in sfs)
{
string sMethod = sf.GetMethod().Name;
string sNamespace = sf.GetMethod().ReflectedType.Namespace;
string sType = sf.GetMethod().ReflectedType.Name;
if (sMethod.ToUpper().Contains("SCROLL") || sType.ToUpper().Contains("SCROLL"))
{
retval = true;
Console.WriteLine("{0}.{1}.{2}", sNamespace, sType, sMethod);
}
}
return retval;
}
}
}