Tuesday, January 31, 2012

How to create Event Source using PowerShell Command in Microsoft Windows 2008 R2

In my previous Blog “Writing Event Logs for SharePoint 2010 Projectsin C#”, we looked at creating Event Logs using C# for SharePoint 2010. In order to use the event logs in specific “Source” first we need to create a source. It could be done using the code but I think it would be an added overhead to check for the source every time a log is written. Moreover, it would turn out to be a nightmare if you end up having security exceptions while creating the event source through the code :S You can easily create a source using the PowerShell Command as given below:

[System.Diagnostics.EventLog]::CreateEventSource(“MySource”, "Application")

Here you go, it is that simple …. This simple creates an event source with the name “MySource” under “Application” Log. Or you could also end up adding a new custom log by having “MyProject” instead of “Application” in the method above.

And of course don’t forget to run the power shell as Administrator ;)

3 comments:

  1. C:\PS> new-eventlog -source MySource -logname Application

    ReplyDelete
  2. Is it better to use installutil to do the event source creation for the appliation to register itself with the event source being created? I have tried this and it works fine.

    ReplyDelete