programming4us
programming4us
WEBSITE

ASP.NET 4 in VB 2010 : Page Tracing (part 2) - Writing Trace Information

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Writing Trace Information

The default trace log provides a set of important information that can allow you to monitor some important aspects of your application, such as the current state contents and the time taken to execute portions of code. In addition, you'll often want to generate your own tracing messages. For example, you might want to output the value of a variable at various points in execution so you can compare it with an expected value. Similarly, you might want to output messages when the code reaches certain points in execution so you can verify that various procedures are being used (and are used in the order you expect). Once again, these are tasks you can also achieve using Visual Studio debugging, but tracing is an invaluable technique when you're working with a web application that's been deployed to a web server.

To write a custom trace message, you use the Write() method or the Warn() method of the built-in Trace object. These methods are equivalent. The only difference is that Warn() displays the message in red lettering, which makes it easier to distinguish from other messages in the list. Here's a code snippet that writes a trace message when the user clicks a button:

Protected Sub cmdWrite_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles cmdWrite.Click

Trace.Write("About to place an item in session state.")
Session("Test") = "Contents"
Trace.Write("Placed item in session state.")
End Sub

These messages appear in the trace information section of the page, along with the default messages that ASP.NET generates automatically (see Figure 11).

Figure 11. Custom trace messages

You can also use an overloaded method of Write() or Warn() that allows you to specify the category. A common use of this field is to indicate the current method, as shown in Figure 12.

Figure 12. A categorized trace message

Protected Sub cmdWriteCategory_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cmdWriteCategory.Click

Trace.Write("cmdWriteCategory_Click", _
"About to place an item in session state.")
Session("Test") = "Contents"
Trace.Write("cmdWriteCategory_Click", _
"Placed item in session state.")
End Sub

Alternatively, you can supply category and message information with an exception object that will automatically be described in the trace log, as shown in Figure 13.

Figure 13. An exception trace message

Protected Sub cmdError_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles cmdError.Click

Try
DivideNumbers(5, 0)
Catch err As Exception
Trace.Warn("cmdError_Click", "Caught Error", err)
End Try
End Sub

Private Function DivideNumbers(ByVal number As Decimal, _
ByVal divisor As Decimal) As Decimal
Return number/divisor
End Sub

By default, trace messages are listed in the order they were written by your code. Alternatively, you can specify that messages should be sorted by category using the TraceMode attribute in the Page directive:

<%@ Page Trace="True" TraceMode="SortByCategory" %>

or the TraceMode property of the Trace object in your code:

Trace.TraceMode = TraceMode.SortByCategory
Other  
  •  ASP.NET 4 in VB 2010 : Logging Exceptions (part 4) - Retrieving Log Information
  •  ASP.NET 4 in VB 2010 : Logging Exceptions (part 3) - Custom Logs, A Custom Logging Class
  •  ASP.NET 4 in VB 2010 : Logging Exceptions (part 2) - Writing to the Event Log
  •  ASP.NET 4 in VB 2010 : Logging Exceptions (part 1) - Viewing the Windows Event Logs
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - Getting Started with BCS (part 2) - Creating an External List in SharePoint, Adding Custom Actions to an External Data Li
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - Getting Started with BCS (part 1) - Creating an External Content Type
  •  Sharepoint 2010 : Composite Applications with Business Connectivity Services - BCS Components
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 5) - DEVELOPING WINDOWS AZURE APPLICATIONS - Creating a Model
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 4) - DEVELOPING WINDOWS AZURE APPLICATIONS - Creating Your First Windows Azure Application
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 3) - DEVELOPING WINDOWS AZURE APPLICATIONS - Setting Up Your Development Environment
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us