You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by ym...@apache.org on 2002/09/26 00:47:39 UTC

cvs commit: jakarta-avalon-excalibur/csframework/src/cs/Logger DiagnosticsLogger.cs

ymikulski    2002/09/25 15:47:39

  Added:       csframework/src/cs/Logger DiagnosticsLogger.cs
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  jakarta-avalon-excalibur/csframework/src/cs/Logger/DiagnosticsLogger.cs
  
  Index: DiagnosticsLogger.cs
  ===================================================================
  //------------------------------------------------------------------------------
  // Copyright (C) The Apache Software Foundation. All rights reserved.
  //
  // This software is published under the terms of the Apache Software License
  // version 1.1, a copy of which has been included  with this distribution in
  // the LICENSE.txt file.
  //------------------------------------------------------------------------------
  using System;
  using System.Diagnostics; 
  
  namespace Apache.Avalon.Logger
  {
  	/// <summary>
  	/// The Logger using standart Diagnostics namespace.
  	/// </summary>
  	public class DiagnosticsLogger: ILogger
  	{
      
  		EventLog logger;
  
  		/// <summary>
  		/// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
  		/// </summary>
  		/// <param name="logName"><see cref="EventLog.Log"/></param>
  		public DiagnosticsLogger(string logName)
  		{
  			logger = new EventLog(logName);
  		}
  
  		/// <summary>
  		/// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
  		/// </summary>
  		/// <param name="logName"><see cref="EventLog.Log"/></param>
  		/// <param name="source"><see cref="EventLog.Source"/></param>
  		public DiagnosticsLogger(string logName, string source)
  		{
  
  			// Create the source, if it does not already exist.
  			if(!EventLog.SourceExists(source))
  			{
  				EventLog.CreateEventSource(source, logName);
  			}
  
  			logger = new EventLog(logName); 
  		}
  
  		/// <summary>
  		/// Creates a logger based on <see cref="System.Diagnostics.EventLog"/>.
  		/// </summary>
  		/// <param name="logName"><see cref="EventLog.Log"/></param>
  		/// <param name="machineName"><see cref="EventLog.MachineName"/></param>
  		/// <param name="source"><see cref="EventLog.Source"/></param>
  		public DiagnosticsLogger(string logName, string machineName, string source)
  		{
  			
  			// Create the source, if it does not already exist.
  			if(!EventLog.SourceExists(source, machineName))
  			{
  				EventLog.CreateEventSource(source, logName, machineName);
  			}
    			
  			logger = new EventLog(logName, machineName, source);
  		}
  
  		/// <summary>
  		/// Logs a debug message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		public void Debug(string message )
  		{
  			Debug(message, null);
  		}
  
  		/// <summary>
  		/// Logs a debug message. 
  		/// </summary>
  		/// <param name="message">The Message</param>
  		/// <param name="exception">The Exception</param>
  		public void Debug(string message, Exception exception)
  		{
  			System.Diagnostics.Debug.WriteLine(string.Format("message: {0}", message));
  			
  			if (exception != null)
  			{
  				System.Diagnostics.Debug.WriteLine(string.Format("exception: {0}", exception));
  			}
  		}
  
  		/// <summary>
  		/// Debug level is always enabled.
  		/// </summary>
  		/// <value>The Value is always True</value> 
  		public bool IsDebugEnabled
  		{
  			get
  			{
  				return true;
  			}
          
  		}
  
  		/// <summary>
  		/// Logs an info message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		public void Info(string message )
  		{
  			Info(message, null);
  		}
  
  		/// <summary>
  		/// Logs an info message. 
  		/// </summary>
  		/// <param name="message">The Message</param>
  		/// <param name="exception">The Exception</param>
  		public void Info(string message, Exception exception)
  		{
  			Log(message, exception, EventLogEntryType.Information);  
  		}
  
  		/// <summary>
  		/// Information level is always enabled.
  		/// </summary>
  		/// <value>The Value is always True</value> 
  		public bool IsInfoEnabled
  		{
  			get
  			{
  				return true;
  			}
  		}
  
  		/// <summary>
  		/// Logs a warn message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		public void Warn(string message )
  		{
  			Warn(message, null);
  		}
  
  		/// <summary>
  		/// Logs a warn message. 
  		/// </summary>
  		/// <param name="message">The Message</param>
  		/// <param name="exception">The Exception</param>
  		public void Warn(string message, Exception exception)
  		{
  			Log(message, exception, EventLogEntryType.Warning); 
  		}
  
  		/// <summary>
  		/// Warning level is always enabled.
  		/// </summary>
  		/// <value>The Value is always True</value> 
  		public bool IsWarnEnabled
  		{
  			get
  			{
  				return true;
  			}
  		}
  
  		/// <summary>
  		/// Logs an error message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		public void Error(string message )
  		{
  			Error(message, null);
  		}
  
  		/// <summary>
  		/// Logs an error message. 
  		/// </summary>
  		/// <param name="message">The Message</param>
  		/// <param name="exception">The Exception</param>
  		public void Error(string message, Exception exception)
  		{
  			Log(message, exception, EventLogEntryType.Error);
  		}
  
  		/// <summary>
  		/// Error level is always enabled.
  		/// </summary>
  		/// <value>The Value is always True</value> 
  		public bool IsErrorEnabled
  		{
  			get
  			{
  				return true;
  			}
  		}
  
  		/// <summary>
  		/// Logs a fatal error message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		public void FatalError(string message )
  		{
  			FatalError(message, null);
  		}
  
  		/// <summary>
  		/// Logs a fatal error message.
  		/// </summary>
  		/// <param name="message">The Message</param>
  		/// <param name="exception">The Exception</param>
  		public void FatalError(string message, Exception exception)
  		{
  			Log(message, exception, EventLogEntryType.Error);
  		}
  
  		/// <summary>
  		/// FatalError level is always enabled.
  		/// </summary>
  		/// <value>The Value is always True</value> 
  		public bool IsFatalErrorEnabled
  		{
  			get 
  			{
  				return true; 
  			}
  		}
  
  		/// <summary>
  		/// Create a new logger with the same Log, MachineName and Source properties.
  		/// </summary>
  		/// <param name="name">Ignored, cause a source can only be registered to one log at a time.</param>
  		/// <returns>The New ILogger instance.</returns> 
  		/// <exception cref="System.ArgumentException">If the name has an empty element name.</exception>
  		public ILogger CreateChildLogger(string name )
  		{
  			return new DiagnosticsLogger(logger.Log, logger.MachineName, logger.Source);  
  		}
  
  		private void Log(string message, Exception exception, EventLogEntryType type)
  		{
  			string result;
  
  			if (exception == null)
  			{
  				result = string.Format("message: {0}", message);
  			}
  			else
  			{
  				result = string.Format("message: {0}; exception: {1}", message, exception);
  			}
  			
  			logger.WriteEntry(result, type);
  		}
  	}
  }
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>