跟踪与记录和如何log4net的适应? [英] Tracing versus Logging and how does log4net fit in?

查看:150
本文介绍了跟踪与记录和如何log4net的适应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有关记录和跟踪之间的区别是什么。

时的差别基本上是跟踪更详细的日志为开发人员提供一种工具来调试应用程序在运行时?

我一直在尝试log4net的和做记录。现在,我想知道是否我应该做的追踪,以及,如果我可以/应该使用log4net的用于这一目的。 我应该做的跟踪与log4net的,是那里的log4net记录器的一些跟踪级别?我应该使用不同的日志级别调试和跟踪目的还是确定使用相同的? 你能举一个简单的例子,我怎么会做记录和跟踪一个简单的方法是什么?

编辑: 尽管下面我一些有用的答案,我仍然不确定我应该怎么做跟踪与记录。

我在我的业务层以下的方法,我想补充记录/跟踪它。我想知道如何有效地做到这一点。是下面的方法在日志/跟踪方面可以接受吗?如果日志消息是类型信息,而不是调试的?是调试消息,我认为记录痕迹?你将如何改变呢?


IEnumerable的<车> GetCars()
{
   尝试
   {
      logger.Debug(获得汽车);
      IEnumerable的<车>轿车= CarAccessor.GetCars()ConvertAll(DataAccessToBusinessConverter)。
      logger.Debug(得到总+​​ cars.Count +车);
   }赶上(例外五){
      logger.Error(错误获取汽车的时候,E);
      (让汽车时意外错误)抛出新的异常;
   }
}

解决方案

日志是通用术语,用于记录信息 - 跟踪是伐木用于调试的具体形式。

在.NET中System.Diagnostics.Trace和System.Diagnostics.Debug对象允许简单的日志记录了一些事件侦听器,你可以在App.config中配置。您还可以使用TraceSwitches配置和过滤器(错误和信息之间的水平,例如)。

 私人无效TestMethod的(字符串x)
{
    如果(x.Length→10)
    {
        Trace.Write(字符串为+ x.Length);
        抛出新的ArgumentException(字符串太长);
    }
}
 

在ASP.Net,有跟踪(System.Web.TraceContext)的一个特殊版本将写入ASP页面或Trace.axd的底部。 在ASP.Net 2 +,还有被称为健康监测一个更全面的日志框架。

log4net的是追查或登录比内置跟踪,甚至ASP健康监测的更丰富,更灵活的方式。就像Diagnostics.Trace您配置的事件监听器(追加程序)的配置。对于简单的跟踪,使用简单,如内置的跟踪。使用log4net的决定是你是否有更复杂的要求。

 私人无效TestMethod的(字符串x)
{
    Log.Info(字符串长度+ x.Length);
    如果(x.Length→10)
    {
        Log.Error(字符串为+ x.Length);
        抛出新的ArgumentException(字符串太长);
    }
}
 

I am wondering about what the difference between logging and tracing is.

Is the difference basically that tracing is more detailed log giving developers a tool to debug applications at runtime?

I have been experimenting with log4net and doing logging. Now I am wondering if I should be doing tracing as well and if I could/should use log4net for that purpose. Should I be doing tracing with log4net and is there some trace level for log4net loggers? Should I use a different log level for debug and trace purposes or is it ok to use the same? Can you give a simple example on how I would do logging and tracing for a simple method?

Edit: Despite a few helpful answers below I am still unsure how I should be doing tracing versus logging.

I have the following method in my Business layer and I want to add logging/tracing to it. I am wondering how to do it efficiently. Is the following method acceptable in terms of logging/tracing? Should the log messages be of type Info instead of Debug? Are the Debug messages I am logging considered trace? How would you change it?


IEnumerable<Car> GetCars()
{
   try
   {
      logger.Debug("Getting cars");
      IEnumerable<Car> cars = CarAccessor.GetCars().ConvertAll(DataAccessToBusinessConverter);
      logger.Debug("Got total of " + cars.Count + " cars"); 
   } catch (Exception e) {
      logger.Error("Error when getting cars", e);
      throw new Exception("Unexpected error when getting cars");
   }
}

解决方案

Logging is the generic term for recording information- tracing is the specific form of logging used to debug.

In .Net the System.Diagnostics.Trace and System.Diagnostics.Debug objects allow simple logging to a number of "event listeners" that you can configure in app.config. You can also use TraceSwitches to configure and filter (between errors and info levels, for instance).

private void TestMethod(string x)
{
    if(x.Length> 10)
    {
        Trace.Write("String was " + x.Length);
        throw new ArgumentException("String too long");
    }
}

In ASP.Net, there is a special version of Trace (System.Web.TraceContext) will writes to the bottom of the asp page or Trace.axd. In ASP.Net 2+, there is also a fuller logging framework called Health Monitoring.

Log4Net is a richer and more flexible way of tracing or logging than the in-built Trace, or even ASP Health Monitoring. Like Diagnostics.Trace you configure event listeners ("appenders") in config. For simple tracing, the use is simple like the inbuilt Trace. The decision to use Log4Net is whether you have more complicated requirements.

private void TestMethod(string x)
{
    Log.Info("String length is " + x.Length);
    if(x.Length> 10)
    {
        Log.Error("String was " + x.Length);
        throw new ArgumentException("String too long");
    }
}

这篇关于跟踪与记录和如何log4net的适应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