我试图围绕我的网络API调用实施一些日志记录。没有什么太花哨,但我还在学习async / await,并且希望在不中断性能的情况下进行日志记录。
以下是我要实施的示例,但并不完全确定如何执行此操作。
public class Logging : ActionFilterAttribute
{
public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
// DoLoggingHere(); ????
base.OnActionExecutingAsync(actionContext, cancellationToken);
// OrDoLoggingHere(); ????
// Also, what does the return look like if any?
}
public override Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
// This method should/could complete before the logging completes.
base.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
// Might log here too.
}
}
基本上我不希望任何日志记录妨碍HTTP调用。它应该是透明的。
寻求任何帮助!
答案 0 :(得分:2)
您可以等待调用,以便线程返回池以获取更多传入请求,然后在调用完成后进行日志记录:
MinMax minMax = objects.stream()
.mapToInt(e -> e.getAndResetValue())
.collect(() -> new minMax(startValues), MinMax::addValue, MinMax::compareMinMax);