WEBSITE

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

12/18/2013 1:18:41 AM

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
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone