Thanks LucD!
I am using ESXi server and the above code was very helpful and is running absolutely fine.
So I can create user created events and can find the event in the log as type 'user'. Is it possible to generate system defined events by the user?
For example,
Event: VmPoweredOffEvent (Type: Info)
VmFailedToPowerOnEvent (Type: Minor)