我正在尝试在Devforce Classic中实现自定义日志发布者。我想要做的是将写入Client和Server调试日志的所有内容重定向到实现NLOG的日志发布者。该文档指出您可以实现自己的TraceSubscriber来连接自定义日志发布者。我无法获取任何跟踪消息来解雇我的自定义发布者的方法。是否有可用的样本可以指向正确的方向?
答案 0 :(得分:0)
我找到的唯一示例是一个片段,其中显示了如何直接使用DF TraceSubscriber为已发布的消息设置订阅者:
// 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();
您将无法替换现有的TracePublisher机制,因为它已融入DF,但您可以使用TraceSubscriber直接在与上述类似的代码中,通过继承TraceSubscriber或通过实现ITraceSubscriber来自定义订阅方。订阅者将收到所有跟踪消息,然后您的代码可以确定如何处理它们。
如果您确实想尝试自定义ITraceSubscriber实现,请注意文档(和实现)有点古怪,要连接它,您还必须获取TracePublisher实例或代理并调用其Subscribe方法:
// 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);
订阅后,发布商将调用ITraceSubscriber.OnPublish方法:
public void OnPublish(TraceMessage traceMessage)
{
// do something with trace message
}