0

let's suppose I've a class that inherits from the log4net.Appender.FileAppender type because I've to provide some custom log functionalities and that class owns an unmanaged resource.

In the below code I report the definition of the class and the implementation of the Disposable pattern.

public class MyLogFileAppender : FileAppender, IDisposable
{
     private IDisposable _myUnmanagedResource;

     #region Other methods used for flushing and so on are here.
     #endregion

     public void Dispose()
     {
         Dispose(true);
     }

     protected virtual void Dispose(bool disposing)
     {
         if (disposing)
         {
             _myUnmanagedResource?.Dispose();
         }
     }
}

My question is: how and when call the Dispose method? I would be nice if it's called by the underlying log4net system but I think that is not the case and I should manually invoke it.

user2896152
  • 762
  • 1
  • 9
  • 32
  • Separate, but: are you sure that `_myUnmanagedResource` is actually unmanaged? If it's `IDisposable` then it's a .NET class, and it's managed. Unmanaged resources are things like pointers to memory allocated outside of .NET – canton7 Apr 27 '22 at 08:38
  • Does this answer your question? [Proper way to shutdown a logger instance in log4Net](https://stackoverflow.com/questions/5892916/proper-way-to-shutdown-a-logger-instance-in-log4net) – canton7 Apr 27 '22 at 08:39
  • @canton7 yes sorry maybe I chose a bad name, but I mean an instance that uses an unmanaged resource and should be disposed. For example, in my actual case, that is a ManualResetEvent. – user2896152 Apr 27 '22 at 08:43

0 Answers0