Google Search

Google
 

Sunday, August 31, 2008

Video: #24 | How Do I: Implement Tracing in an ASP.NET Web Site?

In this video Chris Pels will show you how to implement tracing in an ASP.NET web site to monitor or measure the performance and to diagnose errors. Learn how to add tracing to an entire web site as well as individual pages. See how to add custom trace messages in pages and business objects to aid in assessing site performance and in diagnosing errors. Learn the differences between tracing in an ASP.NET page and in using System.Diagnostics.Trace for tracing information in business objects. Writing trace information to log files and viewing trace information in the trace viewer are also discussed.

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Pie this dispels this video were to learn how to integrate tracing in an ASP net application lets get started by creating a new website and will call this website TV now to enable tracing in an ASP net website you can do one of two ways you can neither enable it for the entire website and why putting some information in the Web config file or you can enable it on a page by page basis let s start out by enabling it for our entire websites can be good web config found to the system Web element and what it had what s called a trace element and work in a two page output equals true what this does is it will add the trace output to the bottom of our webpage ago also said

Minute 1

In a bold attribute to be true to enable tracing and organist at the local only attribute to false which means that the tracing information will use be displayed to any webpage so that small addition to our web config lets start up the default page which has nothing on her moment in to see what we get to see that this provides us with a whole bunch of information on our page spread to top BS in information on the request including the ID of the session the time the encoding type of request as well as a status code which is a W3C value of this one indicating success from a CD trace information which goes through all of the offense that are fired on our page in order to render it as well as over here in the right received this is the delta time from the previous steps of this would be where you can indication of how

Minute 2

On a particular step is taking in rendering your page with dengue complete list of the controls that are on the page or these are the Web server controls that are used to render the page they get their size the view state size as well as the controls a size we also would get these session and application state if that was available information on the cookies coming in that request as well as the cookies for the response within CV complete header collection interval header values as well as they see us in detail on the response headers the form collection if values for their query string and then lastly we see all of the server of the variables that came inbound with this page such as United do

Minute 3

Simple tracing on your entire website lets take a look at some of the other attributes that we can sit here in the web config file that control the tracing on the website as a whole were the other attributes is called the request limit and this is an integer which indicates how many requests should be stored on the server for tracing by default it s 10 lets make it 44 website it can go up to a maximum of 10 000 and the next attribute them to look at is called most recent and what most recent does is to indicate whether or not when the request limit is reached or tracing stop that would be false or true tracing continue which would be most recent equals true and if it continues what happens is that the request limit is kind of like us back in at the most recent 40 requests would be stored on the server

Minute 4

Let s set this to true and what is their attributes everything said here is called the trace mode and as you can see it has two options whether to sort the trace output by category or sorted by time by default it sort by time and let s just stick with that for the moment there is one more attribute called right to diagnostics trace which will come back to in a few minutes after we looked at it to other aspects of a tracing sweat like do nowadays to create two webpages just demonstrate how you can control tracing on a single webpage so will create new page and the first one more call just tracing and in that one when I turned tracing on and will create a second page which will call no tracing and an alarm when I turn it off now because tracing is turned on in the web config file are tracing page will have tracing enabled

Minute 5

So what we want to do is hear in the no tracing page lets turn off tracing and as an attribute of the page element called trace and it s a Boolean acidity of their true or false indicating whether or not we want tracing to occur on his patience as we want tracing to be off will set this to false when note is that the trace attribute in the page element takes precedence over what s in the web config file so in this case are trace element on the new trace tracing page will turn off tracing sources take a quick look at these two pages to show you what the difference is first will go to the tracing page and we ll do that in the browser and this comes up looks a lot like what we just saw on the default page includes older tracing information have to go to no tracing and review that one in the browser does he read

Minute 6

Get tracing information on this page so they turned tracing on and off in individual pages should note that this trace attribute in the page element just turns tracing on and off the different parameters like to request limit etc can only be set in the web config file for the application as a whole to now let s look at how we might write some of our own custom trace messages to do that time didn t put a few controls on our tracing page and you put a fuse link button controls and the first armored in a call a right link to it that IDE will run at equals server and the text of this one won t be just right link and what is copied and will put a second control

Minute 7

Does well and this one we re going to call a warning link s defendant s change this millet scored to the code in the page behind and we want to add a day event for the page load it in here where you say if it is post back then were going to do a select case he quests the form for him ago and we want ahead of the event target for variable and this is how I was to pick up both the one link in the right link link buttons that we created on the

Minute 8

