Microsoft enterprise database 4.1 learning notes () log module uses the Log Module in applications

Source: Internet
Author: User

 

 

Is the basic configuration of a log module. This section describes the content and code examples of this module.

By default. the configuration information in the config file is in plain text. the built-in encryption/Decryption protection mechanism of NET encrypts the configuration information, or uses the protection mechanism provided by the enterprise database. You can select from the configuration tool, as shown in

  

 

 

You can choose either of them. After saving them, you can open the web. in the config file, you will find that the log configuration section in the original plain text is changed to the following encryption format (developers do not have to worry about decryption, And will automatically decrypt it when used ).

 

  

 

 

 

The Log Module of the enterprise database can record the log information in multiple forms, such as system events, text files, emails, event viewer, database, message queue, and xml format, shows how to create a log record.

 

As shown in, right-click the Trace Listener and select New. Many options are displayed. Each option represents a record format, which can be said to be the purpose and position of the record.

It is a database record log, which needs to be used together with the data access module. A DatabaseInstance is required, and there must be two stored procedures: AddCategoryStoreProcedureName, the name of the stored procedure for adding a category, and WriteLogStoreProcedureName, name of the stored procedure for writing logs to the database.

The advantage of record in the database is convenient statistics, query, you can use the database T-SQL capabilities for log statistics and query.

You can use the example database of the enterprise database to experiment with the database record log, as shown in the example SQL database of the installation directory. Run it in the query analyzer to create databases, tables, and stored procedures.

 

The example database is as follows:

 

 

Configuration of trace listener, category, and database access module is as follows:

 

 

The Code is the final sample code. Now there is another log record location in the Trace group, that is, the database Just configured. After running the code, you will find some more records when you open the database.

 

Is To Write File logs, in the format of text files, you can set the directory, as shown in the trace under the logs directory. log File. the filename attribute supports some system variables, such as % WINDIR %, % TEMP %, and % USERPROFILE %.

With the use of the system, log files will become larger and larger. In this case, a file will become several hundred MB in a day and cannot be opened. If I want to delete the file three months ago, what should we do? It seems that there is no way. You only need to open the file and delete it, which is too troublesome. There is also the file name. I want to add a directory in front. The directory is the current DATE, for example, 2009-9-9, but it seems that the system variable % DATE % is not useful here, I don't know if I used it wrong or what happened. I hope you can give me some advice.

 

 

Click the icon next to the Template... You can open the file template settings, that is, the file record content, which can be adjusted as needed.

The above text file logs may encounter two problems:

1) As the system uses more and more files, it is best to have one file per day, one file per hour, or one file per 10 MB.

2) It is recommended that the files be automatically placed under the folder of the day, and then a folder is created every month. In this way, you can also process logs of a fixed period and delete the logs to get them and store them elsewhere.

The first problem can be solved using Roll Flat File Trace Listener. As shown in, you can set the Interval of the loop, and RollInterval is the cycle, it contains minutes, hours, days, months, years, and daily nights. RollFileExistsBehavior is how to handle a file with the same name, which can be overwritten or auto-incrementing. RollSizeKB is used to set the file size. After the file size is exceeded, a file is automatically created to move the old logs to the file.

I cannot find a solution to the second problem, because I thought it was useless to set filename to % DATE % \ trace. log. You can set it to % WINDIR % \ trace. log to create the log file trace. log in the C: \ WINDOWS directory. I don't know if I am using it wrong, or is it not supported originally? Hope you can give me some advice. Thank you !!!

Sometimes we may think that the record is in xml format, and we can use C # for parsing, displaying, querying, and viewing. However, I encountered a problem, that is, the filename attribute of the xml trace listener does not support relative paths. The preceding setting is trace. log to create a trace in the root directory of the application. log File, But If trace is entered here. xml, there is nothing, and no error is reported. Only the absolute path f: \ mydocument \ trace can be used. xml to create a trace in the f: \ mydocument directory. xml file. As shown in. I don't know if it's my problem, or is it not supported? I also hope you can give me some advice. Thank you !!!!!

 

 

As shown in, Category groups the trace listener set earlier, that is, one or more logging methods are used to record text files while recording databases. Group records.

The Trace group contains four log record forms.

 

Sample Code

 

Private void RecordSelectedCategories ()
{

Categories. Add ("Trace ");



}
Protected void button#click (object sender, EventArgs e)
{
RecordSelectedCategories ();
LogEntry log = new LogEntry ();
Log. EventId = 100;
Log. Priority = 3;
Log. Message = "information message ";
Log. Categories = Categories;
Dic. Add ("1", "<log> <id> 1 </id> <name> shiwenbin </name> </log> ");
Dic. Add ("2", 2 );
Log. ExtendedProperties = Dic;

Logger. Write (log );

}
Private IDictionary <string, object> Dic = new Dictionary <string, object> ();

[NonSerialized]
Private ICollection <string> categories = new List <string> (0 );
[IgnoreMember]
Public ICollection <string> Categories
{
Get {return categories ;}
Set {this. categories = value ;}
}

 

First, create a new LogEntry, set its properties, and set the log Group Category to Trace,

Log. Categories = Categories;

 

 

When logs are logged, the group containing the log type set in Categories is recorded.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.