Reputation: 243
Should dispose be called on the events that come back from the ReadEvent method of EventLogQuery?
var eventLogQuery = new EventLogQuery("application", PathType.LogName) ;
using (var logReader = new EventLogReader(eventLogQuery))
{
var eventInstance = logReader.ReadEvent();
while (eventInstance != null)
{
eventInstance.Dispose(); //Should this be done? Or is this not ours to dispose?
eventInstance = logReader.ReadEvent();
}
}
Should my code call a dispose on eventInstance? Or is that something else's job?
Upvotes: 1
Views: 330
Reputation: 37059
If it implements IDispose
, dispose of it before it leaves the scope where it was created -- unless you plan to keep it around for a while for some particular reason. And calling EventLogReader.ReadEvent()
is the kind of thing you should presume qualify as creation unless MSDN says otherwise (I checked -- it doesn't).
"Unless you plan to keep it around for a while" is why logReader
should be deeply reluctant to dispose of the objects it created for you: It just can't know what you plan to do with them, so it should leave the decision up to you. It's not a bad idea to check MSDN if in doubt, but it would be strange if EventLogReader.Dispose()
disposed of the query results as well. If you look at EventLogRecord
in MSDN, it doesn't say anything about the subject, so just take IDispose
as a suggestion to call Dispose()
on the thing when you are done with it.
Upvotes: 2