我如何在日志中显示变化的值



我有一个ASP.NET核心应用程序,我不知道下一步该怎么做。注意 - 我使用Visual Studio2019。

我有一个进度的图像下载。我想知道我的代码是否效果很好并输出正确的值(如果您注意到某些内容,或者如果有更好的解决方案,欢迎您通知我(。但是我不想不断调试整个代码的所有步骤(此时需要大量时间(。

所以我的想法是将progress放在某个地方(也许在控制台日志中(,所以我可以一直观看。

我尝试了什么?

我已经在我的方法中添加了一个简单的Console.WriteLine (progress),但是我没有控制台。然后我使用Command Window>? progress尝试了一下,但是仅在设置断点时输出值,这再次导致同一问题。

我必须调整什么,以便看到日志中的进度?

这是我的代码:

while(...)
{
    if (ExpectedStreamSize.HasValue && _configSize.HasValue)
    {
        var expected = ExpectedStreamSize + _configSize.Value;
        var progress = _stream.ReadPosition / (float) expected;
        var limitedProgress = progress > 1 ? 1 : progress;
        var epsilon = 0.001;
        if (!_lastReportedProgress.HasValue || _lastReportedProgress.Value + epsilon < limitedProgress)
            _onProgressChanged?.Invoke(limitedProgress);
        _lastReportedProgress = limitedProgress;
        Console.WriteLine(progress);     // I setted my breakpoint in here
    }
}

您可以尝试记录文件。这样,即使在发布模式或在Visual Studio之外运行可执行文件时,您也可以调试。

public void LogToFile(string text)
{
    string path = @"c:tempMyTest.txt";
    string appendText = text + Environment.NewLine;
    File.AppendAllText(path, appendText);
}

在调试时,右键单击变量,选择"添加手表"。您会在手表窗口中看到它的值。

最新更新