Search code examples
c#eventlog-source

Eventlog leakage issue(.net profiler shows undisposed)


I am writing error to event log but when i checked for leakage i got event log leakage in .net profiler, Do I need to dispose this object? Is it will create any issue in multi threading?

public override void ProcessWarning(string title, string message)
{
    if (title == null)
        eventLog.WriteEntry(message, EventLogEntryType.Warning);
    else
        eventLog.WriteEntry(title + '\n' + message, EventLogEntryType.Warning);
    }
}

Solution

  • The EventLog class extends Component, which shows that it implements IDisposable. So yes, you will need to (eventually) Dispose() of it.

    Apparently your eventLog is a field in your class. This (an IDisposable field) means that your class needs to implement IDisposable itself. In your own Dispose method you will then need to dispose of that eventLog.

    And of course this means that anything using this class must treat it as the IDisposable that it now is.