Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.PluginInfrastructure/3.3/CrossDomainTracing.cs @ 16573

Last change on this file since 16573 was 16102, checked in by abeham, 6 years ago

#2213: some pending changes exploring the topic

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Diagnostics;
24using System.IO;
25using System.Reflection;
26
27namespace HeuristicLab.PluginInfrastructure {
28  public delegate void TraceWriterHandler(string message);
29
30  internal class SynchronizedTraceListener : TraceListener {
31    private TraceWriterHandler messageHandler;
32
33    public SynchronizedTraceListener(TraceWriterHandler writeHandler) {
34      messageHandler = writeHandler;
35    }
36
37    public override void Write(string message) {
38      messageHandler(message);
39    }
40
41    public override void WriteLine(string message) {
42      messageHandler(message + Environment.NewLine);
43    }
44  }
45
46  [Serializable]
47  public sealed class CrossDomainTracer : MarshalByRefObject {
48    private CrossDomainTracer remoteTracer;
49    private SynchronizedTraceListener remoteListener;
50
51    public CrossDomainTracer() {
52    }
53
54    public static void StartListening(AppDomain remoteDomain) {
55      var tracer = new CrossDomainTracer(remoteDomain);
56      Trace.Listeners.Add(new SynchronizedTraceListener(new TraceWriterHandler(Console.Write)));
57    }
58
59    public CrossDomainTracer(AppDomain farDomain) {
60      AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
61      this.remoteTracer = farDomain.CreateInstanceFrom(Assembly.GetExecutingAssembly().Location, typeof(CrossDomainTracer).FullName).Unwrap() as CrossDomainTracer;
62      AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
63      if (remoteTracer != null) {
64        remoteTracer.StartListening(this);
65      }
66    }
67
68    public void StartListening(CrossDomainTracer farTracer) {
69      this.remoteTracer = farTracer;
70      this.remoteListener = new SynchronizedTraceListener(new TraceWriterHandler(Write));
71      Trace.Listeners.Add(this.remoteListener);
72    }
73
74    public void Write(string message) {
75      this.remoteTracer.RemoteWrite(message);
76    }
77
78    public void RemoteWrite(string message) {
79      Trace.Write(message);
80    }
81
82    Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
83      try {
84        Assembly assembly = Assembly.Load(args.Name);
85        if (assembly != null) {
86          return assembly;
87        }
88      } catch { }
89
90      // Try to load by assembly fullname (path to file)
91      string[] Parts = args.Name.Split(',');
92      string File = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\" + Parts[0].Trim() + ".dll";
93
94      return Assembly.LoadFrom(File);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.