asp tutorials, asp.net tutorials, sample code, and Microsoft news from 15Seconds
Data Access  |   Troubleshooting  |   Security  |   Performance  |   ADSI  |   Upload  |   Email  |   Control Building  |   Component Building  |   Forms  |   XML  |   Web Services  |   ASP.NET  |   .NET Features  |   .NET 2.0  |   App Development  |   App Architecture  |   IIS  |   Wireless
 
Pioneering Active Server
 Power Search





Active News
15 Seconds Weekly Newsletter
• Complete Coverage
• Site Updates
• Upcoming Features

More Free Newsletters
Reference
News
Articles
Archive
Writers
Code Samples
Components
Tools
FAQ
Feedback
Books
Links
DL Archives
Community
Messageboard
List Servers
Mailing List
WebHosts
Consultants
Tech Jobs
15 Seconds
Home
Site Map
Press
Legal
Privacy Policy
internet.commerce














internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

HardwareCentral
Compare products, prices, and stores at Hardware Central!

Writing Messages to the WinNT Event Log with a COM Component
By Toby Patke
Rating: 3.7 out of 5
Rate this article


  • email this article to a colleague
  • suggest an article

    Introduction

    Handling errors gracefully is something every program needs to do well. Ideally, if a user causes an error to occur, the user should be given the information necessary to correct the problem. However, if the problem is program related, the programmer needs to be given the information necessary to correct the problem. This distinction prevents the user from getting cryptic error messages they are powerless to correct.

    Creating the Project

    The first step in creating a COM component is to open Visual Basic and select New Project / ActiveX DLL.

    Project Code

    Writing an event to the event log is very straightforward. In fact the functionality is built into the VB application object. The syntax is:

    
    app.LogEvent(LogBuffer as String, [EventType])
    
    
    Where LogBuffer is the message you want to pass to the event log, and EventType is the type of event you want to occur. The parameters for EventType are as follows:

    1 = The “Error” event.
    2 = The “Warning” event
    4 = The “Information” event

    While this code is very simple, it is not supported in ASP. Therefore, we must build a wrapper around the code. To do this, copy the following code into class1.cls:

    
    'This subprocedure is used to write information to the WinNT event log
    Public Sub WinNTLog(ByVal SysErrorNum As String, ByVal SysErrDesc As String, _
                ByVal SysErrSource As String, _
                Optional ByVal UserErrDesc As String, Optional ByVal ErrType As Integer)
                
    'The values you choose to pass the subprocedure will be based
    '   on how you plan to use the event log.  In this case, we will
    '   be using the event log to document errors.  Therefore, I
    '   have chosen to pass error messages to the subprocedure.
    
    Dim ErrorMsg As String   'Used to store a formatted error message
    
    'Format the error message.
    SysErrorNum = "System Error Number:  " & SysErrorNum
    SysErrDesc = "System Error Description:  " & SysErrDesc
    SysErrSource = "System Error Source:  " & SysErrSource
    
    ErrorMsg = vbCrLf & vbCrLf & SysErrorNum & vbCrLf & SysErrDesc & vbCrLf & _
                    SysErrSource
    
    If Not IsEmpty(UserErrDesc) Then
    UserErrDesc = "Programmer Error Description - " & UserErrDesc
    End If
    
    'If a valid value was not passed in, set a default.
    If Not (ErrType = 1 Or ErrType = 2 Or ErrType = 4) Then
        ErrType = 1
    End If
    
    'Write the formatted error to the NT log
    With App
        If IsEmpty(UserErrDesc) Then
            .LogEvent ErrorMsg, ErrType
        Else
            .LogEvent ErrorMsg & vbCrLf & vbCrLf & _
                    UserErrDesc, ErrType
        End If
    End With
    
    End Sub
    
    

    Testing the Component

    Next we’ll create an ASP page to test our component. To do this, copy the following code into an ASP page.

    
    <%@ Language=VBScript %>
    
    <%
    on error resume next
    
    dim x
    
    x = 1/0		'This line will generate an error
    
    if err.number <> 0 then
    	
    	'Show a "friendly" error message to the user.
    	Response.Write "<h2>You have tried to divide by zero.  Please try “ & _ 
      “dividing by a valid number.</h2>"
    	
    	'Generate an error message for the programmer.
    	ErrMsg = "Some user tried to divide by zero."
    		set myObj = server.CreateObject("ErrorHandler.Class1")
    	myobj.WinNTLog err.number, err.description, err.source, errmsg, 1
    	set myobj = nothing
    	
    	Response.End
    	
    end if
    %>
    
    
    We can test our component directly from our ASP page by choosing to run the VB program. When we do so, we will be given the following prompt:

    Figure 3

    We can use this screen to tell VB how we will call the object. Because we are calling the object from an ASP page, we can accept the defaults.

    Finally, run the ASP page. This should give us the following error message:

    “You have tried to divide by zero. Please try dividing by a valid number.”

    If you check the event log however, you will notice that no error message has been written. This is because the application object can only write to the event log from a fully compiled program. The advantage of running the program from the server’s memory before compiling the program is that this allows us to spot any simple syntax or logic errors in our code. So if the program has run with no errors, we can now compile it and run it again from the compiled DLL.

    After running the program, we will see the same message in the browser, but if we look in the event log, we should see an error message similar to the following.

    Figure 4

    About the Author

    Toby Patke is a programmer analyst for the Web development team of a Fortune 50 firm. Toby enjoys creating database or LDAP-enabled multitiered Internet, extranet, and Intranet applications using ASP, ADSI, VB, MTS, DCOM, and creative code. When not programming (a very rare occurrence) he enjoys Harry Belafonte, kayaking, and playing craps for big money. His motto is “Let it ride!” He can be reached at toby_patke@hotmail.com

  • Rate This Article
    Not HelpfulMost Helpful
    1 2 3 4 5
    Other Articles
    Apr 27, 2004 - Develop and Customize Web Parts with Custom Tool Parts
    Tool Parts provide an interface for Web Part properties well beyond the capabilities of the default property pane. In this article Gayan Peiris shows how to customize Web Parts with custom Tool Parts.
    [Read This Article]  [Top]
    Apr 7, 2004 - Reusable Components in ASP.NET 2.0, Object Binding and Precompilation
    This article demonstrates how to create a reusable component in ASP.NET 2.0 and then consume it from an ASP.NET page. Also learn how the ObjectDataSource control can be used to directly bind the output of an object to the controls in an ASP.NET page and how precompilation can be used to increase the performance of the Web application and catch compilation errors.
    [Read This Article]  [Top]
    Mar 31, 2004 - Build a Managed BHO and Plug into the Browser
    Browser Helper Objects (BHOs) are COM components that communicate with Internet Explorer to enrich the browsing experience. Michele Leroux Bustamante returns to the world of COM to show you how to build a managed BHO with the help of the .NET Framework's COM interoperability features.
    [Read This Article]  [Top]
    Feb 18, 2004 - Customizing SharePoint Web Parts with Custom Properties
    In addition to creating custom Web Parts for SharePoint Portal Server, developers can actually create their own custom properties to further enhance Web Part appearance and behavior. Gayan Peiris explains the process and provides all the necessary code.
    [Read This Article]  [Top]
    Sep 26, 2003 - Accessing Shared Resources Using ASP.NET
    Accessing shared resources is a challenge for many ASP.NET developers. Tony Arslan explains how a simple serviced component can solve this infamous problem.
    [Read This Article]  [Top]
    Oct 2, 2002 - Function Pointers and COM
    Using callbacks and function pointers in VB can be risky and complicated. Ben Garcia explains his work-around for the function pointer issue he encountered while creating the VB version of his SNMP component.
    [Read This Article]  [Top]
    Sep 4, 2002 - Creating an SNMP Component - Part 2
    In part two of this intriguing article series, Ben Garcia shows how to build an updated and improved SNMP component in VC++ AND VB, and he briefly explains why limitations in VB make VC++ a better language for developing this type of application.
    [Read This Article]  [Top]
    Jul 23, 2002 - Creating an SNMP Component
    Ben Garcia sheds some light on the Simple Network Management Protocol (SNMP). First he provides a history of SNMP, then he dives right into its architecture. Finally, he shows how to build a COM component that communicates with SNMP-enabled devices.
    [Read This Article]  [Top]
    Jun 26, 2002 - Accessing Caller ID from the Web - Part 1
    Paul Apostolos begins his series on using Web services and the MSComm32.OCX component to access caller id information from a Web page. In part 1, learn how to write the Visual Basic program that runs on the server and updates a database with incoming callers.
    [Read This Article]  [Top]
    Nov 20, 2001 - Creating a Server Component with VB - Redesigned - Part 2
    Doug Dean explains different methods of retrieving and manipulating data from a database in a VB DLL so that it is ready to be rendered in a browser.
    [Read This Article]  [Top]
    Mailing List
    Want to receive email when the next article is published? Just Click Here to sign up.

    Support the Active Server Industry

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info

    Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers