I've noticed that calling Report.Log from a parallel thread doesn't log messages to the main report.
How can I make it log messages from parallel threads so that I can see them on screen at runtime and later on in the report?
Report.Log in parallel threads
Re: Report.Log in parallel threads
I noticed this last week too, support informed me this is not currently possible (out of the box), but will be possible in a future release...
A solution just now though would be to created a dedicated reporting thread.
A solution just now though would be to created a dedicated reporting thread.
Re: Report.Log in parallel threads
Can you please give me a short code example on doing that?
Thanks a lot!
Thanks a lot!
- artur_gadomski
- Posts: 207
- Joined: Mon Jul 19, 2010 6:55 am
- Location: Copenhagen, Denmark
- Contact:
Re: Report.Log in parallel threads
We have come across this issue as well. It's not big enough part of the code that would warrant making a whole reporting thread, but it would be nice if in the future it would simply work.
Re: Report.Log in parallel threads
Here's a workaround log system I've created. It will add the messages from the main thread to the report, and it will write all the messages from other threads to a file.
Code: Select all
using System;
using Ranorex;
using System.IO;
namespace Helper
{
static public class Logger
{
static readonly Object locker = new Object();
public static void LogMessage(string msg)
{
LogMessage(msg, ReportLevel.Info);
}
public static void LogMessage(string msg, ReportLevel reportLevel)
{
string reportLevelString;
if (reportLevel == ReportLevel.Info)
reportLevelString = "Info";
else if (reportLevel == ReportLevel.Error)
reportLevelString = "Error";
else if (reportLevel == ReportLevel.Failure)
reportLevelString = "Failure";
else
reportLevelString = "Misc";
Report.Log(reportLevel, msg);
lock (locker) {
System.IO.StreamWriter sw = System.IO.File.AppendText("mylog.txt");
try
{
string logLine = System.String.Format(
"{0:G}: {1}", System.DateTime.Now, msg);
sw.WriteLine(logLine);
}
finally
{
sw.Close();
}
}
}
}
}
Re: Report.Log in parallel threads
John, do you set up a new thread which calls the logmessage method, if so how do you keep it alive?
This is my way for the time being until its fixed internally -
This is my way for the time being until its fixed internally -
Code: Select all
using System;
using Ranorex;
using System.Collections;
using System.Threading;
namespace Automation
{
public class Logger
{
private static Queue messages;
private static Thread logThread;
private static object setupLock = new object();
private static bool stop, reportIsSetup;
private static void MessageLogger()
{
if(!reportIsSetup){
Report.Setup(ReportLevel.Debug, "threadlog.rxlog", true);
}
while (true) {
if(messages.Count > 0){
logdata data = (logdata)messages.Dequeue();
Report.Log(data._level, data._message);
}
if(stop){
Report.End();
break;
}
Thread.Sleep(300);
}
}
public static void Stop(){
stop = true;
}
public static void LogMessage(ReportLevel level, string message)
{
lock(setupLock){
if(messages == null){
messages = new Queue();
}
if(logThread == null){
logThread = new Thread(new ThreadStart(MessageLogger));
logThread.IsBackground = true;
logThread.Start();
}
}
logdata data = new logdata(level, message);
messages.Enqueue(data);
}
}
public struct logdata{
public ReportLevel _level;
public string _message;
public logdata(ReportLevel level, string message){
_level = level;
_message = message;
}
}
}
Re: Report.Log in parallel threads
I have many different threads in my automation that come and go and I need to log messages from all of them. So far my solution worked fine, even though it's very simple.
Thanks for sharing your code!
Thanks for sharing your code!
Re: Report.Log in parallel threads
Is there a solution for this problem already?
I have the same issue. Messages from a parallel thread are not sent to the Ranorex Report.
I'm using Ranorex 3.3.3
I have the same issue. Messages from a parallel thread are not sent to the Ranorex Report.
I'm using Ranorex 3.3.3
- Support Team
- Site Admin
- Posts: 12145
- Joined: Fri Jul 07, 2006 4:30 pm
- Location: Houston, Texas, USA
- Contact:
Re: Report.Log in parallel threads
Hello,
Currently, we don't support parallel threads for reporting.
Reporting will be simplified in Ranorex version 4.0.
In that version, it should be possible to use several threads for reporting.
Regards,
Markus (T)
Ranorex Support Team
Currently, we don't support parallel threads for reporting.
Reporting will be simplified in Ranorex version 4.0.
In that version, it should be possible to use several threads for reporting.
Regards,
Markus (T)
Ranorex Support Team