#region License Information /* HeuristicLab * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Diagnostics; using System.IO; using System.Reflection; namespace HeuristicLab.PluginInfrastructure { public delegate void TraceWriterHandler(string message); internal class SynchronizedTraceListener : TraceListener { private TraceWriterHandler messageHandler; public SynchronizedTraceListener(TraceWriterHandler writeHandler) { messageHandler = writeHandler; } public override void Write(string message) { messageHandler(message); } public override void WriteLine(string message) { messageHandler(message + Environment.NewLine); } } [Serializable] public sealed class CrossDomainTracer : MarshalByRefObject { private CrossDomainTracer remoteTracer; private SynchronizedTraceListener remoteListener; public CrossDomainTracer() { } public static void StartListening(AppDomain remoteDomain) { var tracer = new CrossDomainTracer(remoteDomain); Trace.Listeners.Add(new SynchronizedTraceListener(new TraceWriterHandler(Console.Write))); } public CrossDomainTracer(AppDomain farDomain) { AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve); this.remoteTracer = farDomain.CreateInstanceFrom(Assembly.GetExecutingAssembly().Location, typeof(CrossDomainTracer).FullName).Unwrap() as CrossDomainTracer; AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve); if (remoteTracer != null) { remoteTracer.StartListening(this); } } public void StartListening(CrossDomainTracer farTracer) { this.remoteTracer = farTracer; this.remoteListener = new SynchronizedTraceListener(new TraceWriterHandler(Write)); Trace.Listeners.Add(this.remoteListener); } public void Write(string message) { this.remoteTracer.RemoteWrite(message); } public void RemoteWrite(string message) { Trace.Write(message); } Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { try { Assembly assembly = Assembly.Load(args.Name); if (assembly != null) { return assembly; } } catch { } // Try to load by assembly fullname (path to file) string[] Parts = args.Name.Split(','); string File = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + Parts[0].Trim() + ".dll"; return Assembly.LoadFrom(File); } } }