Preface: the project cycle has been relatively tight recently, so this article is coming soon. I hope my friends who like this series will understand it. The previous article is also relatively simple. I will take the time to make up for it. I 'd like to apologize to you first ^_^
1. Logs and detection applicationsProgramBlock overview:
Enterprise Library logging and instrumentation Application BlockAllows developers to integrate standard logs andMonitoringFunction. Applications can use logs and monitoring blocks to record events in multiple locations:
1. Event Log (Event Log)
2. Email (Email)
3. Database (Database)
4. Text File (Textfile)
5. Message Queue (MSMQ)
6.WMI
7. User-Defined
Ii. Logs andMonitoringApplication blocks help application development in multiple aspects:
1. It helps maintain consistent logs and standard practices in the application and the entire enterprise.
2It uses a consistent architecture model, allowing developers to take less detours in the learning process.
3It provides implementation for solving common application logs and specification problems.
4. It is scalable and supports custom implementations of formatting programs and event receivers.
Iii. common situations:
Developers often write logs andMonitoringFunction Application. 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.
Logs andMonitoringApplication blocks collect the most common logs that an application needs to contain.MonitoringTask to simplify application development. Each task is processed in a consistent manner, from a specific log andMonitoringAbstract applications in the providerCode. The architecture model allows you to change the underlying event receiver and formatting programs by changing the configuration without changing the application code.
Iv. Log item Overview:
When logging, you create a day-to-day item to carry the recorded information. Each log item has the following attributes:
Message(Required)
Cagegory(Default value is provided)
Priority(The default value is-1)
Eventid(The default value is-1)
Severity(The default value isSeverity unspecified)
Title(The default value is "")
V..Several important concepts:
Sink: Location of the log record
Category: Determines where the logs you add to the program are written, which is implemented through configuration. For example, we have twoCategoryFirst, we can specifySinkFor event logs, the second one can be specifiedSinkIs a text file.
Formatter: The formatter determines the log record format. It is implemented through configuration. I will focus on it in the advanced article.
6. Add logs to the application:
Add logs to an application. The steps are similar to those of other application blocks. They are also divided into three steps: Create a configuration file first, create a log item object, and then pass itLogger. Write ()Method. Next let's take a look at the specific operation steps: (we also think that you have a created Project and a configuration fileApp. configOrWeb. config)
1After running the configuration tool, selectFile | open applicationOpen the application configuration file.
2InApplicationRight-click and selectNew | logging and instrumentation Application Block.
3. Default log and Monitoring Application BlockClient settingsDefinedIn-process Distribution StrategyAndLogginenabledIsTrue.
4 . default distributor Settings defines two Category (including General and trace ). We can see General "> Event Log destination sink is Event Log sink , its formatter is by default. Text formatter . right-click categorys and select New | Category to create a new Category .
] 5. SelectFile | save allSave all.
6. Finally, do not forget to copy the directory.
" $ (Projectdir) \ *. config " " $ (Targetdir) "
7. Add a reference to the Application Block in the project.
Microsoft. Practices. enterpriselibrary. Logging. dll
8. Reference in the program
1 Using Microsoft. Practices. enterpriselibrary. logging;
9. Add log:
Before that, for the standardization and rigor of the program, we should first define two enumeration types:
1 /**/ /// <Summary>
2///Define severity-level Enumeration
3/// </Summary>
4 Public Struct Priority
5 {
6 Public Const Int Lowest = 0 ;
7 Public Const Int Low = 1 ;
8 Public Const Int Normal = 2 ;
9 Public Const Int High = 3 ;
10 Public Const Int Highest = 4 ;
11 }
12
13 /**/ /// <Summary>
14///Enumeration of defined classes
15/// </Summary>
16 Public Struct Category
17 {
18 Public Const String General = " General " ;
19 Public Const String Trace = " Trace " ;
20 }
Add logs. The write method is used to add logs.
1 /**/ ///Create a log entry
2 Logentry log = New Logentry ();
3
4 Log. Message = This . Txt_logmessage.text;
5 Log. Category = Category. General;
6 Log. Priority = Priority. normal;
7
8 /**/ ///Write logs
9 Logger. Write (log );
10Open the Event Viewer. We can see that a log is written in it:
11Double-click the log information to view the details:
So far, we have finished a complete log record. This is the Getting Started article. In addition, you can take a look at it:
Http://terrylee.cnblogs.com/archive/2005/11/02/267063.html
In the advanced section, I will introduce how to create a name.-Dictionary of value pairs, filtering events, customizing the format of log messages, and configuring synchronization and Asynchronization.