是否应该等待指标调用?不等他们是安全的吗?

问题描述 投票:0回答:1

在我的后端Web服务中,指标会在多个时间发送,以跟踪其行为。要发送指标,我进行了http调用。我担心等待它会增加服务响应的无用延迟。在我看来,他们应该是火又忘了。是否安全和/或建议不等待此类呼叫?

注意:我的服务在c#,dotnet核心中。

PS:呼叫失败可能由度量标准数据包本身记录。指标调用失败不应中断用户调用。

c# .net-core async-await metrics fire-and-forget
1个回答
0
投票

如果您不需要它,为什么要等待。但是要小心abbout错误。

这里有一些扩展名可帮助解决错误

public static void RunAndForget(this Task task, Action<Exception> onError)
{
    task.ContinueWith(t => { onError?.Invoke(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
}

//the Logging.LogException is a static Action so you can set it once
public static void RunAndForget(this Task task)
{
    task.ContinueWith(t => { Logging.LogException(t.Exception); }, TaskContinuationOptions.OnlyOnFaulted);
}
© www.soinside.com 2019 - 2024. All rights reserved.