gpt4 book ai didi

c# - 欧文通话时长

转载 作者:太空宇宙 更新时间:2023-11-03 10:27:13 24 4
gpt4 key购买 nike

有没有简单的方法可以在 C# 中查找通话时长。

我正在使用 Owin 中间件来拦截对 API 的调用并记录有关它们的信息,我想记录的部分内容是调​​用的持续时间。

public override async Task Invoke(IOwinContext context)
{
var request = context.Request;
var response = context.Response;
var userName = request.User != null && request.User.Identity.IsAuthenticated ? request.User.Identity.Name : "No username";

var entity = new TrackingEntity()
{
UserName = userName
};

// invoke the next middleware in the pipeline

await Next.Invoke(context);

WriteRequestHeaders(request, entity);
WriteResponseHeaders(response, entity);

await storage.InsertEntity(entity);
}

如果我的跟踪实体有一个 Duration 字段用于存储通话的时间长度,那么记录通话的最佳方式是什么?

public TimeSpan Duration { get; set; }

使用在调用下一个之前开始并在之后结束的计时器的最准确方法是什么?

任何意见都将不胜感激,我想我只是想多了。

提前致谢。

最佳答案

我认为您不需要创建计时器的开销,只需将开始时间记录到一个变量中,然后将结束时间记录到另一个变量中以设置持续时间。

var startTime = DateTime.Now;
await Next.Invoke(context);
var endTime = DateTime.Now;
Duration = endTime - startTime;

有点乱,但如果您在此中间件中所做的只是记录日志,您希望它很快,这应该符合要求。

关于c# - 欧文通话时长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31305108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com