Eggins.com
Powershell, .Net, Visual Studio, Team Foundation Server, Windows Communication Foundation

Add event source using Powershell

If you write to the event log from an ASP.Net application, the default ASPNET account will not have the correct privileges to add your event source. You have to add the event source outside of the ASP.Net application. I used to use a C# console application that I wrote for the purpose of registering my ASP.Net application event sources. Now that I have Powershell on all of the computers I work on, I would rather do this from within Powershell.

At the simplest, this will work:

[System.Diagnostics.EventLog]::CreateEventSource("MySourceName", "Application")

But the following function will check if the event source is already registered before registering the name.
 
Function RegisterEventSource ([String]$sourceName, [string]$eventLogName = "Application") {
  if ([System.Diagnostics.EventLog]::SourceExists($sourceName) -eq $False) {
    [System.Diagnostics.EventLog]::CreateEventSource($sourceName, $eventLogName)
    Write-Host "The event source '$sourceName' has been added to the '$eventLogName' event log."
  }
  else {
    Write-Host "The event source '$sourceName' already existed"
  }
}

So now all you need is Powershell, and all developers have Powershell installed don't they? :-) 


Posted Feb 16 2008, 08:29 PM by David
Filed under: , ,

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Copyright eggins.com, 1998 - 2008
Powered by Community Server (Non-Commercial Edition), by Telligent Systems