{"id":258,"date":"2010-06-28T12:01:01","date_gmt":"2010-06-28T06:31:01","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=258"},"modified":"2010-06-28T12:01:01","modified_gmt":"2010-06-28T06:31:01","slug":"tracing-asp-net-website","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/tracing-asp-net-website\/","title":{"rendered":"Tracing ASP.NET Website"},"content":{"rendered":"<p>In ASP.NET there two types of Tracing<\/p>\n<ul>\n<li>Application Level<\/li>\n<li>Page Level<\/li>\n<\/ul>\n<p>Page level Tracing takes the\u00a0precedence over the Application  Level  tracing.<\/p>\n<p>Lets start with creating new website.<\/p>\n<p>In web.config add following entries to enable Application level   tracing below System.web element.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;system.web&gt;\n&lt;trace\npageOutput=&quot;true&quot;\nenabled=&quot;true&quot;\nrequestLimit=&quot;10&quot;\nlocalOnly=&quot;false&quot;\nmostRecent=&quot;true&quot;\ntraceMode=&quot;SortByTime&quot;\n\/&gt;\n&lt;\/system.web&gt;\n<\/pre>\n<p>where:<\/p>\n<p>pageOutput=&#8221;true&#8221;  &#8211; add the tracing information at the bottom of the  ASPX page.<\/p>\n<p>localOnly=&#8221;false&#8221;  &#8211; tracing will be available to any web page.<\/p>\n<p>requestLimit=&#8221;10&#8243;  &#8211; How many request should be saved on the server.<\/p>\n<p>mostRecent=&#8221;true&#8221;  &#8211; whether or not display the recent tracing  information if the  request reach at the limit.<\/p>\n<p>After adding above tag in web.config, the output of ASP.NET page will   look like:<\/p>\n<h1><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Tracing-FirstPage1.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Tracing-FirstPage1.jpg?resize=474%2C248&#038;ssl=1\" alt=\"ASP.NET Tracing Output\" width=\"474\" height=\"248\" \/><\/a><\/h1>\n<p>Here you can see much of informations displayed like SessionId,   Request Status, Controls\u00a0available on that page,\u00a0 All the events of the   page with start and end time etc and thus you can figure out the   performance of your web application.<\/p>\n<p>No  Trace for a particular Page<\/p>\n<p>Create a web page and in aspx file and at header write the tag  Trace=&#8221;false&#8221;<\/p>\n<p>Create  few controls like link button and view the page in browser.  You will  see that the page is displayed with no tracing information,  although its  turned ON in web.config but page level tracing has the  precedence\u00a0over  application level tracing.<\/p>\n<p>Writing\u00a0Custom  Trace Information<\/p>\n<p>Now on  the click event of Link buttons write the\u00a0custom\u00a0trace  information.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprotected void lblwriteMessage_Click(object sender, EventArgs e)\n{\n\tTrace.Write(&quot;Custome Message&quot;, &quot;Write Link Button clicked&quot;);\n}\nprotected void lblwarnMessage_Click(object sender, EventArgs e)\n{\n\tTrace.Warn(&quot;Custome Message&quot;, &quot;Warn Link Button clicked&quot;);\n}\n<\/pre>\n<p>when you will click on the WriteLink button, below output will be   seen.<\/p>\n<div>\n<dl>\n<dt><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/MessageCategory.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" style=\"width: 487px; height: 37px;\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/MessageCategory.jpg?ssl=1\" alt=\"ASP.NET Trace\" \/><\/a><\/dt>\n<dd>ASP.NET Trace<\/dd>\n<\/dl>\n<\/div>\n<p>and when you will click on warn button, the message will be displayed   in Red color.  so it is suggested to display the the important trace  message as a  warn.<\/p>\n<div>\n<dl>\n<dt><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/TraceWarn.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" style=\"width: 482px; height: 41px;\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/TraceWarn.jpg?ssl=1\" alt=\"ASP.NET Trace\" \/><\/a><\/dt>\n<dd>ASP.NET Trace<\/dd>\n<\/dl>\n<\/div>\n<p><strong>Page.Trace Vs  System.Diagnostics.Trace<\/strong><\/p>\n<p>Create  a new class file and write a static function which will write  the Trace  Message. You will notice that Trace object is not available  by default  like ASPX page instead you will need to import the package   &#8220;System.Diagnostics&#8221;.<\/p>\n<p>So,  there is lot difference in System.Diagnostic.Trace and  Page.Trace.<\/p>\n<p>Trace  from Diagnostics display the trace information in output  window of  Visual studio whereas the Trace from page displays the Trace  information  in ASPX Page.<\/p>\n<p>Integrate System.Diagnostics  with ASPX Page (Routing all Trace  information to web page)<\/p>\n<p>We will need to add the\u00a0listener\u00a0in web.config file to route all the   tracing information to the single web page.<\/p>\n<p>To  integrate the System.Diagnostics Trace with ASPX page, write  below line  of code in web.config.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;system.diagnostics&gt;\n  &lt;trace&gt;\n    &lt;listeners&gt;\n       &lt;add name=&quot;WebPageTraceListener&quot;\n            type=&quot;System.Web.WebPageTraceListener, System.Web, Version=2.0.3600.0, Culture=neutral,\nPublicKeyToken=b03f5f7f11d50a3a&quot;\/&gt;\n    &lt;\/listeners&gt;\n  &lt;\/trace&gt;\n&lt;\/system.diagnostics&gt;\n<\/pre>\n<p>The output will look like below image:<br \/>\n<a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/AllTrace.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" style=\"width: 463px; height: 95px;\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/AllTrace.jpg?ssl=1\" alt=\"ASP.NET Tracing Application Level\" \/><\/a><br \/>\n<strong>trace.axd file<\/strong><\/p>\n<p>This file contains all the information about  the tracing of the   application.<\/p>\n<p>To view the trace information  just write the file name and you will   get page which will look like  below snap.<\/p>\n<p>path structure :  http:\/\/&lt;servername&gt;\/webapp\/trace.axd<\/p>\n<dl>\n<dt><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/trace.axd_.jpg?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" style=\"width: 466px; height: 118px;\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/trace.axd_.jpg?ssl=1\" alt=\"Application Trace in ASP.NET\" \/><\/a><\/dt>\n<dd>Application Trace in ASP.NET<\/dd>\n<\/dl>\n<p><strong><span style=\"text-decoration: underline;\">Creating  Custom Trace\u00a0Listeners<\/span><\/strong><\/p>\n<p><strong>Database trace\u00a0listener<\/strong><\/p>\n<p>.NET  has provided us the flexibility of writing our own Trace   Listeners in  the form of the\u00a0TraceListener class. Every Trace  Listener  is inherited  from this class; therefore, in order to implement  your  own Trace  Listener, you must inherit your Trace Listener class from   this class.<\/p>\n<p>TraceListener  class has many virtual and abstract methods; at the   very least, each  inheritor of this class must implement the\u00a0Write   and\u00a0WriteLine  methods; other important methods are\u00a0Fail,\u00a0Close,   and\u00a0Flush.  Inheritors are not supposed to implement these methods but   it is a good  idea to implement these methods. Descriptions of these   methods are given  in the beginning of the article.<\/p>\n<table style=\"width: 465px; height: 331px;\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"115\" valign=\"removed\"><strong>Method Name<\/strong><\/td>\n<td width=\"475\" valign=\"removed\"><strong>Result<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"115\" valign=\"removed\">Fail<\/td>\n<td width=\"475\" valign=\"removed\">Outputs  the specified text with the   Call Stack.<\/td>\n<\/tr>\n<tr>\n<td width=\"115\" valign=\"removed\">Write<\/td>\n<td width=\"475\" valign=\"removed\">Outputs  the specified text.<\/td>\n<\/tr>\n<tr>\n<td width=\"115\" valign=\"removed\">WriteLine<\/td>\n<td width=\"475\" valign=\"removed\">Outputs the specified text and a    carriage return.<\/td>\n<\/tr>\n<tr>\n<td width=\"115\" valign=\"removed\">Flush<\/td>\n<td width=\"475\" valign=\"removed\">Flushes the output buffer to the   target  media.<\/td>\n<\/tr>\n<tr>\n<td width=\"115\" valign=\"removed\">Close<\/td>\n<td width=\"475\" valign=\"removed\">Closes the output stream in order to    not receive the tracing\/debugging output.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Write   and\u00a0WriteLine  methods are overloaded; following is a list of  all the  overloaded  version of the Write method:<\/p>\n<ul>\n<li>public  override void Write(string message)<\/li>\n<li>public override void  Write(object o)<\/li>\n<li>public override void Write(string message,  string category)<\/li>\n<li>public override void Write(object o, string  category)<\/li>\n<\/ul>\n<p>For this article, I have created a Trace  Listener,\u00a0<strong>DatabaseTraceListener<\/strong>,   which actually stores trace and  debug messages into a database.<\/p>\n<p>add below code in Web.config  which stores the connection string   information.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;appSettings&gt;&lt;\/pre&gt;\n&lt;pre&gt;&lt;add key=&quot;ConnectionString&quot; value=&quot;Data Source=.SQLExpress;Integrated Security=True;User Instance=True;\nAttachDBFilename=|DataDirectory|TraceDB.mdf&quot; \/&gt;&lt;\/pre&gt;\n&lt;pre&gt;&lt;add key=&quot;MaximumRequests&quot; value=&quot;2&quot; \/&gt;&lt;\/pre&gt;\n&lt;pre&gt;&lt;\/appSettings&gt;\n<\/pre>\n<p>The structure of Table is:<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/TraceDBStructure.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/TraceDBStructure.jpg?resize=334%2C161&#038;ssl=1\" alt=\"\" width=\"334\" height=\"161\" \/><\/a><\/p>\n<li>TraceDateTime  column stores the date and  time of the trace   message<\/li>\n<li>TraceCategory  column stores the  actual category of  the trace  message<\/li>\n<li>TraceDescription  column  stores the trace  message<\/li>\n<li>StackTrace column contains the  stack  trace<\/li>\n<li>DetailedErrorDescription  column contains the  detailed  error  message  which will be passed in  second parameter of the\u00a0<strong>Fail<\/strong> method.<\/li>\n<p>create      below stored procedure in SQLExpress<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" style=\"width: 475px; height: 164px;\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/SQL.png?ssl=1\" alt=\"Stored Procedure ASP.NET Tracing\" align=\"none\" \/><br \/>\nNow create a class named    &#8220;DatabaseTraceListener&#8221; which inherit the   abstract class    &#8220;TraceListener&#8221;. You can check the code snippets in   Source code of this    article.<\/p>\n<div>if you want to implement your own Trace Listener,    simply derive   your listener class from <strong>TraceListener <\/strong>class,    implement at  least  the <strong>Write <\/strong>and <strong>WriteLine <\/strong>methods and    you are  done.<\/div>\n<div>In our Example, <strong>Flush <\/strong>and <strong>Close <\/strong>methods    simply  save  all the cached messages into the database.<\/div>\n<p><strong>Saving    Trace information in File<\/strong><\/p>\n<p>We can also save the trace     information in log files.<\/p>\n<p>To save the trace information in  File,    simply add the following  entry  in web.config<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;add name=&quot;myListener&quot;\ntype=&quot;System.Diagnostics.TextWriterTraceListener&quot;\ninitializeData=&quot;TextWriterOutput.log&quot; \/&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Demonstrates that how to Trace ASP.NET web applications<\/p>\n","protected":false},"author":1,"featured_media":265,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[21],"tags":[43,73,202],"class_list":["post-258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net","tag-asp-net","tag-debugging","tag-tracing"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":125,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/cannot-access-a-closed-file-fileupload-in-asp-net\/","url_meta":{"origin":258,"position":0},"title":"cannot access a closed file &#8211; FileUpload in ASP.NET","author":"Jitendra","date":"May 13, 2010","format":false,"excerpt":"This was very\u00a0interesting\u00a0error, i got during development of file upload control in ASP.NET. On my local system, every thing was just fine. but when i deployed my application on development server, my control was able to upload only small size files. whenever i tried to upload large size file i\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2299,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/tutorial-read-and-export-excel-file-in-asp-net-using-c\/","url_meta":{"origin":258,"position":1},"title":"Tutorial &#8211; Read and export excel file in ASP.Net  using C#","author":"Jitendra","date":"July 29, 2011","format":false,"excerpt":"Tutorial - Read and export excel file in ASP.Net using C#","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Read And Export Excel in ASP.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/07\/Read-And-Export-Excel-in-ASP.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1138,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/how-to-read-value-from-configuration-file-app-config-or-web-config\/","url_meta":{"origin":258,"position":2},"title":"How to read value from Configuration file &#8211; ( app.config or web.config )","author":"Jitendra","date":"September 19, 2010","format":false,"excerpt":"How to read value from Configuration file - app.config or web.config in C# application or web application","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"add reference of System.Configuration in .NET","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/System.Configuration-300x250.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":132,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/upload-files-in-asp-net-at-production-server\/","url_meta":{"origin":258,"position":3},"title":"Upload Files in ASP.NET at Production server","author":"Jitendra","date":"May 13, 2010","format":false,"excerpt":"In this article, i am going to demonstrate that how to upload the file in ASP.NET production server. Most of the case, a developer created a code to upload the file and test it on his local machine. program runs smoothly, but as he\u00a0forward\u00a0the same code on production. He\u00a0stuck\u00a0in the\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/WebShare-Folder.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1029,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/logging-made-easy-in-net-log-4-net\/","url_meta":{"origin":258,"position":4},"title":"Logging made easy in .NET &#8211; log 4 net","author":"Jitendra","date":"September 30, 2010","format":false,"excerpt":"How to use the log 4 net in .Net application. Example, demonstration and sourcecode","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1527,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/ajax-based-multiselect-jquery-autocomplete-control-in-asp-net\/","url_meta":{"origin":258,"position":5},"title":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","author":"Jitendra","date":"February 19, 2011","format":false,"excerpt":"Tutorial on creating Ajax Based Multiselect JQuery Autocomplete User Control in ASP.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Ajax Based Multiselect JQuery Autocomplete Control in ASP.Net","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/02\/Ajax-Based-Multiselect-JQuery-Autocomplete-Control-in-ASP.Net_.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/258","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=258"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/258\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}