NUnit testing of eventlog entry?

  • Thread starter Thread starter Ole Hanson
  • Start date Start date
O

Ole Hanson

Hi

I am trying to engineer a way of testing that my logging framework is
capable of writing to my eventlog. I want to include this test in my already
existing NUnit tests - but I'm a little low on ideas as how to assert that I
actually did write to the eventlog successfully.

Naturally I want this assertion without having to open my eventlog and
visually verify the writing.

Any ideas???

Thanks!
 
Ole,
Naturally I want this assertion without having to open my eventlog and
visually verify the writing.
Have you considered using a snippet of C# code (within the unit test itself)
to "open" the eventlog & "read" the entries to see if the entry you expect
was written?

You can use the EventLog.Entries property to "read" the entries from an
EventLog...

http://msdn.microsoft.com/library/d...ystemDiagnosticsEventLogClassEntriesTopic.asp

Alternatively you might be able to use the EventWritten event to see if an
entry was written during the test...

If possible I would attempt to use a private test Event Log above so as to
have total control on outside programs interferring with the test...

Hope this helps
Jay
 
nUnit rides along with exceptions from the .NET framework and you may be
able to assert your test on an event log exception. A handled exception by
your code would by default cause nUnit to fail an assertion test unless you
override it with an ExpectedException parameter in your test method.

You could also use a conditional assertion in nUnit to check the return
value of the write method in your logging classes, that way you could at
least evaluate against a succesful write. NUnit also recognizes unhandled
exceptions as an indicator that the code has failed in some way. As such, it
reports unhandled exceptions as failures of any test itself - you could
likely assume that any other result permutation is a success without having
to actually open the event log

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
Great stuff!

Just what I needed.
Thanks

/Ole


Jay B. Harlow said:
Ole,
Have you considered using a snippet of C# code (within the unit test
itself) to "open" the eventlog & "read" the entries to see if the entry
you expect was written?

You can use the EventLog.Entries property to "read" the entries from an
EventLog...

http://msdn.microsoft.com/library/d...ystemDiagnosticsEventLogClassEntriesTopic.asp

Alternatively you might be able to use the EventWritten event to see if an
entry was written during the test...

If possible I would attempt to use a private test Event Log above so as to
have total control on outside programs interferring with the test...

Hope this helps
Jay
 
Back
Top