Symbols a case right link and will do case warmly and then we ll just do a case else throw new argument exception have more to say and warning trace and then will throw new invalid operation exception here and we ll just put a warning trace in the CIA the warning appear where we should have had rights let s just put right trays on that one and then let s just take this from here and in day else case will just put

Minute 9

General except as a message there were just going to messages sent in here so we can differentiate where we re landing in these different places now on will want to do it is to put this in a tried catch block so let s just take the code could appear in our try and we want to first catch an argument exception and news and we want to catch an invalid operation exception set to one since then in this first catch Rich is connected to our right trace from the right link to double or want to do is down in the argument exception which pertains to the right trace

Minute 10

We want to do with it ago of trace write a rather every overwrite the unit has three things into this method the first is can it be a category to scroll down to the overload that we can use witches number three cicada very will call exception and that our message and this is a right phrase and everyone actually passed the exception or error information now let s just take that one copy it down here but this time rather than the write method work and use the worn method we use the same category of exception and will just change this message to the worn trace and we want to pass in the second exception information poker let s start at the

Minute 11

Page and see what happens when you first put a breakpoint here online eight can watch that code being head start page first time to post back is false so we do not go through the select case statement page comes up we get our trace information now let s click on one of our links first let s click on the right link comes in post back his true steps in the event target for variable hits the right link throws the Excel argument exception down here it s caught now in a right out of trace message page comes up and I should see now in our trace information we have a new item with the category of exception and then we have the right trace message which was our message followed by the area from

Minute 12

In the air object smell go back up let s just do the warning link as well will see the same thing happens post back true over until Warren Link down to the exception and I were there right a warning here s our page and the difference you can see we saw the category of exception all of the appropriate warning information our message and air information but warnings are in red words the right method clause something that wasn t black so if you something important that you want to warn about than you would use to warn that it s now I want to do is to create a customer s page that shows us a simple list of customers in a great view and take a look at some of the additional tracing information that s available in

Minute 13

Create a new page for the site at pass a simple call as customers and what is close out the other pages and where one to put on this page a two things let s go to design mode and the first thing I would like to put on here is an XML data source and the second thing is agreed to an integrated view on to pick the XML data source as the data source for the great view of the two back into the source view and let s just quickly set up a little bit of data for this page will get into is in a manually enter a little data in the data element of the XML data source and I can put an XML structure writing here is a list call us customers and let s put a few customer elements

Minute 14

Notice that to add attributes to put first name equals show and a last name vehicles Smith and will just copy that and will change if you so put a will put John Doe and here and put Johnson and will take that one great to have then we ll put this to be city Hodges then the less fun will just make Jones and will make that Linda

Minute 15

Him down into her grief you list is said to have a few columns here and the first column opiate bound field and a data field is the vehicle for the first name and he header text watches could first name is copy and paste dad and take the other one the last name and will be all thought and we will just go one more eye to put a photo generate column sequels false and let s just go over into the code behind and in our

Minute 16

Page load event Lester said to trace messages will put a trace write and it will make this application page category and the message will be loading page and let s put aside one and also call this application page category and will say finish loading page now let s make our customers paid to the start page

Minute 17

In and what s important to note here is now on our trace information we had to write messages for the start and finish of the page loading and we get an indication of how long that process takes its prefixes for not doing much in their change that in just a moment and the other thing is to look down here in the controls tree and not only have a little bit more robust control you can start to see how useful this information can be so here is our grid view and you can start to see the rendering size of the review control as well as how much or how many bytes it is taking up in the view state for this can be very helpful when you have a more complex page to get an idea of what is taking up space in the response of your page that is

Minute 18

Pieces of your page take up the most space in a response object as well as you get a perspective on how big your view state is and what is contributing to the size that you stay now let s go back and add a little bit more are going to do is to make a copy of our customers page and actually what I m going to do is let me add a new page by a slightly different name customer business object and to save time I m going to do is to take the code from our customers page and where to put that over in our new customers business object page load event will put that over here as well

Minute 19

They just is the original customers patiently don t inadvertently make a change else going to the page and what I want to do this time is communicate the XML data source but we can take the customer information is copied to the clipboard and it deleted out of the XML data source control itself and we can add a new file to our half data folder and Excel file record customers and within a paste in the customer XML information to that file so you probably guess for them to do here is what a loaded from the file rather than have it directly in the data source and now let s head a new class and in this class would collect customer at its justices can edit via code folder okay let s head

Minute 20

A few methods inherent in the first one case called to get customers and it s been a taken a filename as a string and its gunner returned a strength and that also questioned said hey and constructor method and in the constructor method will route in a duet trace right and notice that we don t get the IntelliSense so what were missing here is that we re no longer working with the ASP net trace element or I should say object is what we need to do is head and imports that does imports system diagnostics and now we ll have the trace class within the system diagnostics namespace

