[Azure services platform step by step-11th] windows azure Lanzhou noodle shop-Use of logs and queues

Source: Internet
Author: User
Tags baseuri log4net azure sdk

In article 9th, I compared the Windows azure environment to "Azure Lanzhou noodle shop" for your convenience ". In this article, we will continue to use this metaphor to talk about the use of queues and logs in Windows azure.

 

Queue StorageAs described in [azure services platform step by step-9th] windows azure storage overview, we will not repeat it here. As the name implies, queue is a queue that processes data in the FIFO order. In the azure Lanzhou noodle shop, queue serves as a small book to record the ordering situation. The front-end waiter records the dishes ordered by the customers in queue, chefs in the backend kitchens cook dishes (processing data) one by one in the order of queue ).

Windows azure logIs the log of the operating status of the entire azure platform. Microsoft officially compares windows azure to the "Operating System of the cloud platform". Since it is an operating system, it will certainly maintain the log! Windows azure provides a very simple way to record logs, but unfortunately it does not provide the same simple way to view logs-in other words, throughProgramIt is easy to record logs, and it is cumbersome to view logs. To avoid this problem, this article only shows how to view logs in the Development fabric. For more information about how to program log reading, seeSource code(See the attachment at the end of the article ).

 

Tutorial steps:

 

Step 1:

Create a web and worker cloud service in vs2008. This is also the seriesArticleWorker role is used for the first time.

Add a reference to storageclient as in article 1.

 

Step 2:

Run the sum in the azure SDK and start the queue service in the Development storage.

 

Step 3:

Configure serviceconfiguration. cscfg and servicedefinition. csdef. If you have any questions, please refer to [azure services platform step by step-8th] to develop and deploy the azure message board or the example in the attachment at the end of this ArticleCode.

 

 

Step 4:

Open default. aspx of the Web role project and drag the following controls:

 
TextboxTxtmessage
 
ButtonBtnsend
TextboxTxtlog

 
Step 5:
 
InButtonBtnsend_click is added to btnsend's click. The Code is as follows.
 Protected void Btnsend_click ( Object Sender, Eventargs E ){ // Read the configuration of the queue service. Note: What rolemanager. getconfigurationsetting obtains is  
// Configuration in service configuration (serviceconfiguration. cscfg file. Uri Baseuri = New Uri (Rolemanager . Getconfigurationsetting ( "Queueendpoint" )); String Accountname = Rolemanager . Getconfigurationsetting ( "Accountname" ); String Accountkey = Rolemanager . Getconfigurationsetting ( "Accountsharedkey" ); // Initialize the storageaccountinfo object. Storageaccountinfo Account = New Storageaccountinfo (Baseuri, Null , Accountname, accountkey ); // Create a queuestorage entity (I .e., the queue Storage Service) Queuestorage Service = Queuestorage . Create (account ); // Create a messagequeue (Queue) entity. // This queue is the "log Course Book" of our azure noodle shop. // We named this course book "message ". Messagequeue Queue = service. getqueue ( "Messages" ); // Determine whether "message" already exists in the current queue Storage Service" If (! Queue. doesqueueexist () {queue. createqueue ();} // Record the ordering information in the note text! Message MSG = New Message (Txtmessage. Text); queue. putmessage (MSG); txtlog. Text + = "\ N you are in" + Datetime . Now. tostring ( "HH: mm: SS seconds" ) + "Ordered a copy \"" + Txtmessage. Text. Trim () + '\"' ; Txtmessage. Text = String . Empty ;}
 
The most critical step is:Queue. putmessage (MSG );In this way, messages are easily stored in the queue.
 
 

Step 6:

Open workerrole. Cs in the workerrole project. First, we need to override start.

The START () function can be understood as the worker role entry function. Once the worker role is deployed, start () is triggered ().

We have discussed that the ox X of worker role is to keep a program running. How can this problem be achieved? You just need to write the START () method into an endless loop method.

 Public override void Start (){ // Initialize account information  Uri Baseuri = New  Uri ( Rolemanager . Getconfigurationsetting ( "Queueendpoint" )); String Accountname = Rolemanager . Getconfigurationsetting ("Accountname" ); String Accountkey = Rolemanager . Getconfigurationsetting ( "Accountsharedkey" ); Storageaccountinfo Account = New  Storageaccountinfo (Baseuri, Null , Accountname, accountkey );   Queuestorage Service = Queuestorage . Create (account );
// Get a reference to the queue of the log course named "message"MessagequeueQueue = service. getqueue ("Messages");// Construct an endless loop
 
While(True){
 
// The program in the endless loop is executed once every second
 
Thread. Sleep (10000 );If(Queue. doesqueueexist ()){
 
// Obtain the message to be processed in the log book
MessageMSG = queue. getmessage ();If(MSG! =Null){
 
// Record windows azure log
 
Rolemanager. Writetolog ("Critical",String. Format ("The chef has completed {1} At {0. ",Datetime. Now. tostring ("HH: mm: SS seconds"), MSG. contentasstring ()));
 
// Record the message that has been processed
 
Queue. deletemessage (MSG );}}}}

In this program, there are three points worth noting:

    1. Obtain the data to be processed:MessageMSG = queue. getmessage (); since it is a queue, data must be operated in the FIFO order. You can only use the getmessage method to obtain the top data in the queue, but not other data. It is like waiting in line to buy a train ticket. The ticket conductor can only sell to the person who ranks at the top of the team.
    2. Delete a message: queue. deletemessage (MSG); this is like queuing up to buy a train ticket. When the guy at the top buys a ticket, he should immediately disappear from the team.
    3. Rolemanager. Writetolog (string eventlogname, string message) method.
      This is the simple and easy-to-use log recording method mentioned above. Programmers who have used log4net can see at a glance that this is a "fake version" of log4net. It draws on the log recording mode of log4net, and castrated the configuration and custom format functions of log4net.
      Azure log supports five types of EventLog: critical, error, warning, information, and verbose. If you have never used log4net or do not understand the meaning of the EventLog type, you can try to understand it as follows: azure log classifies logs into five categories, and classification log makes it easier to view. In this example, "critical" is used for convenient viewing.

Step 7:

F5 run the program. At this time, our azure Lanzhou noodle shop was officially opened in the Development fabric!

Open Development fabric.

Select worker Role in the service we just deployed, right-click it, select "Clear logs" to clear the screen, and then select logging level-critical. This is to view the log categories. We chose to view the criticial category because we used critical when recording the ordering information.

 

Step 8:

Test it :)

Look, let's order food. The chefs cook the dishes in turn!

Let's take a look at a piece of data, such as "Yangzhou Fried Rice (Lanzhou)". We will write it into queue at a.m. After the chef finds it in the queue, he processes it.

Sort out the process:

A la carte-Write the food name to the queue-the chef checks the queue every second. If there is a dish to be prepared in the queue, then, cook in the order of first come and then-delete the prepared dish from the log-Write the cooking information to the azure log.

 

So far, we have mastered: getting the reference of the queue storage, creating a queue, inserting data into the queue (queuing), and retrieving data from the queue (queuing), record azure system logs, view azure system logs.

 

--- Split line with construcia ---

[Attachment ]:

The legendary omnipotent storageclient: storageclient.rar

WPF Application azure Log Viewer source code: azurelogviewer.zip

All source code for this experiment: queuestorage.rar

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.