#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you 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.
//
#endregion
using System;
using System.Reflection;
using System.Collections;
using log4net;
using log4net.Core;
using log4net.Repository;
using log4net.Repository.Hierarchy;
/*
* Custom Logging Classes to support Remoting by reference.
*/
namespace log4net.Ext.MarshalByRef
{
///
/// LogManager that returns loggers.
///
public class MarshalByRefLogManager
{
#region Static Member Variables
///
/// The wrapper map to use to hold the objects
///
private static readonly WrapperMap s_wrapperMap = new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler));
#endregion
#region Constructor
///
/// Private constructor to prevent object creation
///
private MarshalByRefLogManager() { }
#endregion
#region Type Specific Manager Methods
///
/// Returns the named logger if it exists
///
///
/// If the named logger exists (in the default hierarchy) then it
/// returns a reference to the logger, otherwise it returns
/// null.
///
/// The fully qualified logger name to look for
/// The logger found, or null
public static ILog Exists(string name)
{
return Exists(Assembly.GetCallingAssembly(), name);
}
///
/// Returns the named logger if it exists
///
///
/// If the named logger exists (in the specified domain) then it
/// returns a reference to the logger, otherwise it returns
/// null.
///
/// the domain to lookup in
/// The fully qualified logger name to look for
/// The logger found, or null
public static ILog Exists(string domain, string name)
{
return WrapLogger(LoggerManager.Exists(domain, name));
}
///
/// Returns the named logger if it exists
///
///
/// If the named logger exists (in the specified assembly's domain) then it
/// returns a reference to the logger, otherwise it returns
/// null.
///
/// the assembly to use to lookup the domain
/// The fully qualified logger name to look for
/// The logger found, or null
public static ILog Exists(Assembly assembly, string name)
{
return WrapLogger(LoggerManager.Exists(assembly, name));
}
///
/// Returns all the currently defined loggers in the default domain.
///
///
/// The root logger is not included in the returned array.
///
/// All the defined loggers
public static ILog[] GetCurrentLoggers()
{
return GetCurrentLoggers(Assembly.GetCallingAssembly());
}
///
/// Returns all the currently defined loggers in the specified domain.
///
/// the domain to lookup in
///
/// The root logger is not included in the returned array.
///
/// All the defined loggers
public static ILog[] GetCurrentLoggers(string domain)
{
return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
}
///
/// Returns all the currently defined loggers in the specified assembly's domain.
///
/// the assembly to use to lookup the domain
///
/// The root logger is not included in the returned array.
///
/// All the defined loggers
public static ILog[] GetCurrentLoggers(Assembly assembly)
{
return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
}
///
/// Retrieve or create a named logger.
///
///
/// Retrieve a logger named as the
/// parameter. If the named logger already exists, then the
/// existing instance will be returned. Otherwise, a new instance is
/// created.
///
/// By default, loggers do not have a set level but inherit
/// it from the hierarchy. This is one of the central features of
/// log4net.
///
/// The name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(string name)
{
return GetLogger(Assembly.GetCallingAssembly(), name);
}
///
/// Retrieve or create a named logger.
///
///
/// Retrieve a logger named as the
/// parameter. If the named logger already exists, then the
/// existing instance will be returned. Otherwise, a new instance is
/// created.
///
/// By default, loggers do not have a set level but inherit
/// it from the hierarchy. This is one of the central features of
/// log4net.
///
/// the domain to lookup in
/// The name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(string domain, string name)
{
return WrapLogger(LoggerManager.GetLogger(domain, name));
}
///
/// Retrieve or create a named logger.
///
///
/// Retrieve a logger named as the
/// parameter. If the named logger already exists, then the
/// existing instance will be returned. Otherwise, a new instance is
/// created.
///
/// By default, loggers do not have a set level but inherit
/// it from the hierarchy. This is one of the central features of
/// log4net.
///
/// the assembly to use to lookup the domain
/// The name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(Assembly assembly, string name)
{
return WrapLogger(LoggerManager.GetLogger(assembly, name));
}
///
/// Shorthand for .
///
///
/// Get the logger for the fully qualified name of the type specified.
///
/// The full name of will
/// be used as the name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(Type type)
{
return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
}
///
/// Shorthand for .
///
///
/// Get the logger for the fully qualified name of the type specified.
///
/// the domain to lookup in
/// The full name of will
/// be used as the name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(string domain, Type type)
{
return WrapLogger(LoggerManager.GetLogger(domain, type));
}
///
/// Shorthand for .
///
///
/// Get the logger for the fully qualified name of the type specified.
///
/// the assembly to use to lookup the domain
/// The full name of will
/// be used as the name of the logger to retrieve.
/// the logger with the name specified
public static ILog GetLogger(Assembly assembly, Type type)
{
return WrapLogger(LoggerManager.GetLogger(assembly, type));
}
#endregion
#region Extension Handlers
///
/// Lookup the wrapper object for the logger specified
///
/// the logger to get the wrapper for
/// the wrapper for the logger specified
public static ILog WrapLogger(ILogger logger)
{
return (ILog)s_wrapperMap.GetWrapper(logger);
}
///
/// Lookup the wrapper objects for the loggers specified
///
/// the loggers to get the wrappers for
/// Lookup the wrapper objects for the loggers specified
public static ILog[] WrapLoggers(ILogger[] loggers)
{
ILog[] results = new ILog[loggers.Length];
for(int i=0; i
/// Method to create the objects used by
/// this manager.
///
/// The logger to wrap
/// The wrapper for the logger specified
private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
{
return new MarshalByRefLogImpl(logger);
}
#endregion
}
}