If errors, exceptions, or crashes occur after the project is officially launched
The first thing we often think of is to view logs.
Therefore, logs are very important for the maintenance of a system.
All log systems
A log system is often a program;
Imagine if your logs are completely provided by third-party components;
That means your dll;
Maybe you can encapsulate it twice, so you still need to reference your encapsulated log project for all projects.
On the other hand
Some log components need to be instantiated before they can be used, such as log4net. This also means that you can encapsulate them again.
However, Microsoft has provided us with two very convenient static classes for logging.
System. Diagnostics. Trace and System. Diagnostics. Debug
For more information about the two classes, see MSDN.
System. Diagnostics. Trace
System. Diagnostics. Debug
His use is so convenient that you will fall in love with him once you use it.
You don't need to reference any dll, because it is Microsoft's own thing, it is in System. dll.
It's easy to call his method.
.TraceError(.TraceWarning(.TraceInformation(.WriteLine(.Flush();
Of course, there are more than four methods. For more information, see MSDN.
Trace and Debug call methods are identical. The difference lies in
All Debug methods have
[()]
It indicates that in the Release mode (), this is not executed, but not compiled into the program)
That is to say, the Debug. XXX () method only runs in Debug mode, which saves us a lot of trouble.
Log rewriting implementation
The Console of Trace and Debug methods is the same as that of Console. Write.
However, we change the TraceListener listener to implement more operations.
Required methods include:
Write( WriteLine( message);
However, you can also manually override other methods.
Write a MyTraceListener
Write( WriteLine(, DateTime.Now.ToString() + message +
Trace. Listeners
Main( Trace.Listeners.Add( MyTraceListener()); }
In a method test
i = / i); + ex.Message);
For example
Write( o, msg = (.IsNullOrWhiteSpace(category) == ) = category + (o Exception) ex == ex.Message ++= (o != =
i = / i); }}
The rest.
Initialize the listener through the configuration file
Initializing the listener through the configuration file is a little more complicated than writing code directly, but it is also more convenient. We can quickly set the log listener without re-compiling the system.
Especially in Web projects, this will become more convenient
I have just compiled MyTraceListener into a project as a dll.
Add a constructor and the FilePath attribute to set the location of the log file.
FilePath {; MyTraceListener (= Write (WriteLine () + message + Write (o, msg = (. isNullOrWhiteSpace (category) = category + (o Exception) ex = + = ex. message ++ = (o! =MyTraceListener configuration file
The type parameter can be obtained in this way.
typeof(MyLog.MyTraceListener).AssemblyQualifiedName
Version, Culture, and PublicKeyToken can also be ignored.
Test
No problem
If you use third-party components such as log4net, you only need to reference log4net in the project that implements TraceListener.
Finished... bye-bye ~~
Code download
LogDemo.rar
# The wheat brothers on the 13th floor wrote well at on.
However, I have two questions:
1. Currently, it is written to a text file. If it is also written to a system event or database, what should I do?
2. Third-party components such as the Enterprise Library can be set to generate logs cyclically by date or by file size. How can this problem be solved?
# Xiaoxiang blow rain blog on the 14th floor, this Trace method often appears when many write operations are performed, many files named by guids are frequently written.
# Censhao is good at a.m. on the 23rd floor. It is applicable to simple programs.
For example, log4net is easy to use in programs that can easily generate several GB of log files.
Do you really read the article? It's not something my opponent sent to play with me .....
Okay, I'm not sure. I'll concentrate on these questions and give them a simple answer with a chestnut.
= MyTraceListener(= = Write( WriteLine() + message +