本文整理汇总了C#中System.Diagnostics.StackTrace类的典型用法代码示例。如果您正苦于以下问题:C# StackTrace类的具体用法?C# StackTrace怎么用?C# StackTrace使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
StackTrace类属于System.Diagnostics命名空间,在下文中一共展示了StackTrace类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetCurrentMethod
public static string GetCurrentMethod(int frame)
{
StackTrace st = new StackTrace();
StackFrame sf = st.GetFrame(frame);
return sf.GetMethod().Name;
}
示例2: Eval
public override void Eval(MockDirectoryWrapper dir)
{
if (DoFail && TestThread())
{
bool isDoFlush = false;
bool isClose = false;
var trace = new StackTrace();
foreach (var frame in trace.GetFrames())
{
var method = frame.GetMethod();
if (isDoFlush && isClose)
{
break;
}
if ("flush".Equals(method.Name))
{
isDoFlush = true;
}
if ("close".Equals(method.Name))
{
isClose = true;
}
}
if (isDoFlush && !isClose && Random().NextBoolean())
{
HitExc = true;
throw new IOException(Thread.CurrentThread.Name + ": now failing during flush");
}
}
}
示例3: Lock
/// <summary>
/// The object to lock.
/// </summary>
/// <param retval="o">The object to lock.</param>
/// <param retval="timeout">The timeout.</param>
/// <returns></returns>
/// <exception cref="LockTimeoutException">
/// </exception>
public static TimedLock Lock(object o, TimeSpan timeout)
{
var tl = new TimedLock(o);
if (!Monitor.TryEnter(o, timeout))
{
#if DEBUG
GC.SuppressFinalize(tl._leakDetector);
StackTrace blockingTrace;
lock (Sentinel.StackTraces)
{
blockingTrace = Sentinel.StackTraces[o] as StackTrace;
}
throw new LockTimeoutException(blockingTrace);
#else
throw new LockTimeoutException();
#endif
}
#if DEBUG
// Lock acquired. Store the stack trace.
var trace = new StackTrace();
lock (Sentinel.StackTraces)
{
Sentinel.StackTraces.Add(o, trace);
}
#endif
return tl;
}
示例4: findCallToLoadPackages
private static string findCallToLoadPackages()
{
var trace = new StackTrace(Thread.CurrentThread, false);
var frame = trace.GetFrame(2);
return "{0}.{1}(), {2} line {3}".ToFormat(frame.GetMethod().DeclaringType.FullName, frame.GetMethod().Name,
frame.GetFileName(), frame.GetFileLineNumber());
}
示例5: PagerState
public PagerState(AbstractPager pager)
{
_pager = pager;
#if DEBUG_PAGER_STATE
Instances[this] = new StackTrace(true);
#endif
}
示例6: ExceptionToString
/// <summary>
/// Converts an Exception to a verbose string
/// </summary>
public static string ExceptionToString(Exception e)
{
string message;
if (object.ReferenceEquals(FormatException, null))
{
var trace = new StackTrace(e, true);
var frame = trace.GetFrame(0);
var file = frame.GetFileName();
var line = frame.GetFileLineNumber();
StringBuilder sb = new StringBuilder(String.Format("{0} line {1}", file, line));
sb.AppendLine("");
sb.AppendLine(String.Format("[Source] {0} : [Message] {1}", e.Source, e.Message));
sb.AppendLine("");
sb.AppendLine(e.ToString());
message = sb.ToString();
}
else
{
message = FormatException(e);
}
return message;
}
示例7: CallerMethodName
public static string CallerMethodName()
{
StackTrace stackTrace = new StackTrace();
StackFrame stackFrame = stackTrace.GetFrame(2);
MethodBase methodBase = stackFrame.GetMethod();
return methodBase.Name;
}
示例8: ToCodeLocations
/// <summary>
/// Converts an exception stack to a sarif code location list.
/// </summary>
public static IList<AnnotatedCodeLocation> ToCodeLocations(this Exception exception)
{
List<AnnotatedCodeLocation> codeLocations = new List<AnnotatedCodeLocation>();
StackTrace stack = new StackTrace(exception);
foreach (StackFrame frame in stack.GetFrames())
{
AnnotatedCodeLocation codeLocation = new AnnotatedCodeLocation();
MemberInfo member = frame.GetMethod();
if (member != null)
{
codeLocation.Message = member.ReflectedType.FullName + "." + member.Name;
}
PhysicalLocationComponent physicalLocation = new PhysicalLocationComponent();
string filename = frame.GetFileName();
if (!String.IsNullOrWhiteSpace(filename))
{
physicalLocation.Uri = new Uri(filename);
}
physicalLocation.Region = new Region();
physicalLocation.Region.StartLine = frame.GetFileLineNumber();
physicalLocation.Region.EndLine = frame.GetFileLineNumber();
physicalLocation.Region.StartColumn = frame.GetFileColumnNumber();
physicalLocation.Region.EndColumn = frame.GetFileColumnNumber();
codeLocation.PhysicalLocation = new List<PhysicalLocationComponent>() { physicalLocation };
codeLocations.Add(codeLocation);
}
return codeLocations;
}
示例9: LoadReport
private void LoadReport(int memberid, string fromDate, string toDate)
{
try
{
List<ReportParameter> rp = new List<ReportParameter>();
rp.Add(new ReportParameter("MemberID", memberid.ToString()));
rp.Add(new ReportParameter("StartDate", fromDate));
rp.Add(new ReportParameter("RunDate", toDate));
//ReportViewer1.ServerReport.ReportPath = "/APT.Reports/BenefitStatement";
ReportViewer1.ServerReport.ReportPath = "/APT.Reports/APTARFInvestmentStatementWeb";
ReportViewer1.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings["ReportServerUrl"]);
ReportViewer1.ServerReport.ReportServerCredentials = new ReportCredentials(ConfigurationManager.AppSettings["ReportServerUser"], ConfigurationManager.AppSettings["ReportServerPassword"], ConfigurationManager.AppSettings["ReportServerDomain"]);
ReportViewer1.ServerReport.SetParameters(rp);
ReportViewer1.ShowParameterPrompts = false;
ReportViewer1.ServerReport.Refresh();
}
catch (Exception ex)
{
StackTrace stackTrace = new StackTrace();
APTLog.LogException(ex, stackTrace.GetFrame(0).GetMethod().Name, APT2012AssemblyInfo.AssemblyInfo, APT2012AssemblyInfo.SUBSYSTEM);
//throw ex;
}
}
示例10: LogEventArgs
public LogEventArgs(string message, MessageType messageType, StackTrace st)
{
this.message = message;
this.dateTime = DateTime.Now;
this.messageType = messageType;
this.stackTrace = st;
}
示例11: ErrorEvent
// Methods
public ErrorEvent()
{
base.m_Severity = EventSeverity.Error;
base.m_EventID = 0x3e8;
base.m_Message = string.Format("Error in '{0}'", base.m_CallingMethod.ToString());
StringBuilder builder1 = new StringBuilder(0x3e8);
try
{
StackTrace trace1 = new StackTrace(true);
int num1 = 2;
Type type1 = null;
do
{
num1++;
type1 = trace1.GetFrame(num1).GetMethod().DeclaringType;
} while ((num1 < trace1.FrameCount) && type1.IsSubclassOf(typeof (BaseEvent)));
for (int num2 = num1; num2 < trace1.FrameCount; num2++)
{
StackFrame frame1 = trace1.GetFrame(num2);
if (num2 > num1)
{
builder1.Append('|');
}
builder1.AppendFormat("{{{0}}}:{1}:{2}", ReflectionHelper.MethodSignature(frame1.GetMethod()),
frame1.GetFileName(), frame1.GetFileLineNumber());
}
}
catch
{
}
base.AddProperty("StackTrace", builder1.ToString());
}
示例12: Parse
public bool Parse(StackTrace stackTrace)
{
foreach (IStackTraceParser p in GetParsers())
{
if (p.Parse(stackTrace))
{
parser = p;
return true;
}
}
var helpLink = @"http://blog.approvaltests.com/2012/01/creating-namers.html";
throw new Exception(
string.Format("Approvals is not set up to use your test framework.{0}" +
"It currently supports {1}{0}" +
"To add one use {2}.AddParser() method to add implementation of {3} with support for your testing framework.{0}" +
"To learn how to implement one see {4}",
Environment.NewLine,
ForTestingFramework,
GetType(),
typeof (IStackTraceParser),
helpLink))
{
HelpLink = helpLink
};
}
示例13: LogInfo
//**************************************************
// 関数名 :LogInfo
// 機能説明 :ログ情報をログファイルに書き込む。
// 引数 :logInfo ログ情報
// 引数 :args ログ詳細情報
// 修正履歴 :2010/04/13
//**************************************************
public void LogInfo(string logInfo, params object[] args)
{
string cHeader = "";
string cLogTime = "";
cLogTime = System.DateTime.Now.ToString();
if (args != null && args.Length > 0)
{
logInfo = String.Format(logInfo, args);
}
StackTrace st = new StackTrace(true);
StackFrame sf = st.GetFrame(1);
//cHeader = sf.GetMethod
cHeader = sf.GetMethod().ReflectedType.FullName + "." + sf.GetMethod().Name + "()";
cHeader = cLogTime + " [" + cHeader + "] ";
//ログ情報
logInfo = cHeader + logInfo + Environment.NewLine;
try
{
//'ログ情報はログファイルに書き込む。
File.AppendAllText(LogFileName, logInfo);
}
catch (Exception ex)
{
logInfo = String.Format("異常が発生しました。{0}", ex);
//'ログ情報はログファイルに書き込む。
File.AppendAllText(LogFileName, logInfo);
}
}
示例14: Fail
public override void Fail (string message, string detailMessage)
{
var frames = new StackTrace (1, true).GetFrames ();
//find the stack frame that actually called into the trace system
int callerFrame = 0;
for (; (callerFrame < frames.Length && IsInfrastructureMethod (frames [callerFrame])); callerFrame++)
continue;
if (callerFrame == frames.Length - 1)
callerFrame = 0;
var sb = new StringBuilder ();
if (IsRealMessage (message)) {
if (!string.IsNullOrEmpty (detailMessage)) {
sb.AppendFormat ("Failed assertion: {0} - {1}", message, detailMessage);
} else {
sb.AppendFormat ("Failed assertion: {0}", message);
}
} else {
sb.Append ("Failed assertion at ");
FormatStackFrame (sb, frames [callerFrame]);
callerFrame++;
}
sb.Append ("\n");
FormatStackTrace (sb, frames, callerFrame);
LoggingService.LogError (sb.ToString ());
}
示例15: Tracer
/// <summary>
/// Initializes a new instance of the <see cref="Tracer"/> class.
/// Results in a method entry trace being output.
/// </summary>
public Tracer()
{
if (!_isTracingEnabled)
{
return;
}
// Due to optimizations during JIT compilation the StackTrace and its properties are not reliable
// especially when the following code is in a separate method
// http://www.smelser.net/blog/2008/11/default.aspx
_numberOfCallingMethodParams = 0;
_callingMethodName = "Not available";
StackTrace stackTrace = new StackTrace();
StackFrame callingMethodFrame = stackTrace.GetFrame(1);
if (callingMethodFrame != null)
{
MethodBase methodInfo = callingMethodFrame.GetMethod();
if (methodInfo != null)
{
ParameterInfo[] parameterInfo = methodInfo.GetParameters();
_numberOfCallingMethodParams = parameterInfo.Count();
Type declaringType = methodInfo.DeclaringType;
if (declaringType != null)
{
_callingMethodName = declaringType.Name + "." + methodInfo.Name;
}
}
}
TraceMethodEntry(null);
}