C# 每隔 n 次记录一次进度百分比
C# logging percentage progress every n'th
我是 downloading/uploading 文件,我需要记录它的进度,现在我正在记录该进度的百分比,但我的问题是它打印了太多行,它可能需要多达 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;
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;
}
}
}
到目前为止我找不到解决这个问题的方法。
您可以只跟踪您要记录的下一个“步骤”的百分比:
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;
}
}
}
这当然不会记录最后一个“100%”,所以如果你想要,你需要在 if (offset == fileBytes.Length)
中添加一个额外的 _logger.LogInformation(...)
我是 downloading/uploading 文件,我需要记录它的进度,现在我正在记录该进度的百分比,但我的问题是它打印了太多行,它可能需要多达 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;
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;
}
}
}
到目前为止我找不到解决这个问题的方法。
您可以只跟踪您要记录的下一个“步骤”的百分比:
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;
}
}
}
这当然不会记录最后一个“100%”,所以如果你想要,你需要在 if (offset == fileBytes.Length)
_logger.LogInformation(...)