5

I'm trying to write a custom trace listener for Enterprise Library Logging which sends all log messages to an arbitrary WCF endpoint. The idea behind this is that I can set up a simple console app, etc at the other end which prints out all log messages in real time.

My question is in two parts:

  1. Is there a mechanism to do this already? I already looked at the MSMQ listener and I'm not interested in using that because I may have a need to use a different protocol/binding at some point.
  2. The way I have it implemented below - is it efficient enough or is there a better way? My concern is that every time a message comes through from the Logger (which may be frequent) I'm opening a new channel and then slamming it shut. Will this cause performance issues?

In my sample RemoteClient derives from ClientBase<T>.

[ConfigurationElementType(typeof(CustomTraceListenerData))]
public class RemoteTraceListener : CustomTraceListener
{
    public override void Write(string message)
    {
        RemoteClient client = new RemoteClient();
        client.Open();
        client.Write(message);
        client.Close();
    }

    public override void WriteLine(string message)
    {
        RemoteClient client = new RemoteClient();
        client.Open();
        client.WriteLine(message);
        client.Close();
    }

    public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
    {
        if (data is LogEntry && this.Formatter != null)
        {
            WriteLine(this.Formatter.Format(data as LogEntry));
        }
        else
        {
            WriteLine(data.ToString());
        }
    }
}
Jacobs Data Solutions
  • 4,850
  • 4
  • 33
  • 38

3 Answers3

0

How often is this writing? I suggest WCF streaming as a better alternative of you're going to be logging frequently.

Failing that, it's probably a good idea to keep the client instance around as long as possible. You could try pooling it.

Thilak Nathen
  • 1,333
  • 1
  • 8
  • 13
  • I'm not clear on what you mean by 'pooling' it. Is there some kind of caching mechanism in IIS I can use or do you mean implementing it as a singleton of some kind? Also, what do you mean by streaming? You mean like using a sessionful TCP binding? That would mean keeping the channel open, correct? I'd like to know what the implications of that would be for performance, system stability, etc. FYI, right now I'm using the netMSMQ binding so sessions don't seem to be an option if I keep going down this road. – Jacobs Data Solutions Aug 17 '11 at 18:45
  • I believe that `ClientBase` already implements what Thilak means by 'pooling'. It's essentially caching a ChannelFactory, so that when you `new Client` it will reuse the existing, non-faulted Client (behind the scenes caching) – Kirk Broadhurst Aug 22 '11 at 04:17
0

I found an open-source project called 'CLog' which does exactly what I'm looking for: http://clog.codeplex.com/.

A brief glance at the source code shows that he's using a singleton object to keep track of all the open channels that will receive log messages, and he's going with ChannelFactory<TChannel> as opposed to ClientBase<T> to instantiate each channel proxy. There are some threading implications that need to be addressed but I still think this is the way to fly.

It shouldn't be too hard to use this as a starting point for the implementation of my own custom trace listener which logs to WCF endpoints.

Jacobs Data Solutions
  • 4,850
  • 4
  • 33
  • 38
-1

I think you should use a SQL database on witch you should log to because if you logg in a console app you could not see for examle something before 2 days.While in SQL you can make a quote and get the right data you need. Another solution is to use the log4net project.

Bosak
  • 2,103
  • 4
  • 24
  • 43
  • We're already using the flat file trace listener and will probably be adding a SQL trace listener as well. I'm adding this in addition to the other trace listeners so that I can view log data in real time. – Jacobs Data Solutions Aug 18 '11 at 15:35