简体   繁体   English

如何使用 Visual Studio 2019 在 azure function 编写的 .net 核心 3.1 的捕获块中捕获状态代码 2019

[英]How to capture status code in catch block of an azure function written .net core 3.1 using visual studio 2019

I'm developing azure functions written in .NET CORE 3.1 using Visual Studio 2019. I have a catch block as below我正在使用 Visual Studio 2019 开发用 .NET CORE 3.1 编写的 azure 函数。我有一个如下的 catch 块

catch (Exception ex)
{
    log.LogError(ex, "Error: " + ex.Message + "");
    var errormodel = new { isError = true, Message = ex.Message, errorInnerException = ex.InnerException, stackTrace = ex.StackTrace };
    return new ObjectResult(errormodel)
    {

    };
    throw ex;
}

I'm not able to capture status code here... Any idea how to capture status code in catch block?我无法在这里捕获状态代码...知道如何在 catch 块中捕获状态代码吗?

You can install this package: ServiceStack 5.10.4 , then in your catch block, you can just use the ToStatusCode() method.您可以安装此 package: ServiceStack 5.10.4 ,然后在您的 catch 块中,您可以只使用ToStatusCode()方法。

The code like below:如下代码:

        catch(Exception ex)
        {
            var x = ex.ToStatusCode();
            log.LogError(ex, "Error: " + ex.Message + ", Status Code:" + ex.ToStatusCode());
            
            var errormodel = new { isError = true, Message = ex.Message, errorInnerException = ex.InnerException, stackTrace = ex.StackTrace, StatusCode = ex.ToStatusCode() };
            return new ObjectResult(errormodel)
            {

            };
            throw ex;
        }

暂无
暂无

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

相关问题 适用于 .Net Core 3.1 的 Visual Studio 2019 中的 WinForms 设计器窗口 - WinForms Designer window in Visual Studio 2019 for .Net Core 3.1 如何在 ASP.NET Core 3.1 中使用 Visual Studio 2019 在 docker 中设置环境? - How to set the environment in a docker with Visual Studio 2019 in ASP.NET Core 3.1? 如何在 ASP.NET CORE 3.1 Web API 中捕获当前状态码 - How to capture current Status code in ASP.NET CORE 3.1 Web API 在使用 visual studio 2019 运行应用程序时,如何防止在 Asp.Net Core API 3.1 中自动生成 web.config? - How to prevent auto-generated web.config in Asp.Net Core API 3.1 while running the application using visual studio 2019? 在 .net 核心 3.1 / Visual Studio 2019 中自定义脚手架 Razor 页面 (CRUD) 的模板 - Customise the template for Scaffolded Razor Pages (CRUD) in .net core 3.1 / Visual Studio 2019 无法在 Visual Studio 2019 for Mac 上运行 .NET Core 3.1 测试项目 - Cannot run .NET Core 3.1 tests projects on Visual Studio 2019 for Mac 如何在.Net Core Windows Forms 在 Visual Studio 2019 中的应用程序 C# 中编码菜单/子菜单 - How to code Menues / Sub Menues in .Net Core Windows Forms Application in Visual Studio 2019 C# 如何在 Visual Studio 2019 中本地运行多个 Azure .net 5 函数项目? - How Can I run multiple Azure .net 5 Function Projects locally in Visual Studio 2019? 如何使用Visual Studio 2019社区创建.NET Core 3控制台应用程序? - How to create a .NET Core 3 console application with Visual Studio 2019 Community? 我如何在 Visual Studio 2019 中的 React Core 3.1 模板中重定向 - How can i redirect in react core 3.1 template in visual studio 2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM