views:

59

answers:

4

Any there any good libraries out there that extend System.Diagnostics.Trace?

Some of the features I am looking for.

  • Rolling logs
  • smtp

And "Use log4net" is not an answer. The reason being is that i don't want a reference to any third party assemblies.

In response to comments: Because using trace does not pollute the business code. I only call Trace.XXX. Using an extension to Trace can be done in config or a small amount of code at startup. If I use log4net is need references to it everywhere.

A: 

This doesn't count for a library, but I wrote a Text box listener once so that I could just drop the text box on a form, and automagically get trace output....

  public class TraceLogTextBox : RichTextBox
  {
    private TraceListener listener;

    public TraceLogTextBox()
      : base()
    {
      listener = new TextBoxTraceListener(this);
      Trace.Listeners.Add(listener);
    }

    protected override void Dispose(bool disposing)
    {
      Trace.Listeners.Remove(listener);
      base.Dispose(disposing);
    }

  }

  public class TextBoxTraceListener : TraceListener
  {
    private RichTextBox txtBox;
    const string ERROR = "error";

    public TextBoxTraceListener(RichTextBox tbox)
      : base()
    {
      txtBox = tbox;
    }

    public override void Write(object o)
    {
      if (o is Exception)
      {
        Write(o.ToString(), ERROR);
      }
      else
      {
        Write(o.ToString());
      }
    }

    public override void Write(string message)
    {
      Write(message, string.Empty);
    }

    public override void Write(string message, string category)
    {
      Color col = category.ToLower() == ERROR ? Color.Red : Color.Black;
      txtBox.SelectionColor = col;
      if (category == string.Empty)
      {
        txtBox.SelectedText = message;
      }
      else
      {
        txtBox.SelectedText = string.Format("[{0}] {1}", category, message);
      }
    }

    public override void WriteLine(string message)
    {
      WriteLine(message, string.Empty);
    }

    public override void WriteLine(object o)
    {
      if (o is Exception)
      {
        WriteLine(o.ToString(), ERROR);
      }
      else
      {
        WriteLine(o.ToString());
      }
    }

    public override void WriteLine(string message, string category)
    {
      Color col = category.ToLower() == ERROR ? Color.Red : Color.Black;
      txtBox.SelectionColor = col;
      if (category == string.Empty)
      {
        txtBox.SelectedText = string.Format("{0}\n",message);
      }
      else
      {
        txtBox.SelectedText = string.Format("[{0}] {1}\n", category, message);
      }
    }
  }
Tim Jarvis
+1  A: 

The Enterprise Library Logging Application Block "extends" the System.Diagnostics.TraceSource, etc. classes.

John Saunders
+1  A: 

I understand why you don't want to pollute your business code with a dependency on a third party API.

However it is also a fact that System.Diagnostics.Trace is not as flexible an API as that provided by other logging frameworks like EntLib and Log4Net.

What I've done is developed an internal API that is strongly influenced by log4net, that uses a provider-model design pattern to provide a thin wrapper over any suitable logging framework. I have providers for System.Diagnostics.Trace, log4net and EntLib. The basic concept is very similar to The Common Infrastructure for .NET logging library.

In this way your business code only has a dependency on your own internal API, and the logging framework can be selected at runtime using configuration.

Of course you can achieve what you want by sticking to System.Diagnostics.Trace and writing your own TraceListeners for SMTP (See this CodeProject sample) or rolling logs. Or write your own TraceListener that redirects to a logging framework such as Log4Net, which then gives you access to the loggers supported by that framework.

Joe
+1  A: 

You might also check out Ukadc.Diagnostics here

It contains several TraceListeners. More interestingly, it adds the ability to use formatting statements (like what log4net and NLog support). So, you have much more control over the layout of your logfile, including which fields are logged, the ordering of the fields, formatting of at least some of the fields (like data/time formatting). You can even write your own "tokens" and then reference them in the formatting statements. It does not support nearly as many formatting options as log4net or NLog and also does not support nearly the number of TraceListeners, but, for a System.Diagnostics-based solution, it is a real step up from the "out of the box" capabilities available in System.Diagnostics.

wageoghe