in

ASP.NET Weblogs

Tolga Koseoglu

Event logging within MOSS and regular asp.net web application

Event logging in asp.net is pretty straight forward. In MOSS it might be a little different.

1. Regular asp.net web application

First, reference the System.Diagnostics namespace.

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Diagnostics;

 

public partial class General_EventLogging : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        this.WriteToEventLog("my error");      

    }

    private void WriteToEventLog(String _message)

    {      

        String LogFile = "Application";

        String SourceName = "MyCustomApp";

 

        if (!(EventLog.SourceExists(LogFile)))

        {

            EventLog.CreateEventSource(SourceName, LogFile);

        }

 

        EventLog MyLog = new EventLog();

        MyLog.Source = SourceName;

        MyLog.WriteEntry(_message, EventLogEntryType.Error);

 

    }

}

2. Custom MOSS 2007 SP1 web part

In order to get the same piece of code (above) working I advise to use impersonation. Usually, a reader account in MOSS does not have the rights in order to write to a windows log file.

This routine perform the "writing" of the event to the event log. Just before the event is being written to the log the code will impersonate the current user using the MOSS admin user account.

Public Sub WriteToEventLog(ByVal _LogFile As String, ByVal _Source As String, ByVal _Input As String)

 

                Dim oImp As MOSS.Impersonator = Nothing

                Dim oConst As MOSS.Constants = Nothing

 

                Dim LogFile As String = String.Empty

                Dim Source As String = String.Empty

 

                Source = _Source

                LogFile = _LogFile

 

                Try

                    oConst = New MOSS.Constants

                    oImp = New MOSS.Impersonator(oConst.SPSUserName, oConst.SPSDomain, oConst.SPSPwd)

                    oImp.Impersonate()

                    Try

                        If Not EventLog.SourceExists(LogFile) Then

                            EventLog.CreateEventSource(Source, LogFile)

                        End If

 

                        Dim evt As New EventLog

                        evt.Source = Source

                        evt.WriteEntry(_Input, EventLogEntryType.Error)

                    Catch ex As Exception

                    Finally

                        oImp.UndoImpersonation()

                    End Try

                  

                Catch ex As Exception

                End Try

End Sub

Public Class Impersonator

            Private sUsername As String = String.Empty

            Private sPassword As String = String.Empty

            Private sDomain As String = String.Empty

            Private oImpContext As WindowsImpersonationContext = Nothing

 

            Public Sub New(ByVal _username As String, ByVal _domain As String, ByVal _password As String)

                sUsername = _username

                sPassword = _password

                sDomain = _domain

            End Sub

 

            Private Function Logon() As WindowsIdentity

                Dim oWinIdentity As WindowsIdentity = Nothing

                Try

                    Dim handle As IntPtr = New IntPtr(0)

                    Dim bLogOnSuccess As Boolean = False

                    Const LOGON32_LOGON_NETWORK As Integer = 3

                    Const LOGON32_PROVIDER_DEFAULT As Integer = 0

 

                    bLogOnSuccess = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, handle)

                    If bLogOnSuccess = False Then

                        Dim errorCode As Integer = Marshal.GetLastWin32Error

                        Throw New Exception("User logon failed. Error number : " & errorCode)

                    End If

                    oWinIdentity = New WindowsIdentity(handle)

                    CloseHandle(handle)

                Catch ex As Exception

 

                End Try

 

                Return oWinIdentity

            End Function

 

            Public Sub Impersonate()

                Try

                    oImpContext = Logon.Impersonate

                Catch ex As Exception

 

                End Try

            End Sub

 

            Public Sub UndoImpersonation()

                Try

                    oImpContext.Undo()

                Catch ex As Exception

 

                End Try

            End Sub

 

            <DllImport("advapi32", SetLastError:=True)> _

          Private Shared Function LogonUser(ByVal lpszUsername As String, _

                   ByVal lpszDomain As String, _

                    ByVal lpszPassword As String, _

                   ByVal dwLogonType As Integer, _

                   ByVal dwLogonProvider As Integer, _

                   ByRef phToken As IntPtr) As Boolean

            End Function

 

            <DllImport("kernel32", CharSet:=CharSet.Auto)> _

          Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean

            End Function

 End Class

Good luck

--tolga--

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About kemaltolga

Born in Ankara, Turkey. Raised in Germany. Recruited for the Track & Field Team at Texas A&M University in College Station, TX, in January 2000. NCAA champion in 2001 (Discus). Work for design firm in southern California. Married with two daughters.