Trace against registration and how does log4net enter it?

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

The main difference is that trace is a more detailed log that gives developers a tool for debugging applications at runtime?

I experimented with log4net and logged. Now I am wondering if I should also keep track of the trace, and if I could / should use log4net for this purpose. Do I have to trace using log4net and is there any trace level for log4net loggers? Should I use a different log level for debugging and tracing, or is it ok to use the same? Can you give a simple example of how I will log and trace for a simple method?

Edit: Despite a few useful answers below, I'm still not sure how I should track and log.

I have the following method at my business level and I want to add a log / trace to it. I am wondering how to do this efficiently. Is the following method acceptable in terms of logging / tracing? Should log messages be of type Info instead of Debug? Are Debug Messages Tracked? 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"); } } 
+44
logging tracing log4net
Oct 05 '08 at 18:07
source share
7 answers

Registration is a general term for recording information. Tracing is a specific form of logging used for debugging.

In .NET, the System.Diagnostics.Trace and System.Diagnostics.Debug objects allow simple registration for several "event listeners" that you can configure in app.config. You can also use TraceSwitches to configure and filter (for example, between errors and information layers).

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

ASP.NET has a special version of Trace (System.Web.TraceContext) that will be written to the bottom of the ASP or Trace.axd page. ASP.NET 2+ also has a more complete logging system called Health Monitoring.

Log4Net is a richer and more flexible way to track or log than built-in tracing or even monitoring ASP health. Like Diagnostics.Trace, you configure event listeners ("appenders") in config. For simple tracing, use is as simple as built-in tracing. The decision to use Log4Net is whether you have more complex 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"); } } 
+22
Oct 05 '08 at 19:45
source share

IMO ...

Logging should not be intended for debugging development (but it is inevitably used this way)
Logging should be designed for operational monitoring and troubleshooting - this is his raison dêtre. Tracing should be designed to develop debugging and tuning performance. If this is available in the field, it may be useful for truly low-level operational failures, but this is not its primary purpose.

Given this, the most successful approaches that I have seen (and developed / implemented) in the past do not combine them together. It is better to keep both tools separately, each of which carries out one task, and it is also possible.

+12
05 Oct '08 at 19:33
source share

log4net works well for both. We distinguish between logging, which is useful for post-release diagnostics and "tracing" for development purposes, using the DEBUG logging level. In particular, developers write down their trace output (things that are of interest only during development) using Debug() . Our development configuration sets the level at DEBUG:

 <root> <level value="DEBUG" /> ... </root> 

Before the release of the product, the level changes to "INFO":

 <level value="INFO" /> 

This removes all DEBUG output from the release log, but retains INFO / WARN / ERROR.

There are other log4net tools, such as filters, hierarchical (by namespace), many goals, etc., we found that the simple method above is quite effective.

+11
Oct 05 '08 at 19:18
source share

The magazine does not track . The two should be different libraries with different performance characteristics. In fact, I wrote one trace library myself with a unique property that can automatically trace an exception when a method with tracing enabled remains an exception. In addition, you can more efficiently solve the problem of throwing exceptions in certain places in your code.

+8
Jul 21 '10 at
source share

I would say yes. Registration is the only way to determine what happened in the past - if a client calls and says that something is not as expected, without a log, all you can do is shrug and try to reproduce the error. Sometimes this is not possible (depending on the complexity of the software and depending on the client’s data).

There is also a question about registration for audit, the log file can be written with information about what the user is doing, so you can use this to narrow down the debugging capabilities of the problem or even check the requirements of users (if you received a message about a system violation, xyz did not happen , you can look in the logs to find out that the operator could not start this process or did not right-click to make it work)

Then reporting is recorded, and this is what most people think of registration.

If you can configure the log output, put everything in the logs and decrease or increase the amount of data that will be written. If you can dynamically change the output level, then this is ideal.

You can use any methods of logging, taking into account performance problems. I believe that adding to a text file is the best, most portable, easy to view and (very important) easiest to extract when you need it.

+4
Oct 05 '08 at 18:26
source share

logging! = debugging

Sometimes to solve problems with the client it is necessary to store log files, they prove what happened on the server side.

0
Oct 05 '08 at 18:10
source share

Also consider what information is logged or tracked. This is especially true for senstive information.

For example, although it is usually normal to write an error message

"User" X "tried to access but was rejected due to an incorrect password,"

failed to register error message

"User" X "tried to access but was rejected because the password" secret "is incorrect."

It might be acceptable to write such sensitization information into a trace file (and notify the client / user about this fact in “some ways” before you ask him to turn on trace for advanced troubleshooting during production). However, for logging, I always have a policy according to which such senstive information is never written (that is, INFO levels and higher in log4net say).

This, of course, should be force checked and verified by looking at the code.

0
Oct 06 '08 at 6:07
source share



All Articles