programming4us
programming4us
WEBSITE

ASP.NET 4 in VB 2010 : Logging Exceptions (part 3) - Custom Logs, A Custom Logging Class

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

3. Custom Logs

You can also log errors to a custom log. For example, you could create a log with your company name and add records to it for all your ASP.NET applications. You might even want to create an individual log for a particularly large application and use the Source property of each entry to indicate the page (or web service method) that caused the problem.

Accessing a custom log is easy—you just need to use a different constructor for the EventLog class to specify the custom log name. You also need to register an event source for the log. This initial step needs to be performed only once—in fact, you'll receive an error if you try to create the same event source. Typically, you'll use the name of the application as the event source.

Here's an example that uses a custom log named ProseTech and registers the event source DivideByZeroApp:

' Register the event source if needed.
If Not EventLog.SourceExists("DivideByZeroApp") Then
' This registers the event source and creates the custom log,
' if needed.
EventLog.CreateEventSource("DivideByZeroApp", "ProseTech")
End If

' Open the log. If the log does not exist, it will be created automatically.
Dim Log As New EventLog("ProseTech")
log.Source = "DivideByZeroApp"
log.WriteEntry(err.Message, EventLogEntryType.Error)


If you specify the name of a log that doesn't exist when you use the CreateEventSource() method, the system will create a new, custom event log for you the first time you write an entry.

To see a newly created event log in the Event Viewer tool, you'll need to exit Event Viewer and restart it. Custom logs appear in a separate group named Applications and Services Logs, as shown in Figure 5.

Figure 5. A custom log

You can use this sort of code anywhere in your application. Usually, you'll use logging code when responding to an exception that might be a symptom of a deeper problem.

4. A Custom Logging Class

Rather than adding too much logging code in the Catch block, a better approach is to create a separate class that handles the event logging grunt work. You can then use that class from any web page, without duplicating any code.

To use this approach, begin by creating a new code file in the App_Code subfolder of your website. You can do this in Visual Studio by choosing Website => Add New Item. In the Add New Item dialog box, choose Class, pick a suitable file name, and then click Add.

Here's an example of a class named MyLogger that handles the event logging details:

Public Class MyLogger
Public Sub LogError(ByVal pageInError As String, ByVal err As Exception)
RegisterLog()

Dim log As New EventLog("ProseTech")
log.Source = pageInError
log.WriteEntry(err.Message, EventLogEntryType.Error)
End Sub

Private Sub RegisterLog()
' Register the event source if needed.
If Not EventLog.SourceExists("ProseTech") Then
EventLog.CreateEventSource("DivideByZeroApp", "ProseTech")
End If
End Sub
End Class


Once you have a class in the App_Code folder, it's easy to use it anywhere in your website. Here's how you might use the MyLogger class in a web page to log an exception:

Try
' Risky code goes here.

Catch err As Exception
' Log the error using the logging class.
Dim logger As New MyLogger()
logger.LogError(Request.Path, err)

' Now handle the error as appropriate for this page.
lblResult.Text = "Sorry. An error occurred."
Emd Try

If you write log entries frequently, you may not want to check whether the log exists every time you want to write an entry. Instead, you could create the event source once—when the application first starts up—using an application event handler in the global.asax file. 

Event logging uses disk space and takes processor time away from web applications. Don't store unimportant information, large quantities of data, or information that would be better off in another type of storage (such as a relational database). Generally, you should use an event log to log unexpected conditions or errors, not customer actions or performance-tracking information.


Other  
  •  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
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 2) - WINDOWS AZURE PLATFORM
  •  Sharepoint 2013 : Overview of Windows Azure for Sharepoint (part 1) - DEFINING THE CLOUD, DEFINING WINDOWS AZURE
  •  Windows Small Business Server 2011 : Customizing a SharePoint Site - Customizing Companyweb (part 5) - Adding RemoteApps Links - Add the Web Part to Companyweb
  •  Windows Small Business Server 2011 : Customizing a SharePoint Site - Customizing Companyweb (part 4) - Adding RemoteApps Links - Register the Web Part as Safe
  •  
    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