Report.Log in parallel threads

Ask general questions here.
johnsmith
Posts: 18
Joined: Sun Mar 18, 2012 2:13 am

Report.Log in parallel threads

Post by johnsmith » Mon Mar 19, 2012 5:55 pm

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?

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Report.Log in parallel threads

Post by sdaly » Mon Mar 19, 2012 7:23 pm

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.

johnsmith
Posts: 18
Joined: Sun Mar 18, 2012 2:13 am

Re: Report.Log in parallel threads

Post by johnsmith » Mon Mar 19, 2012 8:14 pm

Can you please give me a short code example on doing that?
Thanks a lot!

User avatar
artur_gadomski
Posts: 207
Joined: Mon Jul 19, 2010 6:55 am
Location: Copenhagen, Denmark
Contact:

Re: Report.Log in parallel threads

Post by artur_gadomski » Tue Mar 20, 2012 10:08 am

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.

johnsmith
Posts: 18
Joined: Sun Mar 18, 2012 2:13 am

Re: Report.Log in parallel threads

Post by johnsmith » Tue Mar 20, 2012 1:37 pm

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();
			    }
        	}
		}
        
	}
}

User avatar
sdaly
Posts: 238
Joined: Mon May 10, 2010 11:04 am
Location: Dundee, Scotland

Re: Report.Log in parallel threads

Post by sdaly » Tue Mar 20, 2012 4:54 pm

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;
		}
	}
}

johnsmith
Posts: 18
Joined: Sun Mar 18, 2012 2:13 am

Re: Report.Log in parallel threads

Post by johnsmith » Tue Mar 20, 2012 5:30 pm

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!

Hermch
Posts: 40
Joined: Thu May 26, 2011 7:17 am
Location: Germany

Re: Report.Log in parallel threads

Post by Hermch » Thu Oct 25, 2012 10:44 am

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

User avatar
Support Team
Site Admin
Site Admin
Posts: 11709
Joined: Fri Jul 07, 2006 4:30 pm
Location: Graz, Austria

Re: Report.Log in parallel threads

Post by Support Team » Thu Oct 25, 2012 4:21 pm

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
.
Image