Any libraries that extend System.Diagnostics.Trace?

Are there any good libraries that extend System.Diagnostics.Trace?

Some features I'm looking for.

  • Log Logs
  • SMTP

And "Use log4net" is not the answer. The reason is that I do not want to reference any third-party assemblies.

In response to comments: Since using tracing does not pollute the business code. I call Trace.XXX. Using the extension for tracing can be done in the configuration or a small amount of code at startup. If I use log4net, you need links to it everywhere.

+4
source share
5 answers

I understand why you do not want to pollute your business code with dependency on a third-party API.

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

I developed an internal API that relies heavily on log4net, which uses a provider model design pattern to provide a thin shell 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 Protocol Library .

Thus, your business code depends only on your own internal API, and the logging environment can be selected at runtime using the configuration.

Of course, you can achieve what you want by sticking to System.Diagnostics.Trace and write your own TraceListeners for SMTP ( See this CodeProject example ) or shafts. Or write your own TraceListener, which redirects to a registration framework such as Log4Net, which then gives you access to the registrars supported by this infrastructure.

+1
source

You can also check out Ukadc.Diagnostics here

It contains several TraceListeners. More interestingly, it adds the ability to use formatting instructions (for example, that supports log4net and NLog). Thus, you have much more control over the layout of your log file, including registering fields, arranging fields, formatting at least some fields (e.g., data / time formatting). You can even write your own tokens and then refer to them in formatting instructions. It does not support almost as many formatting options as log4net or NLog, and also does not support almost the number of TraceListeners, but for a solution based on System.Diagnostics it is a real step forward from the out-of-the-box capabilities available in System.Diagnostics.

+3
source

This does not count for the library, but I once wrote a text field listener to just remove the text field on the form and automatically get the 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); } } } 
+2
source

http://essentialdiagnostics.codeplex.com/ provides default trace extensions

+2
source

The enterprise logging application block extends the System.Diagnostics.TraceSource classes, etc.

+1
source

Source: https://habr.com/ru/post/1309385/


All Articles