/* * TextLogger.WriteCycleHandle.cs * * Copyright © 2010 * Diversified Technical Systems, Inc. * All Rights Reserved. */ using System; using System.Collections.Generic; using System.Threading; namespace DTS.Common.Utilities.Logging { // *** see TextLogger.cs *** public partial class TextLogger { /// /// "Handle" containing the state information for a log message write session. /// protected class WriteCycleHandle { /// /// The filename of the log file receiving the messages being supplied to this /// write session. /// public string FullLogFileName { get; set; } /// /// The whose "set" kicks off /// a write cycle. /// public AutoResetEvent CycleTrigger { get; set; } /// /// The that is signaled when /// a write cycle completes. /// public AutoResetEvent CycleCompletionTrigger { get; set; } /// /// The to be /// invoked when the write cycle encounters an exception. /// public WriteCycleExceptionHandler OnWriteCycleException { get; set; } /// /// flag that controls whether or not the write cycle should /// auto-terminate whenever it encounters an exception. /// public bool TerminateCycleOnException { get; set; } private static readonly object MyLock = new object(); /// /// Get/set the flag that will terminate the associated write cycle when set to 'true'. /// public bool TerminateWriteCycle { get { try { lock (MyLock) { if (!_IsTerminateWriteCycleInitialized) throw new ApplicationException("TerminateWriteCycle value has not been initialized"); else return _TerminateWriteCycle; } } catch (Exception ex) { throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".TerminateWriteCycle", ex); } } set { try { lock (MyLock) { // // Set the termination flag and then kick off a cycle so it goes into // effect asap. // _IsTerminateWriteCycleInitialized = true; _TerminateWriteCycle = value; if (_TerminateWriteCycle) CycleTrigger.Set(); } } catch (Exception ex) { throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".TerminateWriteCycle", ex); } } } private bool _TerminateWriteCycle; private bool _IsTerminateWriteCycleInitialized = false; /// /// The actually queue of log message s to be written to the active log file. /// public Queue WriteQueue { get { try { if (!_IsWriteQueueInitialized) throw new ApplicationException("WriteQueue has not been initialized"); else return _WriteQueue; } catch (Exception ex) { throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".WriteQueue", ex); } } set { try { _IsWriteQueueInitialized = true; _WriteQueue = value; } catch (Exception ex) { throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".WriteQueue", ex); } } } private Queue _WriteQueue = null; private bool _IsWriteQueueInitialized = false; /// /// Callback to be invoked when the write cycle has closed it's associated log file's /// write handle for the last time. /// public WriteCycleCallback OnWriteCycleTermination { get { try { lock (MyLock) { return _OnWriteCycleTermination; } } catch (Exception ex) { throw new ApplicationException("encountered problem getting value for property " + GetType().FullName + ".OnWriteCycleTermination", ex); } } set { try { lock (MyLock) { // // Set the cycle termination callback. If we're undergoing termination and there's // nothing left in the write queue, then go ahead and immediately call the termination // callback. Null the reference after the call so we don't accidentally somehow end up // calling it twice. // _OnWriteCycleTermination = value; if (_IsTerminateWriteCycleInitialized && _TerminateWriteCycle && _IsWriteQueueInitialized && _WriteQueue.Count <= 0) { _OnWriteCycleTermination(_WriteQueue); _OnWriteCycleTermination = null; } } } catch (Exception ex) { throw new ApplicationException("encountered problem setting value for property " + GetType().FullName + ".OnWriteCycleTermination", ex); } } } private WriteCycleCallback _OnWriteCycleTermination = null; /// /// Initialize an instance of the class. /// /// /// /// The filename of the log file receiving the messages being supplied to this /// write session. /// /// /// /// The actually queue of log message s to be written to the active log file. /// /// /// /// Callback to be invoked when the write cycle has closed it's associated log file's /// write handle for the last time. /// /// /// /// Callback to be invoked when the write cycle has closed it's associated log file's /// write handle for the last time. /// /// /// /// flag that controls whether or not the write cycle should /// auto-terminate whenever it encounters an exception. /// /// public WriteCycleHandle(string fullLogFileName, Queue writeQueue, WriteCycleCallback onWriteCycleTermination, WriteCycleExceptionHandler onWriteCycleException, bool terminateCycleOnException) { TerminateWriteCycle = false; FullLogFileName = fullLogFileName; WriteQueue = writeQueue; CycleTrigger = new AutoResetEvent(false); CycleCompletionTrigger = new AutoResetEvent(false); OnWriteCycleTermination = onWriteCycleTermination; OnWriteCycleException = onWriteCycleException; TerminateCycleOnException = terminateCycleOnException; } } } }