// Copyright 2003-2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Framework { using System; using System.Diagnostics; /// /// The Logger using standart Diagnostics namespace. /// public class DiagnosticsLogger : ILogger { EventLog m_logger; /// /// Creates a logger based on . /// /// public DiagnosticsLogger(string logName) { m_logger = new EventLog(logName); } /// /// Creates a logger based on . /// /// /// public DiagnosticsLogger(string logName, string source) { // Create the source, if it does not already exist. if(!EventLog.SourceExists(source)) { EventLog.CreateEventSource(source, logName); } m_logger = new EventLog(logName); } /// /// Creates a logger based on . /// /// /// /// 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); } m_logger = new EventLog(logName, machineName, source); } /// /// Logs a debug message. /// /// The Message public void Debug(string message ) { Debug(message, null as Exception); } /// /// Logs a debug message. /// /// The Message /// The Exception 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)); } } /// /// Logs a debug message. /// /// Message format /// Array of objects to write using format public void Debug( string format, params Object[] args ) { Debug(String.Format(format, args), null as Exception); } /// /// Debug level is always enabled. /// /// The Value is always True public bool IsDebugEnabled { get { return true; } } /// /// Logs an info message. /// /// The Message public void Info(string message ) { Info(message, null as Exception); } /// /// Logs an info message. /// /// The Message /// The Exception public void Info(string message, Exception exception) { Log(message, exception, EventLogEntryType.Information); } /// /// Logs an info message. /// /// Message format /// Array of objects to write using format public void Info( string format, params Object[] args ) { Info(String.Format(format, args)); } /// /// Information level is always enabled. /// /// The Value is always True public bool IsInfoEnabled { get { return true; } } /// /// Logs a warn message. /// /// The Message public void Warn(string message ) { Warn(message, null as Exception); } /// /// Logs a warn message. /// /// The Message /// The Exception public void Warn(string message, Exception exception) { Log(message, exception, EventLogEntryType.Warning); } /// /// Logs a warn message. /// /// Message format /// Array of objects to write using format public void Warn( string format, params Object[] args ) { Warn(String.Format(format, args)); } /// /// Warning level is always enabled. /// /// The Value is always True public bool IsWarnEnabled { get { return true; } } /// /// Logs an error message. /// /// The Message public void Error(string message ) { Error(message, null as Exception); } /// /// Logs an error message. /// /// The Message /// The Exception public void Error(string message, Exception exception) { Log(message, exception, EventLogEntryType.Error); } /// /// Logs an error message. /// /// Message format /// Array of objects to write using format public void Error( string format, params Object[] args ) { Error(String.Format(format, args)); } /// /// Error level is always enabled. /// /// The Value is always True public bool IsErrorEnabled { get { return true; } } /// /// Logs a fatal error message. /// /// The Message public void FatalError(string message ) { FatalError(message, null as Exception); } /// /// Logs a fatal error message. /// /// The Message /// The Exception public void FatalError(string message, Exception exception) { Log(message, exception, EventLogEntryType.Error); } /// /// Logs a fatal error message. /// /// Message format /// Array of objects to write using format public void FatalError( string format, params Object[] args ) { FatalError(String.Format(format, args)); } /// /// FatalError level is always enabled. /// /// The Value is always True public bool IsFatalErrorEnabled { get { return true; } } /// /// Create a new logger with the same Log, MachineName and Source properties. /// /// Ignored, cause a source can only be registered to one log at a time. /// The New ILogger instance. /// If the name has an empty element name. public ILogger CreateChildLogger(string name ) { return new DiagnosticsLogger(m_logger.Log, m_logger.MachineName, m_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); } m_logger.WriteEntry(result, type); } } }