Search code examples
c#unit-testingevent-log

Create EventLogRecord for unit test


I implemented an API to process EventLogRecord object.

using System.Diagnostics.Eventing.Reader;

...
MyLog ProcessEvent(EventLogRecord eventRecord)

Now, I want create unit test for this and EventLogRecord doesn't have a public constructor. How do I get a EventLogRecord object without hooking up to the event logging system?


Solution

  • The standard way of making untestable items like this (non-virtual) work is by using a wrapper. Here is some pseducode of the idea:

    WrapperEventLogRecord : IEventLogRecordWrapper
    {
        ctor(EventLogRecord eventLogRecord)
        public ActivityId {get{return _eventLogRecord.ActivityId;}}
    }
    

    Now you can pass in the interface and mock it out as necessary