How to get correct stack trace for exceptions in Web API 2 async actions?

前端 未结 3 1040
陌清茗
陌清茗 2021-02-13 04:53

I have a simple API controller method

public async Task GetByDate(DateTime date, string user = null)
{
    throw new InvalidOperationExce         


        
3条回答
  •  -上瘾入骨i
    2021-02-13 05:40

    I believe stack traces are improved on .NET 4.5.1 when running on Windows 8.1 / Server 2012 R2.

    Alternatively, I do have an "Async Diagnostics" NuGet package that you can install into your project. Then add this line:

    [assembly: AsyncDiagnosticAspect]
    

    And you can use the ToAsyncDiagnosticString extension method on the Exception type. ToAsyncDiagnosticString includes all the information from ToString and then appends a "logical stack". More documentation (and the source) on GitHub.

    Note that there is no support for partial trust and this works best in Debug builds.

提交回复
热议问题