The whole mechanism of writing events to the log is rather involved
and can be complex depending on how detailed you want to get with your
messaging. I will give a brief overview of what I recently learned
and implemented but a good explanation of all the details can be found
here:
http://msdn.microsoft.com/library/de...nt_logging.asp
When you write a message to the Windows EventLog, the system looks in
the registry to see if a message resource file has been associated
with the Application Name you specify.
HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\S ervices\Eventlo g\Application\{ your
application name}
In your application you call the following
If (Not EventLog.Source Exists("Applica tion")) Then
EventLog.Create EventSource("Ap plication", "Applicatio n")
End If
which checks to see if the following registry key exists and creates
it if not:
HKEY_LOCAL_MACH INE\SYSTEM\Curr entControlSet\S ervices\Eventlo g\Application\A pplication
The documentation I've read says that you don't really need to create
the EventSource because a call to WriteEvent will create the source if
it doesn't already exist. However, to use predefined messages you
definitely need to create the event source in the registry manually
first.
Under this registry key there needs to be several values:
1. EventMessageFil e - REG_EXPAND_SZ string value specifying the path
to a message resource dll (I'll discuss that file in a moment)
2. TypesSupported - REG_DWORD value using bit masks to indicate
whether your message file handles Information, Warning, Error,
AuditSuccess, and/or AuditFailure messages.
3. CategoryMessage File - optional REG_EXPAND_SZ string value
specifying the path to a category resource dll. This could be the same
file as the EventMessageFil e or a separate file.
4. CategoryCount - optional REG_DWORD value specifying how many
Category types have been specified in the Category resource dll.
The message resource dll is created by compiling a text file through
the mc command line executable. This generates a resource file with an
rc extension. This file then needs to be resource compiled with the rc
command line executable resulting in a resource dll that you can use
for the registry entry above.
The text file specifies the message text associated with each Event ID
(or error code). A message file declaration looks something like
this:
--------------------
MessageIdTypede f=DWORD
SeverityNames=( Success=0x0:STA TUS_SEVERITY_SU CCESS
Information=0x1 :STATUS_SEVERIT Y_INFORMATION
Warning=0x2:STA TUS_SEVERITY_WA RNING
Error=0x3:STATU S_SEVERITY_ERRO R
)
MessageId=0x03E F
Severity=Error
SymbolicName=MS G_ERROR
Language=Englis h
A user has attempted to visit a non-existent page
..
--------------------
Note that the Severity value specified for a MessageID sets the high
order bits of the resulting Event ID. In other words, the message
declared above results in an Event ID of 0xC00003EF. You must pass in
this error code to get a match.
The message text you declare in the text file can have insert values
as well, specified by %n where n is 1 through 99. The message string
you pass into the call to write to the event log is used as the insert
value. If you pass in a string with embedded chr$(0), each substring
is used as a separate insert value.
--------------------
MessageId=0x03E F
Severity=Inform ation
SymbolicName=ER R_MSG_MISSING_P AGE
Language=Englis h
A user has attempted to visit a non-existent page: %1
..
--------------------
You would then change your code to something like this
Public Const ERR_MSG_MISSING _PAGE = &HC00003EF
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArg s) _
Handles MyBase.Load
{...some code that loads the page...}
If {...some test that load failed...} Then
WriteToEventLog ("MyApplication ", ERR_MSG_MISSING _PAGE, _
Request.QuerySt ring("aspxerror path"))
End If
End Sub
Sub WriteToEventLog (ByVal SourceName As String, _
ByVal EventID as Integer, _
ByVal Message As String)
'Create event log if it doesn't exist
If (Not EventLog.Source Exists(SourceNa me)) Then
EventLog.Create EventSource(Sou rceName, "Applicatio n")
End If
'Write to application event log
Dim Log As New EventLog("Appli cation")
Log.Source = SourceName
Log.WriteEntry( Message, EventLogEntryTy pe.Error, EventID)
End Sub
HTH,
Michael Levy