Minute 21

And we won t get a message called customer class is created so it is an indication when this class is created in an anarchic customers method lets start out by his having a message that says start getting customer data and let s put that in a category called customer and engine lets put it one coal and get a customer data have also put that in the category of customer or we want to do is to create a variable as it is that XML

Minute 22

XML document with the new system XML XML document and were then we re going to load the file name that has been enamored in a returned the document s outer XML as the strength now let s go back over to our customer is in a business object page and inner page load were given to his new dimension a new customer object instance to take the XML data source one and work in a set the data property on that equal to

Minute 23

Customer get customers method and in the era we need to put a map path for me to set that equal to be at a data plans customers XML to now let s just ensure that the customer business object is our start page is sorted out and see what we get and we do get the great with customer names coming out of the customers XML file but were not getting our trace messages from our business object s let s go back and look at what we have to do to integrate those messages with our other messages and the reason that we re not getting the messages from our customers thought he d be business object is because trace object here in our page as you can see as part of system

Minute 24

trace context where is when we go over to our app code customer VP to trace class that we have here is part of system diagnostics tool we have to do is integrate this trace information from system diagnostics to display on our webpage tracing argued that we need to go back over to the Web config file and add a few things for us and we didn t do is just to ensure that all our error messages are being this blade servers and custom errors mode equals off and go back up to the top this lesson is really have to be at the top was imported here so you can see it clearly made to adding new section called system diagnostics and then in their

Minute 25

Want to add a trace element and inside the trace element you want to add the listeners element in an insight there we want at a listener and really give this a name webpage trace listener that type you can get from the documentation on adding listeners and that is pasted in here from the documentation that reduce me to close off that element now on ASP net pages you so that the trace traces are displayed automatically however one might have system diagnostics trace messages are only displayed it the trace information is compiled into the applications are rooted to do that we need yet another entry who need to add a section here at

Minute 26

Through the system diagnostics and I ve created a section ahead of time and can copy it from the toolbox because it has a bunch of settings in a nudist pacesetting so it said element by the name system co dawn compilers MRK compiler for Visual Basic is what this will do is use the compiler option of tracing equals true or Tracy post you and I will compile the trace information into our business object Cinelli if we view the page you ll see that in addition to the ASP net tracing information we now have the customer information in our trace log in addition to writing these system diagnostics trace information to the ASP trace page you can also do the reverse and that is

Minute 27

At the ASP trace information to the standard system diagnostics output in order to do that there s two things that we have to do first is we need to add another attribute to our trace element in a system Web element and that is the right to diagnostics trace will set to true and this will route the ASP information to the diagnostics trace then we need to add another listener here inside our system diagnostics element and again I ve copied this from the documentation is pasted into our web config and as she was living at that inside the listener s element that really reform it does see this good thoughts are per webpage and we see will see the trace information that was previously closed down the webpage

Minute 28

Governor afresh the website and now we have an HDI trace log file here every open that up don t find any information in the reason for that is that the listener caches information and it didn t flush it out to our trace log file so we need to do is to go back up your tour trace element and we need to say oil equals true to flush that information out now free open the log file after viewing the page again you ll see we have the same trace information in a log file that we had on the ASP net webpage so this creates a more permanent record of the trace information is it we can do is create a custom listener that wrote the information out to a database file while lessening alike show you he is called the trace viewer and up to this point we ve been doing trace information and the

Minute 29

Webpage images browse this tracing page and this is the type of page we been getting up to this point there s also something called the trace viewer and if you run TR ACD got a XD in the website where you doing the tracing company of a page that looks like this and what this does is it lists the most recent traces that you ve run or the pages that you viewed with tracing and then you can go over your on the right and few details which is the individual data for any of the pages so we ve seen how to add tracing to the entire Web application as well as turn it on and off on individual pages and then saw how to write both regular and warning messages using the trace object and then we looked it having a trace information

Minute 30

To a custom business object using system diagnostics which is similar to but a different approach to tracing in an ASP net webpage and then lastly we looked at how to integrate the trace information from system diagnostics to the ASP webpage trace information and also write both the ASP and system diagnostics face information to a log file hope you find this information on how to put trace information in your ASP net Web application useful encourage you to download the sample code that comes with this video it s in both see sharp and Visual Basic this is Chris Bell s thank you for watching this video on implementing tracing in your ASP net Web application

Click here to go to original video page

No comments: