也许是因为你想要的东西不可用。
我使用的方法是打开一个StreamWriter
写入文本文件。在某些情况下,我使用以下样式的代码:
private void WriteLineToTestLog(string line)
{
StreamWriter sw = File.AppendText("C:\\Windows\\Temp\\MyWebtestsLog.txt");
sw.WriteLine(System.DateTime.Now + " " + line);
sw.Close();
}
对于许多虚拟用户的测试,我担心上述代码的性能。所以我制作了一个版本,它有一个受锁保护的共享流:
public class SingletonWriter
{
private static object myLock = new object();
private static SingletonWriter mySingleton = null;
private StreamWriter outStream;
public static string AgentId { get; set; } // Set when running with multiple agent computers.
private SingletonWriter()
{
if ( AgentId == null ) { AgentId = "00"; }
string streamName = @"TestStatus." + System.DateTime.Now.ToString("yyyy-MM-dd.HHmm.") + AgentId + ".log";
System.Console.WriteLine("Writing to " + streamName);
outStream = new StreamWriter(streamName, true); // true means append to an existing file.
outStream.WriteLine("Start of output");
outStream.Flush();
}
public static void WriteLine(string line)
{
lock (myLock)
{
if (mySingleton == null)
{
mySingleton = new SingletonWriter();
}
mySingleton.outStream.WriteLine(line);
mySingleton.outStream.Flush();
}
}
}
这是从其他 Web 测试中的插件调用的,代码如下:
StringBuilder message sb = new StringBuilder();
sb.Append(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff "));
sb.Append(e.WebTest.Name);
sb.Append(...); // More useful data
string message = sb.ToString();
e.WebTest.AddCommentToResult(message);
SingletonWriter.WriteLine(message);
日志文件被写入执行测试的计算机上。由于这可能是代理计算机,因此需要将文件从代理复制到要对其进行分析的计算机上。当使用 Visual Studio Online 运行测试时,该代码也可以正常工作,但我没有找到任何查看或收集此类测试运行的日志文件的方法。