One of the more common questions we get about the System.Diagnostics.Tracing.EventSource class is whether it can be used to log message to the
Windows Event Log (that is can be viewed with eventvwr.exe). This EventLog support is also called 'Channel support' because that is the official name for a 'stream of events' in the Windows event log.
The answer to whether EventSource supports the Windows Event log as a target has been 'no' but now the answer is 'yes'. Moreover, you don't even have to wait for the next version of the .NET Framework to get it.
That is because we have now created a 'Stand alone' version of EventSource and published as the Microsoft EventSource Library NuGet Package (currently in Prerelease form, so you have to set your filtering to include prereleases to see it). Note that the class in this package is in a different namespace Microsoft.Diagnostics.Tracing.EventSource, so it is 'side by side' with System.Diagnostics.Tracing.EventSource that comes with the V4.5 framework. It is however intended to be 100% compatible with code using the framework version. It is also effectively your way of getting at new features of the framework version before they are officially released (or your customers have upgraded their framework). Switching from one to the other should be as easy as changing a 'Using' declaration at the top of the file that uses EvenSource.
Thus Windows Event Log support is just the first installment of new features that we will be bringing to you through NuGet as a way of getting started now without having to wait for the next version of the runtime.
To see more details about the Event Log support see the dotnet Blog entry 'Announcing the EventSource NuGet Package : Write to the Windows Event Log'.
Along with the Microsoft EventSource Library NuGet Package, there is also the Microsoft EventSource Library Samples NuGet Package. This package makes it very easy to download some working EventSource code and see well commented examples of its use. The best way to use this samples package is to
- Create a new Console Application in Visual Studio called 'DemoEventSource'
- Reference the Microsoft EventSource Library Samples NuGet Package from this new project by
- Right clicking on the 'References node in the Project tree
- Selecting 'Manage NuGet Packages
- Searching for 'EventSource Samples'
- Selecting the Microsoft EventSource Library Samples package
- It will download some code to a 'EventSourceSamples directory, which you look at
- You run the code by adding the following line to your main program
- Hit F5.
Some of the samples are very similar to samples I have blogged about in the past, but the Window Event Log sample is new. It is also more complex because there is a registration (install time) step with the operating system. This is the price you pay for wanting your messages to get to the Windows EventLog.
Finally, also included in both packages is a EventSource Users Guide. This contains information similar to things I have blogged about, as well as best practices etc. Note that the previous link (as the file in this blog) may not be up to date. The most up to date version is in the Microsoft EventSource Library NuGet Package.