Reputation: 878
I've got DEBUG and TRACE defined. I've tried Trace.WriteLine, Debug.WriteLine, Console.WriteLine, and TestContext.WriteLine. In the Test Results window, I've added columns for Output (StdOut), Errors (StdError), Debug Trace, and Stack Trace. I've tried all the "Show output from" options available for different streams in the Output window. I've looked all over the output summary window with the graphs and stuff. And nowhere can I find a single byte of trace output.
I had the same problem with VS 2008, in which load tests were a new and crude feature... I thought the upgrade would make things easier, but it hasn't.
All over the web people talk about logging trace messages to a console in these tests as if it were the easiest and most obvious thing in the world. I have no trouble with this sort of trace output during normal debugging, only when running load-tests. Why can't I find a way to do it?
[UPDATE] I found a partial answer: I can see the trace output on test runs that fail. After a run with failures, the page of graphs and stats opens, and at various places, there's a link to the errors. Click that and open the Load Test Errors window, right-click an error and select View Details, and you get a page in which there's an "Additional Information" section, and that section includes any messages written with TestContext.WriteLine. There's also a Debug Trace section that has messages written with Debug.WriteLine.
But I don't see a way to look at this output for a successful run. (And it would be nice to be able to combine certain outputs into a single stream, but I guess I can see why one would have to implement that manually.)
Upvotes: 1
Views: 1547
Reputation: 405
After a lot of search, I have finally found the answer to this one. Please notice that as you noted, by the default setting, it logs only when there is an error (the log under additional information, in the error details screen) The setting for that is under "run settings" properties, section logging, property "Save Log on test Failure" which has the default value "true".
Now to the solution. Beside that property, there is one called "Save Log Frequency for Completed Tests". The default value is 0. This property is used to save a test log for one out of N tests. If you like to have logs created 100% of time, you will then set it to 1. 1 out of 1 is 100%.
After you have done this, to find the log, in the result screen (right click on the load test and choose "open and manage results.."), navigate to the "details" tab, which will show you some graphs. Hover your mouse pointer over the individual tests in the "virtual user activity chart", and you will see a link to the "test log" there. Voila! Mission accomplished.
Upvotes: 2
Reputation: 14076
Perhaps because what you want is not available.
The method I use is to open a StreamWriter
to write to a text file. For some case I use code in the style:
private void WriteLineToTestLog(string line)
{
StreamWriter sw = File.AppendText("C:\\Windows\\Temp\\MyWebtestsLog.txt");
sw.WriteLine(System.DateTime.Now + " " + line);
sw.Close();
}
For tests with many virtual users I worried about the performance of the above code. So I produced a version that has one shared stream protected by a lock:
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();
}
}
}
This is called from plugins in other web tests with code such as:
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);
The log files are written onto the computer where the test executes. As this might be an agent computer, the files will need to be copied from the agent and onto the computer where they are to be analysed. The code also works without error when tests are run with Visual Studio Online but I have not found any way of viewing or collecting the log files from such test runs.
Upvotes: 2