如何在 web api 2/app insights 中生成关联 ID?
how to generate a correlation id in web api 2/app 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");
}
}
}
}
如果理解有误请指正
我认为这就像手动创建一个 guid 一样简单,并将其添加到异常遥测以及 BadRequest() 中。
try
{
//some code here
}
catch(Exception ex)
{
string guid = Guid.NewGuid().ToString();
Dictionary<string,string> dt = new Dictionary<string, string>();
dt.Add("my error number1", guid);
telemetryClient.TrackException(ex,dt);
return BadRequest("Unknown error:"+guid);
}
并且获取到guid后,可以在azure portal中搜索相关错误:
我在我的 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");
}
}
}
}
如果理解有误请指正
我认为这就像手动创建一个 guid 一样简单,并将其添加到异常遥测以及 BadRequest() 中。
try
{
//some code here
}
catch(Exception ex)
{
string guid = Guid.NewGuid().ToString();
Dictionary<string,string> dt = new Dictionary<string, string>();
dt.Add("my error number1", guid);
telemetryClient.TrackException(ex,dt);
return BadRequest("Unknown error:"+guid);
}
并且获取到guid后,可以在azure portal中搜索相关错误: