简体   繁体   English

C#Portable Class Library等效于System.Diagnostics.StackTrace

[英]C# Portable Class Library Equivalent of System.Diagnostics.StackTrace

A program I am working on has a logging function appropriately named "Error," to notify of errors without crashing the program, however, I would like to include a stack trace so these non-fatal errors can be more easily debugged. 我正在处理的程序有一个适当命名为“Error”的日志记录功能,用于通知错误而不会导致程序崩溃,但是,我想包含一个堆栈跟踪,以便更容易调试这些非致命错误。 My first instinct was to use System.Diagnostics.StackTrace , which is unfortunately not available in PCL's. 我的第一直觉是使用System.Diagnostics.StackTrace ,遗憾的是在PCL中没有。

Then, I tried to throw and promptly catch an exception. 然后,我试图抛出并立即捕获异常。

try { throw new Exception(); } 
catch (Exception ex) { return ex.StackTrace; }

Unfortunately, this only provides the top of the call stack: as it does not unravel the stack on its way down, it doesn't provide any useful information. 不幸的是,这只提供了调用堆栈的顶层:因为它没有解开堆栈,所以它不提供任何有用的信息。 So, my question is this: How do I get a stack trace in ac# PCL function without throwing an error and catching it at the bottom of the stack? 所以,我的问题是: 如何在ac#PCL函数中获得堆栈跟踪而不会抛出错误并将其捕获到堆栈的底部? I would prefer to keep the code entirely in the PCL and avoid using abstractions and platform specific implementation code for something so trivial. 我更愿意将代码完全保留在PCL中,并避免使用抽象和平台特定的实现代码来实现这么简单的事情。

Edit as a response to a comment: `throw new Exception(ex) Only adds another layer to the stack trace, so it has two lines in the stack trace function but still fails to retrieve the full trace. 编辑为对注释的响应:`throw new Exception(ex)仅向堆栈跟踪添加另一个层,因此它在堆栈跟踪功能中有两行但仍无法检索完整跟踪。

Try to use the Environment.StackTrace property . 尝试使用Environment.StackTrace属性

The StackTrace property lists method calls in reverse chronological order, that is, the most recent method call is described first, and one line of stack trace information is listed for each method call on the stack. StackTrace属性按反向时间顺序列出方法调用,即首先描述最近的方法调用,并为堆栈上的每个方法调用列出一行堆栈跟踪信息。 However, the StackTrace property might not report as many method calls as expected due to code transformations that occur during optimization. 但是,由于优化期间发生的代码转换,StackTrace属性可能不会报告与预期一样多的方法调用。

EDIT: 编辑:

Version description seems to be stating that Environment class is supported by PCL : 版本描述似乎表明PCL支持Environment类

Version Information .NET Framework Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0 .NET Framework Client Profile Supported in: 4, 3.5 SP1 Portable Class Library Supported in: Portable Class Library .NET for Windows Store apps Supported in: Windows 8 Supported in: Windows Phone 8.1 Supported in: Windows Phone Silverlight 8.1 Supported in: Windows Phone Silverlight 8 版本信息.NET Framework受以下版本支持:4.6,4.5,4,3.5,3.0,2.0,1.1,1.0 .NET Framework Client Profile受以下版本支持:4,3.5 SP1可移植类库受以下版本支持:可移植类库.NET for Windows商店应用程序受以下版本支持:Windows 8受以下版本支持:Windows Phone 8.1受以下版本支持:Windows Phone Silverlight 8.1受以下版本支持:Windows Phone Silverlight 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Diagnostics.StackTrace紧凑框架 - System.Diagnostics.StackTrace compact framework 在恢复C#异步方法时,System.Diagnostics.StackTrace中缺少Visual Studio 2017调用堆栈中可见的堆栈帧 - Stack frames visible in Visual Studio 2017 Call Stack are missing in System.Diagnostics.StackTrace when resuming C# async method c#可移植类库的代码覆盖率 - Code Coverage for c# Portable Class Library 相当于PCL库中的System.Diagnostics.Trace - Equivalent for System.Diagnostics.Trace in PCL library C#可移植类库 - 如何包含System.Data.Linq - C# Portable Class Library - How do you include System.Data.Linq C# - 可移植类库“命名空间System.XML中不存在XmlElement” - C# - Portable Class Library “XmlElement does not exist in the namespace System.XML” 是否有等效于C ++的System.Diagnostics.EventLog? - Is there an equivalent of System.Diagnostics.EventLog for C++? 使用来自 C# 可移植库的 F# 可移植类库 - Using F# portable class library from C# portable library 如何从C#可移植类库(PCL)添加对F#可移植库的引用 - How do I add a reference to F# Portable Library from C# Portable Class Library (PCL) sw#in c# - 在便携式类库中找不到HandleRef - swig in c# - HandleRef could not be found in portable class library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM