Page 1 of 1

Report.Log in parallel threads

Posted: Mon Mar 19, 2012 5:55 pm
by johnsmith
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?

Re: Report.Log in parallel threads

Posted: Mon Mar 19, 2012 7:23 pm
by sdaly
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.

Re: Report.Log in parallel threads

Posted: Mon Mar 19, 2012 8:14 pm
by johnsmith
Can you please give me a short code example on doing that?
Thanks a lot!

Re: Report.Log in parallel threads

Posted: Tue Mar 20, 2012 10:08 am
by artur_gadomski
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

Posted: Tue Mar 20, 2012 1:37 pm
by johnsmith
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

Posted: Tue Mar 20, 2012 4:54 pm
by sdaly
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 -

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

Posted: Tue Mar 20, 2012 5:30 pm
by johnsmith
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!

Re: Report.Log in parallel threads

Posted: Thu Oct 25, 2012 10:44 am
by Hermch
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

Re: Report.Log in parallel threads

Posted: Thu Oct 25, 2012 4:21 pm
by Support Team
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