"Azure Services Platform Step by step-11th" Windows Azure Lanzhou la noodle Shop-use of logs and queues

Source: Internet
Author: User
Keywords See us manager
Tags button clear click code configuration continue create data
In the 9th article, for the sake of understanding, I likened Windows Azure's environment to "Azure Lanzhou La Noodle Restaurant". In this article we continue to use this analogy to talk about queues (queue Storage) and log usage in Windows Azure.

The picture is not clear? Please click here to view the original image (larger).

Queue storage was introduced in the "Azure Services Platform Step by step-9th" Windows Azure Storage Overview, which is not repeated here. Queues, as the name suggests, are a queue that processes data in a first-in, first-out order. In the "Azure Lanzhou la noodle Shop" above, the queue acts as a small book for ordering, where the receptionist records the customer's order in the queue, and the kitchen chef cooks the dishes in the order of the queue, one by one.

Windows Azure Log is a log of the entire Azure platform running. Microsoft Official likened Windows Azure to "the operating system of the cloud Platform", and since it is the operating system, it will maintain the log! Windows Azure provides a very simple way to log logs, but unfortunately does not provide the same simple way to view the log--in other words, it's easy to log logs through the program, and it's cumbersome to view the logs. In order not to off, this article only shows how to view the log in development fabric. Interested readers can refer to the source code of the WPF application Azurelogviewer for how to programmatically read the log (see annex at end of article).

Experiment steps:

First step:

Create a new web and Worker Cloud Service in VS2008. This is also the first time in this series of articles to use worker role.

As with the 9th article, add a reference to the storageclient.

The picture is not clear? Please click here to view the original image (larger).

Step Two:

Run the and in the Azure SDK and start the queue service in development storage.

Step Three:

Configure Serviceconfiguration.cscfg and Servicedefinition.csdef. If you have questions, refer to the "Azure Services Platform Step by step-8th" to develop and deploy the Azure message board or the sample code in the end of this article.

The picture is not clear? Please click here to view the original image (larger).

Fourth Step:

Open the Default.aspx of the Web role project, and drag into the following controls:

TextBox Txtmessage

Button Btnsend

TextBox Txtlog

The picture is not clear? Please click here to view the original image (larger).

Step Fifth:

Add Btnsend_click to button Btnsend Click. The code is as follows. The configuration of the queue service is read by the

protected void Btnsend_click (object sender, EventArgs e)
{
//. Note: Rolemanager.getconfigurationsetting gets the configuration in the

//service revisit (that is, serviceconfiguration.cscfg file).
Uri BaseUri = new Uri (rolemanager.getconfigurationsetting ("Queueendpoint"));
String accountname = rolemanager.getconfigurationsetting ("AccountName");
String accountkey = rolemanager.getconfigurationsetting ("Accountsharedkey");
//Initialize the Storageaccountinfo entity.
Storageaccountinfo account = new Storageaccountinfo (
BaseUri,
Null,
AccountName,
Accountkey);
Create an entity for the queuestorage (that is, the queue storage service)
Queuestorage service = queuestorage.create (account);
Create a MessageQueue (that is, a queue) entity.
//This queue is the "vegetable book" of our Azure Ramen restaurant
///We name this dish "message".
MessageQueue queue = service. Getqueue ("Messages");
//To determine if the "message"
if (!queue) already exists in the current queue storage service. Doesqueueexist ())
{
Queue. Createqueue ();
}
//Make a note of the order information in the dish book!
msg = new Message (Txtmessage.text);
Queue. Putmessage (msg);
Txtlog.text + = "n You are in" +datetime.now.tostring ("hh dot mm minute ss seconds") + "ordered a copy" "+ TxtMessage.Text.Trim () + '" ";
Txtmessage.text = string. Empty;
}

The key is one step: queue. Putmessage (msg); It was easy to put the message in the queue.

Sixth step:

Open the WorkerRole.cs in the Workerrole project. We first have to override the start ().

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

As we've discussed, the place where the Worker role is the cow x is to make a program run all the time. How to do it? Just write the start () method as a dead loop method.

public override void Start ()
{
//initialization 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);

//obtain a reference to this queue named "message"
MessageQueue queue = service. Getqueue ("Messages");
//Constructed dead loop

while (true)
{

//Dead Loop program executes once per second

Thread.Sleep (10000);
if (queue. Doesqueueexist ())
{

//Get the message that is currently being processed in the Vegetable menu

msg = queue. GetMessage ();
if (msg!= null)
{

//Log Windows Azure Log

Rolemanager.writetolog ("unacknowledged",
St Ring. Format ("The chef has already done the {1}" in {0}.) ", DateTime.Now.ToString (" hh dot mm minute ss seconds "), Msg. ContenTasstring ()); The

//record deletes the message

queue that you have just processed. Deletemessage (msg);
}
}
}
}

In this procedure, there are three notable:

Gets the data that should be processed now: message msg = queue.  GetMessage (); Since queues, you must manipulate the data in a first-in, first-out order. The user can only get the top-most data in the queue through the GetMessage method and cannot fetch other data. It's like standing in line to buy a train ticket, and the conductor can only sell to the person who is in the front line.

Delete message: queue.   Deletemessage (msg); It's like waiting in line for a train ticket, and when the man in the front of the line buys the ticket, he should disappear from the team immediately.

Rolemanager.writetolog (string eventlogname,string message) method.

This is the very easy way to log logs, as mentioned above. A log4net programmer can see that this is a "cottage version" of the log4net. It borrows the model of Log4net record log, and castration the configuration and custom Format function of log4net.

Azure Log supports a total of 5 types of eventlog type Critical,error,warning,information,verbose. If you have not used log4net or do not understand the meaning of the EventLog type, you can try to understand that Azure log divides the log into 5 categories; This example uses "unacknowledged" and is just for easy viewing.

Seventh Step:

F5 Run the program. At this time our Azure Lanzhou Ramen restaurant officially opened in development fabric!

Open the development fabric and see.

Select the worker role in the service we just deployed, click the right button, select Clear Logs, and then select Logging level-critical. This is the category view of the logs, and we chose to view the criticial category because we used unacknowledged to record the order information.

Eighth Step:

Test it:

Look, we order, the chef cooks in turn!

Take a look at a data bar, such as "(Lanzhou edition) Yangzhou Fried Rice" This item, we at 01 points 29 minutes 58 seconds to write it to queue. The chef found it in the queue and handled it at 00 points, 30 minutes, 04 seconds.

Comb through the process:

Order-Write the name of the dish to queue-chefs check the queue every second, and if there are any dishes to be made in the queue, cook in the order of the chronological-remove freshly cooked dishes from the dish-and write the cooking information into Azure log

So far, we've got a reference to queue storage, a queue, inserting data into a queue (team), pulling data out of a queue, logging azure system logs, and viewing azure system logs.

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.