我正在使用NewRelic来洞察我的系统,为了向新的-relic的事务添加自定义属性,我正在使用NewRelic.Api.Agent.NewRelic.GetAgent() (docs)解析IAgent接口。
像这样:
public async Task<IActionResult> MyMethod([FromBody]MyRequest request)
{
//Do Some logic.
IAgent agent = NewRelic.Api.Agent.NewRelic.GetAgent();
ITransaction transaction = agent.CurrentTransaction;
transaction.AddCustomAttribute("MyCustomAttribute", SomeValue)
return Ok(...);
}是否可以使用依赖注入容器来解析IAgent接口?像这样:
[ApiController]
public class MyController
{
private readonly IAgent _agnet;
public MyController(IAgent agent)
{
_agent = agent;
}
public async Task<IActionResult> MyMethod([FromBody]MyRequest request)
{
//Do Some logic.
ITransaction transaction = this._agent.CurrentTransaction;
transaction.AddCustomAttribute("MyCustomAttribute", SomeValue)
return Ok(...);
}
}发布于 2021-03-09 16:53:47
因此它是一个静态实例,可以注册为单例:
services.AddSingletone<IAgent>(NewRelic.Api.Agent.NewRelic.GetAgent());或者:
services.AddSingletone(NewRelic.Api.Agent.NewRelic.GetAgent());不要忘记首先配置代理,然后将其添加到IServiceCollection。
https://stackoverflow.com/questions/66541519
复制相似问题