diff --git a/ExtremelySimpleLogger/FileSink.cs b/ExtremelySimpleLogger/FileSink.cs index c66998f..e2abeb5 100644 --- a/ExtremelySimpleLogger/FileSink.cs +++ b/ExtremelySimpleLogger/FileSink.cs @@ -56,5 +56,12 @@ namespace ExtremelySimpleLogger { } } + /// Disposes this sink, freeing all of the resources it uses. + public override void Dispose() { + base.Dispose(); + if(!this.reopenOnWrite) + this.writer.Dispose(); + } + } } \ No newline at end of file diff --git a/ExtremelySimpleLogger/Logger.cs b/ExtremelySimpleLogger/Logger.cs index a17a707..aba0215 100644 --- a/ExtremelySimpleLogger/Logger.cs +++ b/ExtremelySimpleLogger/Logger.cs @@ -8,7 +8,7 @@ namespace ExtremelySimpleLogger { /// To start logging with a logger, its need to be initialized. /// There are two default implementations: , and . /// - public class Logger { + public class Logger : IDisposable { /// /// All of the instances that this logger logs to. @@ -91,5 +91,13 @@ namespace ExtremelySimpleLogger { /// An optional exception whose stack trace will be appended to the message public void Fatal(object message, Exception e = null) => this.Log(LogLevel.Fatal, message, e); + /// + /// Disposes this logger, freeing all of the resources associated with its . + /// + public void Dispose() { + foreach (var sink in this.Sinks) + sink.Dispose(); + } + } } \ No newline at end of file diff --git a/ExtremelySimpleLogger/Sink.cs b/ExtremelySimpleLogger/Sink.cs index 3ba8b20..3af5ed6 100644 --- a/ExtremelySimpleLogger/Sink.cs +++ b/ExtremelySimpleLogger/Sink.cs @@ -6,7 +6,7 @@ namespace ExtremelySimpleLogger { /// A sink is a way for log messages passed to a to be processed in a certain way. /// By default, and are available. /// - public abstract class Sink { + public abstract class Sink : IDisposable { /// /// The minimum level that a log message needs to have for it to be processed by this sink. @@ -81,5 +81,11 @@ namespace ExtremelySimpleLogger { /// An optional exception whose stack trace will be appended to the message public delegate string LogFormatter(Logger logger, LogLevel level, object message, Exception e = null); + /// + /// Disposes this sink, freeing all of the resources it uses. + /// + public virtual void Dispose() { + } + } } \ No newline at end of file diff --git a/Sample/Program.cs b/Sample/Program.cs index f07a708..4bf45a2 100644 --- a/Sample/Program.cs +++ b/Sample/Program.cs @@ -36,6 +36,9 @@ namespace Sample { logger.Log(LogLevel.Trace, "This is a message that only the file sink will receive, since its minimum level is lower."); logger.Log(LogLevel.Info, "The program finished."); + + // Once we're done using the logger, we can dispose it so that our FileSink instances free their files + logger.Dispose(); } }