Code first, ask questions later
// servicecontract [ServiceContract] public interface ITraceWritable { [OperationContract] void Write(string msg); }
// proxy public partial class RemoteTraceClient : ClientBase<ITraceWritable>, ITraceWritable { public RemoteTraceClient() : base(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/tracer")) { } public void Write(string msg) { Channel.Write(msg); } }
// TraceListener implementation public class RemoteTraceListener : TraceListener { public override void WriteLine(string message) { Write(message + "\n"); } public override void Write(string message) { using (var client = new RemoteTraceClient()) { client.Open(); client.Write(message); client.Close(); } } }
// Service implementation [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)] public class TraceLog : ITraceWritable { public void Write(string msg) { Console.Write(msg); } }
// usage in application to trace to using (ServiceHost host = new ServiceHost(new TraceLog(), new Uri("net.tcp://localhost:9000/tracer"))) { host.AddServiceEndpoint(typeof(ITraceWritable), new NetTcpBinding(), ""); host.Open(); Console.WriteLine("Press enter to exit"); Console.ReadLine(); host.Close(); } // usage in application to trace from System.Diagnostics.Trace.Listeners.Add(new RemoteTraceListener()); System.Diagnostics.Trace.TraceInformation("Added remote tracelistener");
Now my trace messages are directly visible in a console window. This is a great help with debugging. Of course this can be applied to existing applications via the config file….
<system.diagnostics> <trace> <listeners> <add name="remote" type="[NameSpace].RemoteTraceListener, [Assembly]"/> </listeners> </trace> </system.diagnostics>
Thanks for the blurb. A little *.sln with your code would’ve been nice!
Actually it was really easy to make the *.sln never mind 🙂 thanks again!