#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Diagnostics;
using System.IO;
using HeuristicLab.Tracing.Properties;
using log4net;
using log4net.Config;
namespace HeuristicLab.Tracing {
///
/// HeuristicLab Tracing entry point. Default logger. Reads configured tracing
/// file and provides automatic logging with reflection of the calling type.
///
public class Logger {
///
/// true if Configure has been called already.
///
protected static bool IsConfigured = false;
///
/// Configures this instance: Reads the log file specified in the settings.
///
protected static void Configure() {
if (IsConfigured) return;
IsConfigured = true;
if (string.IsNullOrEmpty(Settings.Default.TracingLog4netConfigFile)) {
Settings.Default.TracingLog4netConfigFile =
"HeuristicLab.log4net.xml";
}
XmlConfigurator.ConfigureAndWatch(
new FileInfo(Settings.Default.TracingLog4netConfigFile));
Info("logging initialized " + DateTime.Now);
}
///
/// Gets the default logger for the calling class n levels up in the
/// call hierarchy.
///
/// The number of parent calls.
/// An instance.
public static ILog GetDefaultLogger(int nParents) {
Configure();
StackFrame frame = new StackFrame(nParents + 1);
return LogManager.GetLogger(frame.GetMethod().DeclaringType);
}
///
/// Gets the default logger: The logger for the class of the
/// calling method.
///
/// An instance.
public static ILog GetDefaultLogger() {
Configure();
StackFrame frame = new StackFrame(1);
return LogManager.GetLogger(frame.GetMethod().DeclaringType);
}
///
/// Issues a debug message to the default logger.
///
/// The message.
public static void Debug(object message) {
GetDefaultLogger(1).Debug(message);
}
///
/// Issues an informational message to the default logger.
///
/// The message.
public static void Info(object message) {
GetDefaultLogger(1).Info(message);
}
///
/// Issues a warning message to the default logger.
///
/// The message.
public static void Warn(object message) {
GetDefaultLogger(1).Warn(message);
}
///
/// Issues an error message to the default logger.
///
/// The message.
public static void Error(object message) {
GetDefaultLogger(1).Error(message);
}
///
/// Issues a fatal error message to the default logger.
///
/// The message.
public static void Fatal(object message) {
GetDefaultLogger(1).Fatal(message);
}
///
/// Issues a debug message to the logger of the specified type.
///
/// The type.
/// The message.
public static void Debug(Type type, object message) {
Configure();
LogManager.GetLogger(type).Debug(message);
}
///
/// Issues an iformational message to the logger of the specified
/// type.
///
/// The type.
/// The message.
public static void Info(Type type, object message) {
Configure();
LogManager.GetLogger(type).Info(message);
}
///
/// Issues a warning message to the logger of
/// the specified type.
///
/// The type.
/// The message.
public static void Warn(Type type, object message) {
Configure();
LogManager.GetLogger(type).Warn(message);
}
///
/// Issues an error message to the logger of the specified
/// type.
///
/// The type.
/// The message.
public static void Error(Type type, object message) {
Configure();
LogManager.GetLogger(type).Error(message);
}
///
/// Issues a fatal error message to the logger of
/// the specified type.
///
/// The type.
/// The message.
public static void Fatal(Type type, object message) {
Configure();
LogManager.GetLogger(type).Fatal(message);
}
///
/// Issues a debug message to the default
/// logger including an exception.
///
/// The message.
/// The exception.
public static void Debug(object message, Exception exception) {
GetDefaultLogger(1).Debug(message, exception);
}
///
/// Issues an informational message to the default
/// logger including an exception.
///
/// The message.
/// The exception.
public static void Info(object message, Exception exception) {
GetDefaultLogger(1).Info(message, exception);
}
///
/// Issues a warning message to the default
/// logger including an exception.
///
/// The message.
/// The exception.
public static void Warn(object message, Exception exception) {
GetDefaultLogger(1).Warn(message, exception);
}
///
/// Issues an error message to the default
/// logger including an exception.
///
/// The message.
/// The exception.
public static void Error(object message, Exception exception) {
GetDefaultLogger(1).Error(message, exception);
}
///
/// Issues a fatal error message to the default
/// logger including an exception.
///
/// The message.
/// The exception.
public static void Fatal(object message, Exception exception) {
GetDefaultLogger(1).Fatal(message, exception);
}
///
/// Issues a debug message to the logger of the specified
/// type including an exception.
///
/// The type.
/// The message.
/// The exception.
public static void Debug(Type type, object message, Exception exception) {
Configure();
LogManager.GetLogger(type).Debug(message, exception);
}
///
/// Issues an informational message to the logger of the specified
/// type including an exception.
///
/// The type.
/// The message.
/// The exception.
public static void Info(Type type, object message, Exception exception) {
Configure();
LogManager.GetLogger(type).Info(message, exception);
}
///
/// Issues a warning message to the logger of the specified
/// type including an exception.
///
/// The type.
/// The message.
/// The exception.
public static void Warn(Type type, object message, Exception exception) {
Configure();
LogManager.GetLogger(type).Warn(message, exception);
}
///
/// Issues an error message to the logger of the specified
/// type including an exception.
///
/// The type.
/// The message.
/// The exception.
public static void Error(Type type, object message, Exception exception) {
Configure();
LogManager.GetLogger(type).Error(message, exception);
}
///
/// Issues a fatal error message to the logger of the specified
/// type including an exception.
///
/// The type.
/// The message.
/// The exception.
public static void Fatal(Type type, object message, Exception exception) {
Configure();
LogManager.GetLogger(type).Fatal(message, exception);
}
}
}