Search code examples
devforce

DevForce Classic Implementing a custom TraceSubscriber


I am attempting to implement a custom log publisher in Devforce Classic. What I would like to do is redirect everything written to the Client and Server debug logs to a log publisher implementing NLOG. The documentation notes that you can implement your own TraceSubscriber to wire up the custom log publisher. I am not able to get any trace messages to fire my custom publisher's methods. Is there a sample available that would point me in the right direction?


Solution

  • The only sample I found was a snippet which shows how to directly use the DF TraceSubscriber to set up a subscriber to published messages:

        // Creates either WCF or Remoting instance based on IdeaBladeConfig.
        // Overloads are available, or construct the concrete class directly.
        var traceSubscriber = TraceSubscriber.CreateInstance();   
    
        // The publish handler will be where you take the trace message and log to your own logs.
        traceSubscriber.Publish += PublishHandler;
        traceSubscriber.SubscriptionError += SubscriptionErrorHandler;
    
        // This is overloaded 
        traceSubscriber.StartSubscription(url);
    
        // You can issue this when done.
        traceSubscriber.StopSubscription();
    

    You won't be able to replace the existing TracePublisher mechanism since that's baked into DF, but you can customize the subscriber side using the TraceSubscriber directly in code similar to the above, by subclassing the TraceSubscriber, or by implementing ITraceSubscriber. The subscriber will receive all trace messages, and your code can then determine what to do with them.

    If you do want to try a custom ITraceSubscriber implementation, note that the docs (and implementation) are a bit quirky, and to wire it up you must also obtain a TracePublisher instance or proxy and call its Subscribe method:

      // local (in-process) publisher:
      var publisher = TracePublisher.LocalInstance;
      publisher.Subscribe(this);
    
      // for remote publisher:
      var localURL = TracePublisher.CreateInstance().GetPublisherUrl(null);
      // Use RemotingTracePublisher or WcfTracePublisher 
      var publisher = RemotingTracePublisher.GetRemoteInstance(localURL);
      publisher.Subscribe(this);
    

    Once subscribed, the publisher will call the ITraceSubscriber.OnPublish method:

    public void OnPublish(TraceMessage traceMessage) 
    {
       // do something with trace message
    }