Entlib 3.1 Study Notes (4): logging Application Block

Source: Internet
Author: User
Entlib 3.1 Study Notes (4): logging Application Block

 

[Zh] http://www.microsoft.com/china/MSDN/library/enterprisedevelopment/softwaredev/dnpag2logging.mspx? MFR = true
[En] http://msdn2.microsoft.com/en-us/library/ms998162.aspx

Developers often write applications that require logs and standardized functions. Typically, these applications must properly format events and record events, either locally or over the network. In some cases, you may need to organize events from multiple sources on a computer.

Log application blocks simplify application development by collecting the most common logs and standard tasks that an application needs to contain. Each task is processed in a consistent manner, and application code is abstracted from a specific log and specification provider. The architecture model allows you to change the underlying event receiver and formatting programs by changing the configuration without changing the application code.

1. Applications can use Log blocks to record events in multiple locations:
(1) Event Log
(2) email
(3) Database
(4) Message Queue
(5) files
(6) WMI

2. Use
(0) Configure app. config/Web. config with entlib Configuration tool, new-> logging Application Block:
A. Create or customize formatter (by default, there is only one text formatter, And we can customize its template );
B. create or customize trace listeners (by default, only one fomatted EventLog tracelistener can be used to record logs in system logs), and specify a Formatter for it (the formatter defined in step ); different trace listener records different event locations (email, Eventlog, file, DB, MSMQ, and WMI.
C. create a category Source (there is only one general category by default, which uses EventLog tracelistener to record the log in the System Log), and specify the trace listener for it (the trace listener defined in step B ).

(1) Logging:

Logentry log = new logentry ();
Log. eventid = 300;
Log. Message = "sample message ";
Logentry. categories. Clear ();
Log. categories. Add ("categoryname"); // The parameter is the category. Name Defined in Step C above. You can add multiple category.
Log. Severity = traceeventtype. Information;
Log. Priority = 5;
Logger. Write (log );

(2) The log contains a name-Value Pair dictionary.

Dictionary <string, Object> dictionary = new dictionary <string, Object> ();
Managedsecuritycontextinformationprovider informationhelper = new managedsecuritycontextinformationprovider ();
Informationhelper. populatedictionary (dictionary );
Dictionary. Add ("loginforname", strloginfor );
Logger. Write ("log entry with extra information", dictionary); // use the default genera category to record events to system logs.

(3) tracking activities and recording context information
Lab supports tracking an activity by activity ID. The activity ID can be specified in the Code or automatically generated by the program block. Lab automatically records the start time and end time of the activity:

Using (New tracer ("category1 "))
{
Using (New tracer ("category2 "))
{
Logentry = new logentry ();
// Assign a value to the logentry attribute
Logger. Write (logentry); // at this time, logentry will write five logs:
// 1. Start trace: Activity 'activityid'... at... ticks
// Category: category1
// 2. Start trace: Activity 'activityid (same as above) '... ticks
// Category: category2, category1
// 3. Simulated General... activity = 'activityid (same as above )'
// Category: general, category2, category1
// 4. End trace: Activity 'activityid (same as above) 'At ticks
// Category: category2, category1
// 5. End trace: Activity 'activityid (same as above) 'At ticks
// Category: category1
}
}

(4) create a filter event
Filters-> New-> you can create category filter/custom filter/logenabled filter/Priority filter.
CATEGORY filter: Filter by category (deny all records T: somecategory );
Priority filter: only records events with priority between minimumpriority and maxmumpriority;
Logenabled filter: records (Enabled = true) or does not record (Enabled = false) All logentries.

If (logger. getfilter <logenabledfilter> (). enabled)
{
// Logging is enabled.
}
Else
{
// Logging is not enabled.
}

If (logger. getfilter <categoryfilter> (). shouldlog (categories) // icollection <string> categories;
{
// Event will be logged.
}
Else
{
// Event will not be logged.
}

If (logger. getfilter <priorityfilter> (). shouldlog (priority) // int priority;
{
// Event will be logged.
}
Else
{
// Event will not be logged.
}

If (logger. shouldlog (logentry) // summarize the above filter results to determine whether the event should be filtered out.
{
// Perform possibly expensive operations gather information for the event to be logged.
}
Else
{
// Event will not be logged.
}

3. Design of logging Application Block:

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.