using System;
using System.Diagnostics;

namespace Minntech.Logging
{
	/// <summary>
	/// Summary description for ApplicationLog.
	/// </summary>
	public class ApplicationLog : ILogProvider
	{
		private string		_sourceName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
		private bool		_init		= false;
		private EventLog	_log		= null;
		
		#region construct/init/dispose

		public ApplicationLog()
		{
		}
		
		public void Initialize()
		{
			if ( ! _init )
			{
				//	insure log source has been created and registered on the local host
				if ( ! EventLog.SourceExists(this._sourceName) )
				{
					EventLog.CreateEventSource(this._sourceName, null);
					//	will not be able to access source in this session. Newly
					//	created sources require the OS to do stuff that will not
					//	happen until the next session
					return;
				}
				this._log  = new EventLog(this._sourceName);
				this._init = true;
			}
		}	

		public void Dispose()
		{
			if ( this._log != null )
				this._log.Dispose();
		}

		#endregion

		public string SourceName
		{
			get { return this._sourceName;  }
			set { this._sourceName = value; }
		}

		#region ILogProvider methods

		public void WriteLog ( string Text )
		{
			this.WriteLog(Text, EventLogEntryType.Information, null);
		}
		public void WriteLog ( string Text, EventLogEntryType LogEntryType )
		{
			this.WriteLog( Text, LogEntryType , null );
		}
		public void WriteLog ( string Text, EventLogEntryType LogEntryType , byte[] Data )
		{
			if ( ! _init ) 
				this.Initialize();
			this._log.WriteEntry(Text, LogEntryType , 0, 0, Data);
		}

		#endregion
	}
}
