C#每n'记录进度百分比;th
我正在下载/上传文件,我需要记录进度,现在我正在记录进度的百分比,但我的问题是它打印的行太多,可能会占用100多行,仅占0.04%。。0.06%等。 我需要的是记录每5%的进度,所以它将是0%5%10%15%。。。95%100%C#每n'记录进度百分比;th,c#,logging,.net-core,progress-bar,percentage,C#,Logging,.net Core,Progress Bar,Percentage,我正在下载/上传文件,我需要记录进度,现在我正在记录进度的百分比,但我的问题是它打印的行太多,可能会占用100多行,仅占0.04%。。0.06%等。 我需要的是记录每5%的进度,所以它将是0%5%10%15%。。。95%100% public async Task Send(IChannelHandlerContext ctx, byte[] fileBytes, CancellationToken ct) { var offset = 0; va
public async Task Send(IChannelHandlerContext ctx, byte[] fileBytes, CancellationToken ct)
{
var offset = 0;
var buffer = new ArraySegment<byte>(fileBytes);
while (true)
{
var segment = buffer.GetSegment(offset, 1024);
offset += segment.Count;
var packet = new DataTransferPacket(segment).Serialize(ctx.Allocator.Buffer());
await ctx.WriteAndFlushAsync(packet);
var progress = Math.Round((double) offset / fileBytes.Length * 100);
if (progress % 5 == 0)
{
var byteProgress =
$"[{offset.ToString().PadLeft(fileBytes.Length.ToString().Length)} / {fileBytes.Length} Bytes]";
_logger.LogInformation($"Uploading {progress,6:0.00}% {byteProgress,12}");
}
await Task.Delay(100, ct);
if (offset == fileBytes.Length)
{
return;
}
}
}
公共异步任务发送(IChannelHandlerContext ctx,字节[]文件字节,取消令牌ct)
{
var偏移=0;
var buffer=新的ArraySegment(fileBytes);
while(true)
{
var段=buffer.GetSegment(偏移量,1024);
偏移量+=段计数;
var packet=新的DataTransferPacket(段).Serialize(ctx.Allocator.Buffer());
等待ctx.WriteAndFlushAsync(数据包);
var progress=Math.Round((双精度)offset/fileBytes.Length*100);
如果(进度%5==0)
{
var byteProgress=
$“[{offset.ToString().PadLeft(fileBytes.Length.ToString().Length)}/{fileBytes.Length}字节]”;
_logger.LogInformation($“上传{progress,6:0.00}%{byteProgress,12}”);
}
等待任务。延迟(100,ct);
if(偏移量==fileBytes.Length)
{
返回;
}
}
}
到目前为止,我还没有找到解决方案。您只需跟踪下一步要记录的百分比:
public async Task Send(IChannelHandlerContext ctx, byte[] fileBytes, CancellationToken ct)
{
var offset = 0;
var buffer = new ArraySegment<byte>(fileBytes);
var nextLogThresh = 0; // <== new line
while (true)
{
var segment = buffer.GetSegment(offset, 1024);
offset += segment.Count;
var packet = new DataTransferPacket(segment).Serialize(ctx.Allocator.Buffer());
await ctx.WriteAndFlushAsync(packet);
var progress = Math.Round((double) offset / fileBytes.Length * 100);
if (progress >= nextLogThresh) // <== new line
{
var byteProgress =
$"[{offset.ToString().PadLeft(fileBytes.Length.ToString().Length)} / {fileBytes.Length} Bytes]";
_logger.LogInformation($"Uploading {progress,6:0.00}% {byteProgress,12}");
nextLogThresh += 5; // <== new line
}
await Task.Delay(100, ct);
if (offset == fileBytes.Length)
{
return;
}
}
}
公共异步任务发送(IChannelHandlerContext ctx,字节[]文件字节,取消令牌ct)
{
var偏移=0;
var buffer=新的ArraySegment(fileBytes);
var nextLogThresh=0;//=nextLogThresh)//您只需跟踪要记录的下一个“步骤”的百分比:
public async Task Send(IChannelHandlerContext ctx, byte[] fileBytes, CancellationToken ct)
{
var offset = 0;
var buffer = new ArraySegment<byte>(fileBytes);
var nextLogThresh = 0; // <== new line
while (true)
{
var segment = buffer.GetSegment(offset, 1024);
offset += segment.Count;
var packet = new DataTransferPacket(segment).Serialize(ctx.Allocator.Buffer());
await ctx.WriteAndFlushAsync(packet);
var progress = Math.Round((double) offset / fileBytes.Length * 100);
if (progress >= nextLogThresh) // <== new line
{
var byteProgress =
$"[{offset.ToString().PadLeft(fileBytes.Length.ToString().Length)} / {fileBytes.Length} Bytes]";
_logger.LogInformation($"Uploading {progress,6:0.00}% {byteProgress,12}");
nextLogThresh += 5; // <== new line
}
await Task.Delay(100, ct);
if (offset == fileBytes.Length)
{
return;
}
}
}
公共异步任务发送(IChannelHandlerContext ctx,字节[]文件字节,取消令牌ct)
{
var偏移=0;
var buffer=新的ArraySegment(fileBytes);
var nextLogThresh=0;//=nextLogThresh)//