简体   繁体   中英

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

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?

You can install this package: ServiceStack 5.10.4 , then in your catch block, you can just use the ToStatusCode() method.

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM