← All articles

Creating .NET Event Sources with PowerShell

Skip writing a whole app to register a Windows Event Log source - a two-line PowerShell command checks for it and creates it if missing.

Donn Felker

Donn Felker

2008.02.26 · 1 min read

I use Enterprise Library for Logging and I use different event sources for each app. I’ve noticed that creating event sources is kind of a pain sometimes. Either you have to write an app that does it for you, or you have to write some logic in your code that checks for the event source. Either way, I think that’s too bloated for in-house software. Now for software that has to be deployed, then ok, no problem I see the use. But we’re talking bout non-released code here. Code that is used on an internal site or externally facing site.

Or you can use PowerShell, simple as pie. 🙂

This one liner will check for the Event Source and if it does not exist, it will create it for you. Real easy. Gotta love PowerShell.

if (![System.Diagnostics.EventLog]::SourceExists(“MySourceName”)) { [System.Diagnostics.EventLog]::CreateEventSource(“MySourceName”, “Application”) }

Now if we write an event to the event log, it will show up because the source now exists. 🙂

You HAVE TO love one liners that can replace entire applications. 🙂 Albeit, not a big app, but an app, none the less.

Donn Felker

Written by Donn Felker

I've spent 25+ years shipping software, writing books, and running my own companies. I write about thinking clearly, working for yourself, and doing real work while AI rewrites the rules. New essays land here every week.

Get the newsletter

Keep reading