namespace Wayne.Lib.Log
{
    /// <summary>
    /// A log writer that writes to the EventLog Publisher/subscriber interface.
    /// </summary>
    internal class EventLogSubscriptionLogWriter : LogWriter
    {
        #region Fields

        private string subscriberId;
        private LogConfigEventLogStorageType storageType;

        #endregion

        #region Construction

        public EventLogSubscriptionLogWriter(string logName, LogConfigEventLogSubscriptionOutput output)
            : base(logName)
        {
            subscriberId = output.SubscriberId;
            storageType = output.StorageType;
        }

        #endregion

        #region Properties

        /// <summary>
        /// The id of the subscriber that should pick up the events from this log writer.
        /// </summary>
        public string SubscriberId
        {
            get { return subscriberId; }
        }

        /// <summary>
        /// Type of storage that this log writer manages.
        /// </summary>
        internal LogConfigEventLogStorageType StorageType
        {
            get { return storageType; }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Publishes the specified entry to the event publisher. If it is not 
        /// an event log entry, it is just ignored.
        /// </summary>
        /// <param name="logEntry"></param>
        public override void PerformWrite(LogEntry logEntry)
        {
            EventLogEntry eventLogEntry = logEntry as EventLogEntry;
            if (eventLogEntry != null)
                Logger.PublishEventLog(this.subscriberId, eventLogEntry, this.storageType);
        }

        /// <summary>
        /// Disposes the internal resources.
        /// </summary>
        /// <param name="disposing"></param>
        /// <param name="reason"></param>
        protected override void Dispose(bool disposing, string reason)
        {
        }

        #endregion
    }
}