The logging Application Block in Enterprise Library 2.0 is the logging and instrumentation Application Block in version 1.0. It is renamed as the logging Application Block because it focuses on logging. The logging Application Block provides a unified logging function. It supports writing logs to multiple destinations, such as: 1. database; 2. text files; 3. Email; 4. Message Queue; 5. WMI event; 6. Event Logs; 7. Custom regions (for example: XML file.
The logging Application Block provides a unified interface for writing logs to any specified destination.CodeSpecify the location where the log information should be written, but set the conditions in the configuration file.
This means that the operator can change the log record behavior without modifying the code like our developers. It applies to usProgramDevelopment has the following benefits:
1. Make the logging methods in our applications consistent;
2. It is precisely because it provides a consistent structure model that greatly facilitates our developers to learn about it;
3. Solved the log problem in the application;
4. scalable. We can customize Log Information Filtering programs and formatting log information programs.
......
Compared with the previous version, logging Application Block 2.0 has made the following changes:
1. The logentry object can now belong to one or more categories );
2. In version 2.0, We can customize the filter. This information will be filtered out before the logging Application Block sends the log information to the listener (trace listeners, filters allow you to filter events based on their category and/or priority. You can customize filters that meet your needs to filter events;
3. You can query the filter in the code to determine whether the current event needs to be logged. This greatly reduces the number of log records and effectively improves application performance.
The following describes how to use the logging Application Block. First, you must add references to the following three sets:
Using Microsoft. Practices. enterpriselibrary. logging;
Using Microsoft. Practices. enterpriselibrary. Logging. extrainformation;
Using Microsoft. Practices. enterpriselibrary. Logging. filters;
Like the previous blocks, let's talk about the configuration method before using it. First, use the configuration tool to open the app of our program. config/web. config, right-click the application, and choose new --> logging Application Block, for example:
At this time, you will find that several nodes such as filters, category Source, special source, trace listeners, and formatters automatically appear under the newly created logging Application Block node, as shown below:
The following describes the functions of each node according to the configuration sequence. The first step is formatters, which is used to specify the log information format. For example, we create a text formatter under the formatters node, as shown below:
Then we can edit its template and edit the information we want to display, as shown below:
We can also configure binary formatter and custom formatter.
After formatter is configured, We can configure trace listeners. Enterprise Library 2.0 provides seven trace listeners for recording log information to a specific destination, configure a flat file trace listener as follows:
After flat file trace listener is used, the recorded log information is output to the specified text file. Therefore, we need to specify the path of the output text file, which is in the bin/DEBUG directory by default; at the same time, we need to specify attributes such as formatter and name of the listener. If we configure a Database Listener, we need to create a corresponding table in the database and create a stored procedure for Inserting log information.
After the trace listeners is configured, you can configure the category Source. Create a new category source named General and set sourcelevel to all, as shown below:
Then, we can add one or more trace listener to the newly added category source. In this article, we add the shy52o formatter configured above,
For example, select shy520 listeners in refrencetracelistener.
Finally, let's take a look at how to configure filters. Enterprise Library 2.0 provides three default filters: Category filter, log enable filter, and priority filter, we use the category filter as an example to configure:
You can then modify the Filter Name and filter rules:
Here we can select the filtering mode. There are two filtering modes:
1. Allow all categories should t those explicitly denied below
2. Deny all categories should t those explicitly allowed below
Select the filtering mode as needed, add the category to be filtered, and click OK to complete the configuration of the category filter.
Here is the introduction and configuration of the logging Application Block. Next we will introduce how to use the logging Application Block and how to customize formatter and listener.
If you have any mistakes, please correct them! I hope it will be helpful for beginners and can communicate with you!
Email: pwei013@163.com