226 lines
9.6 KiB
C#
226 lines
9.6 KiB
C#
/*
|
|
* 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
|
|
{
|
|
/// <summary>
|
|
/// "Handle" containing the state information for a log message write session.
|
|
/// </summary>
|
|
protected class WriteCycleHandle
|
|
{
|
|
/// <summary>
|
|
/// The filename of the log file receiving the messages being supplied to this
|
|
/// write session.
|
|
/// </summary>
|
|
public string FullLogFileName { get; set; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="System.Threading.AutoResetEvent"/> whose "set" kicks off
|
|
/// a write cycle.
|
|
/// </summary>
|
|
public AutoResetEvent CycleTrigger { get; set; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="System.Threading.AutoResetEvent"/> that is signaled when
|
|
/// a write cycle completes.
|
|
/// </summary>
|
|
public AutoResetEvent CycleCompletionTrigger { get; set; }
|
|
|
|
/// <summary>
|
|
/// The <see cref="Logging.TextLogger.WriteCycleExceptionHandler"/> to be
|
|
/// invoked when the write cycle encounters an exception.
|
|
/// </summary>
|
|
public WriteCycleExceptionHandler OnWriteCycleException { get; set; }
|
|
|
|
/// <summary>
|
|
/// <see cref="bool"/> flag that controls whether or not the write cycle should
|
|
/// auto-terminate whenever it encounters an exception.
|
|
/// </summary>
|
|
public bool TerminateCycleOnException { get; set; }
|
|
|
|
private static readonly object MyLock = new object();
|
|
/// <summary>
|
|
/// Get/set the <see cref="bool"/> flag that will terminate the associated write cycle when set to 'true'.
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// The actually queue of log message <see cref="string"/>s to be written to the active log file.
|
|
/// </summary>
|
|
public Queue<string> 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<string> _WriteQueue = null;
|
|
private bool _IsWriteQueueInitialized = false;
|
|
|
|
/// <summary>
|
|
/// Callback to be invoked when the write cycle has closed it's associated log file's
|
|
/// write handle for the last time.
|
|
/// </summary>
|
|
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;
|
|
|
|
/// <summary>
|
|
/// Initialize an instance of the <see cref="Logging.TextLogger.WriteCycleHandle"/> class.
|
|
/// </summary>
|
|
///
|
|
/// <param name="fullLogFileName">
|
|
/// The filename of the log file receiving the messages being supplied to this
|
|
/// write session.
|
|
/// </param>
|
|
///
|
|
/// <param name="writeQueue">
|
|
/// The actually queue of log message <see cref="string"/>s to be written to the active log file.
|
|
/// </param>
|
|
///
|
|
/// <param name="onWriteCycleTermination">
|
|
/// Callback to be invoked when the write cycle has closed it's associated log file's
|
|
/// write handle for the last time.
|
|
/// </param>
|
|
///
|
|
/// <param name="onWriteCycleException">
|
|
/// Callback to be invoked when the write cycle has closed it's associated log file's
|
|
/// write handle for the last time.
|
|
/// </param>
|
|
///
|
|
/// <param name="terminateCycleOnException">
|
|
/// <see cref="bool"/> flag that controls whether or not the write cycle should
|
|
/// auto-terminate whenever it encounters an exception.
|
|
/// </param>
|
|
///
|
|
public WriteCycleHandle(string fullLogFileName,
|
|
Queue<string> 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;
|
|
}
|
|
}
|
|
}
|
|
}
|