For some special case, you might need to write the log to Windows Event Log,
To kick start, create a Class so that you can alwiz reuse the code when want to write to Event Log. In my case, I’ve created a Class called EventLogHelper:
class EventLogHelper
{
public static void writeEventLog(String description)
{
String sSource;
String sLog;
sSource = "MyService";
sLog = "Application";
if (!EventLog.SourceExists(sSource))
{
EventLog.CreateEventSource(sSource, sLog);
}
EventLog.WriteEntry(sSource, description);
}
}
So, from your codes, you just need to call the Helper class whenever you want to log to Event Log:EventLogHelper.writeEventLog("Testing 123");
For the first timer, you could encounter the following exception:“The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.” System.Exception {System.
Security.SecurityException}
If you ecounter the exception above, then you will need to manually create New Event Source Entry in registry.
Do do that , access HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\
For instance, the below screen shots & steps are taken from Windows 7:
(1) Go to run, type regedit
regedit
(2) Windows will prompt ” Do you allow the following program to make changes on this computer?”
Click “yes”
(3) To to this path :
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application
(4) Add New Key
Right Click Application –> New –> Key
(5) Name the new key as “MyService”
(6) Run your application![]()
(7) Check the Event Log
Windows –> Control Panel –> Administrator –> Event Viewer
No comments:
Post a Comment