有没有扩展 System.Diagnostics.Trace 的库?

发布于 2024-08-31 16:35:18 字数 249 浏览 4 评论 0原文

有没有什么好的库可以扩展 System.Diagnostics.Trace?

我正在寻找的一些功能。

  • 滚动日志
  • smtp

和“使用 log4net”不是答案。原因是我不想引用任何第三方程序集。

回应评论: 因为使用trace不会污染业务代码。我只调用Trace.XXX。使用 Trace 扩展可以在配置中或启动时的少量代码中完成。如果我使用 log4net 则需要到处引用它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

烙印 2024-09-07 16:35:18

您还可以查看 Ukadc.Diagnostics 此处

它包含几个 TraceListener。更有趣的是,它增加了使用格式化语句的能力(就像 log4net 和 NLog 支持的那样)。因此,您可以更好地控制日志文件的布局,包括记录哪些字段、字段的顺序、至少某些字段的格式(例如数据/时间格式)。您甚至可以编写自己的“标记”,然后在格式化语句中引用它们。它不支持与 log4net 或 NLog 一样多的格式化选项,也不支持几乎相同数量的 TraceListener,但是,对于基于 System.Diagnostics 的解决方案,它是“开箱即用”功能的真正进步在系统诊断中可用。

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.

请持续率性 2024-09-07 16:35:18

这对于库来说不算,但我编写了一次文本框侦听器,以便我可以将文本框放在表单上,​​并自动获取跟踪输出......

  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);
      }
    }
  }

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);
      }
    }
  }
不顾 2024-09-07 16:35:18

http://essentialdiagnostics.codeplex.com/ 提供默认跟踪的扩展

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

昨迟人 2024-09-07 16:35:18

我理解为什么您不想因为依赖第三方 API 而污染您的业务代码。

然而,事实是 System.Diagnostics.Trace 不像 EntLib 和 Log4Net 等其他日志框架提供的 API 那样灵活。

我所做的是开发一个受 log4net 强烈影响的内部 API,它使用提供者模型设计模式在任何合适的日志记录框架上提供一个薄包装器。我有 System.Diagnostics.Trace、log4net 和 EntLib 的提供程序。基本概念与 The Common Infrastructure for .NET 日志记录库非常相似。

这样你的业务代码只依赖于你自己的内部API,并且可以在运行时使用配置选择日志框架。

当然,您可以通过坚持使用 System.Diagnostics.Trace 并为 SMTP 编写自己的 TraceListeners 来实现您想要的目标 (查看此 CodeProject 示例)或滚动日志。或者编写您自己的 TraceListener,重定向到 Log4Net 等日志记录框架,然后您可以访问该框架支持的记录器。

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.

悍妇囚夫 2024-09-07 16:35:18

企业库日志记录应用程序块“扩展”了 System.Diagnostics.TraceSource 等类。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文