#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.Collections;
#if !NETSTANDARD1_3
using System.Reflection;
#endif
using log4net.Appender;
using log4net.Layout;
using log4net.Util;
using log4net.Repository;
using log4net.Repository.Hierarchy;
namespace log4net.Config
{
///
/// Use this class to quickly configure a .
///
///
///
/// Allows very simple programmatic configuration of log4net.
///
///
/// Only one appender can be configured using this configurator.
/// The appender is set at the root of the hierarchy and all logging
/// events will be delivered to that appender.
///
///
/// Appenders can also implement the interface. Therefore
/// they would require that the method
/// be called after the appenders properties have been configured.
///
///
/// Nicko Cadell
/// Gert Driesen
public sealed class BasicConfigurator
{
#region Private Static Fields
///
/// The fully qualified type of the BasicConfigurator class.
///
///
/// Used by the internal logger to record the Type of the
/// log message.
///
private static readonly Type declaringType = typeof(BasicConfigurator);
#endregion Private Static Fields
#region Private Instance Constructors
///
/// Initializes a new instance of the class.
///
///
///
/// Uses a private access modifier to prevent instantiation of this class.
///
///
private BasicConfigurator()
{
}
#endregion Private Instance Constructors
#region Public Static Methods
#if !NETSTANDARD1_3
///
/// Initializes the log4net system with a default configuration.
///
///
///
/// Initializes the log4net logging system using a
/// that will write to Console.Out. The log messages are
/// formatted using the layout object
/// with the
/// layout style.
///
///
public static ICollection Configure()
{
return BasicConfigurator.Configure(LogManager.GetRepository(Assembly.GetCallingAssembly()));
}
///
/// Initializes the log4net system using the specified appenders.
///
/// The appenders to use to log all logging events.
///
///
/// Initializes the log4net system using the specified appenders.
///
///
public static ICollection Configure(params IAppender[] appenders)
{
ArrayList configurationMessages = new ArrayList();
ILoggerRepository repository = LogManager.GetRepository(Assembly.GetCallingAssembly());
using (new LogLog.LogReceivedAdapter(configurationMessages))
{
InternalConfigure(repository, appenders);
}
repository.ConfigurationMessages = configurationMessages;
return configurationMessages;
}
///
/// Initializes the log4net system using the specified appender.
///
/// The appender to use to log all logging events.
///
///
/// Initializes the log4net system using the specified appender.
///
///
public static ICollection Configure(IAppender appender)
{
return Configure(new IAppender[] { appender });
}
#endif // !NETSTANDARD1_3
///
/// Initializes the with a default configuration.
///
/// The repository to configure.
///
///
/// Initializes the specified repository using a
/// that will write to Console.Out. The log messages are
/// formatted using the layout object
/// with the
/// layout style.
///
///
public static ICollection Configure(ILoggerRepository repository)
{
ArrayList configurationMessages = new ArrayList();
using (new LogLog.LogReceivedAdapter(configurationMessages))
{
// Create the layout
PatternLayout layout = new PatternLayout();
layout.ConversionPattern = PatternLayout.DetailConversionPattern;
layout.ActivateOptions();
// Create the appender
ConsoleAppender appender = new ConsoleAppender();
appender.Layout = layout;
appender.ActivateOptions();
InternalConfigure(repository, appender);
}
repository.ConfigurationMessages = configurationMessages;
return configurationMessages;
}
///
/// Initializes the using the specified appender.
///
/// The repository to configure.
/// The appender to use to log all logging events.
///
///
/// Initializes the using the specified appender.
///
///
public static ICollection Configure(ILoggerRepository repository, IAppender appender)
{
return Configure(repository, new IAppender[] { appender });
}
///
/// Initializes the using the specified appenders.
///
/// The repository to configure.
/// The appenders to use to log all logging events.
///
///
/// Initializes the using the specified appender.
///
///
public static ICollection Configure(ILoggerRepository repository, params IAppender[] appenders)
{
ArrayList configurationMessages = new ArrayList();
using (new LogLog.LogReceivedAdapter(configurationMessages))
{
InternalConfigure(repository, appenders);
}
repository.ConfigurationMessages = configurationMessages;
return configurationMessages;
}
private static void InternalConfigure(ILoggerRepository repository, params IAppender[] appenders)
{
IBasicRepositoryConfigurator configurableRepository = repository as IBasicRepositoryConfigurator;
if (configurableRepository != null)
{
configurableRepository.Configure(appenders);
}
else
{
LogLog.Warn(declaringType, "BasicConfigurator: Repository [" + repository + "] does not support the BasicConfigurator");
}
}
#endregion Public Static Methods
}
}