如何在Web api 2 / app见解中生成相关ID?

时间:2019-05-08 20:34:30

标签: c# asp.net .net asp.net-web-api2 azure-application-insights

我正在Web api 2项目中使用App Insights,该项目被称为React前端。

发生错误时,我想向用户显示一个常见错误,例如:请与管理员联系,并向他们显示Guid或错误号。

然后使用该错误号,我可以在App Insights中检查真正的异常是什么。

这可能吗?

我的网络api代码在下面

namespace LuloWebApi.Controllers
{
    [Authorize]
    public class ClientController : ApiController
    {

        [HttpGet]
        public async Task<List<Client>> GetClients()
        {
            //var telemetry = new TelemetryClient();
            //try
            //{
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                return await clientStore.Query().ToListAsync();
            //}
            //catch (System.Exception ex)
            //{
            //    telemetry.TrackException(ex);

           //}
        }

        [HttpGet]
        public async Task<IHttpActionResult> GetClient(string clientId)
        {     
            var telemetry = new TelemetryClient();
            try
            {
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                var client = await clientStore.Query().FirstOrDefaultAsync(x => x.Id == clientId);
                if (client == null)
                {
                    return NotFound();
                }
                return Ok(client);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }

        [HttpPut]
        public async Task<IHttpActionResult> UpdateClient(string id,[FromBody]Client client)
        {

            var telemetry = new TelemetryClient();
            try
            {

                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var result = await clientStore.UpdateAsync(client);
                return Ok(result);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }


        [HttpPost]
        public async Task<IHttpActionResult> AddCLient([FromBody]Client Client)
        {
            var telemetry = new TelemetryClient();
            try
            {
                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }

                var added = await clientStore.AddAsync(Client);
                return StatusCode(HttpStatusCode.NoContent);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }

        }



        public async Task<IHttpActionResult> DeleteClient(string clientId)
        {
            var telemetry = new TelemetryClient();
            try
            {

                var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
                await clientStore.RemoveByIdAsync(clientId);
                return Ok(clientId);
            }
            catch (System.Exception ex)
            {
                telemetry.TrackException(ex);
                return BadRequest("Unknown error");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

如果我误会你,请纠正我。

我认为这就像手动创建一个guid一样简单,并且可以将异常遥测以及BadRequest()添加到其中。

#!/bin/bash

set -xe

MASTER_URL=$1
MASTER_USERNAME=$2
MASTER_PASSWORD=$3
NODE_NAME=$4
NUM_EXECUTORS=$5

# Download CLI jar from the master
curl ${MASTER_URL}/jnlpJars/jenkins-cli.jar -o ~/jenkins-cli.jar

# Create node according to parameters passed in
cat <<EOF | java -jar ~/jenkins-cli.jar -auth "${MASTER_USERNAME}:${MASTER_PASSWORD}" -s "${MASTER_URL}" create-node "${NODE_NAME}" |true
<slave>
  <name>${NODE_NAME}</name>
  <description></description>
  <remoteFS>/home/jenkins/agent</remoteFS>
  <numExecutors>${NUM_EXECUTORS}</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy\$Always"/>
  <launcher class="hudson.slaves.JNLPLauncher">
    <workDirSettings>
      <disabled>false</disabled>
      <internalDir>remoting</internalDir>
      <failIfWorkDirIsMissing>false</failIfWorkDirIsMissing>
    </workDirSettings>
  </launcher>
  <label></label>
  <nodeProperties/>
  <userId>${USER}</userId>
</slave>
EOF
# Creating the node will fail if it already exists, so |true to suppress the
# error. This probably should check if the node exists first but it should be
# possible to see any startup errors if the node doesn't attach as expected.


# Run jnlp launcher
java -jar /usr/share/jenkins/slave.jar -jnlpUrl ${MASTER_URL}/computer/${NODE_NAME}/slave-agent.jnlp -jnlpCredentials "${MASTER_USERNAME}:${MASTER_PASSWORD}"

当您获得GUID时,可以在azure门户中搜索相关的错误:

enter image description here