Commit 02b7f3a3 authored by 阿星Plus's avatar 阿星Plus

Log4Net

parent 707e93cb
using log4net;
using log4net.Core;
using log4net.Util;
using System;
using System.Globalization;
using ILogger = Castle.Core.Logging.ILogger;
namespace Plus.Log4Net
{
/// <summary>
/// Log4NetLogger
/// </summary>
[Serializable]
public class Log4NetLogger : MarshalByRefObject, ILogger
{
private static readonly Type DeclaringType = typeof(Log4NetLogger);
public Log4NetLogger(log4net.Core.ILogger logger, Log4NetLoggerFactory factory)
{
Logger = logger;
Factory = factory;
}
internal Log4NetLogger()
{
}
internal Log4NetLogger(ILog log, Log4NetLoggerFactory factory)
: this(log.Logger, factory)
{
}
public bool IsDebugEnabled
{
get { return Logger.IsEnabledFor(Level.Debug); }
}
public bool IsErrorEnabled
{
get { return Logger.IsEnabledFor(Level.Error); }
}
public bool IsFatalEnabled
{
get { return Logger.IsEnabledFor(Level.Fatal); }
}
public bool IsInfoEnabled
{
get { return Logger.IsEnabledFor(Level.Info); }
}
public bool IsWarnEnabled
{
get { return Logger.IsEnabledFor(Level.Warn); }
}
protected internal Log4NetLoggerFactory Factory { get; set; }
protected internal log4net.Core.ILogger Logger { get; set; }
public bool IsTraceEnabled => throw new NotImplementedException();
public override string ToString()
{
return Logger.ToString();
}
public virtual global::Castle.Core.Logging.ILogger CreateChildLogger(string name)
{
return Factory.Create(Logger.Name + "." + name);
}
public void Debug(string message)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, message, null);
}
}
public void Debug(Func<string> messageFactory)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, messageFactory.Invoke(), null);
}
}
public void Debug(string message, Exception exception)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, message, exception);
}
}
public void DebugFormat(string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void DebugFormat(Exception exception, string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void DebugFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void DebugFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Debug, new SystemStringFormat(formatProvider, format, args), exception);
}
}
public void Error(string message)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, message, null);
}
}
public void Error(Func<string> messageFactory)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, messageFactory.Invoke(), null);
}
}
public void Error(string message, Exception exception)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, message, exception);
}
}
public void ErrorFormat(string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void ErrorFormat(Exception exception, string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void ErrorFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void ErrorFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsErrorEnabled)
{
Logger.Log(DeclaringType, Level.Error, new SystemStringFormat(formatProvider, format, args), exception);
}
}
public void Fatal(string message)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, message, null);
}
}
public void Fatal(Func<string> messageFactory)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, messageFactory.Invoke(), null);
}
}
public void Fatal(string message, Exception exception)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, message, exception);
}
}
public void FatalFormat(string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void FatalFormat(Exception exception, string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void FatalFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void FatalFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsFatalEnabled)
{
Logger.Log(DeclaringType, Level.Fatal, new SystemStringFormat(formatProvider, format, args), exception);
}
}
public void Info(string message)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, message, null);
}
}
public void Info(Func<string> messageFactory)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, messageFactory.Invoke(), null);
}
}
public void Info(string message, Exception exception)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, message, exception);
}
}
public void InfoFormat(string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void InfoFormat(Exception exception, string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void InfoFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void InfoFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsInfoEnabled)
{
Logger.Log(DeclaringType, Level.Info, new SystemStringFormat(formatProvider, format, args), exception);
}
}
public void Trace(string message)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Trace, message, null);
}
}
public void Trace(Func<string> messageFactory)
{
if (IsDebugEnabled)
{
Logger.Log(DeclaringType, Level.Trace, messageFactory.Invoke(), null);
}
}
public void Trace(string message, Exception exception)
{
if (IsTraceEnabled)
{
Logger.Log(DeclaringType, Level.Trace, message, exception);
}
}
public void TraceFormat(string format, params object[] args)
{
if (IsTraceEnabled)
{
Logger.Log(DeclaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void TraceFormat(Exception exception, string format, params object[] args)
{
if (IsTraceEnabled)
{
Logger.Log(DeclaringType, Level.Trace, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void TraceFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsTraceEnabled)
{
Logger.Log(DeclaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void TraceFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsTraceEnabled)
{
Logger.Log(DeclaringType, Level.Trace, new SystemStringFormat(formatProvider, format, args), exception);
}
}
public void Warn(string message)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, message, null);
}
}
public void Warn(Func<string> messageFactory)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, messageFactory.Invoke(), null);
}
}
public void Warn(string message, Exception exception)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, message, exception);
}
}
public void WarnFormat(string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), null);
}
}
public void WarnFormat(Exception exception, string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, new SystemStringFormat(CultureInfo.InvariantCulture, format, args), exception);
}
}
public void WarnFormat(IFormatProvider formatProvider, string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, new SystemStringFormat(formatProvider, format, args), null);
}
}
public void WarnFormat(Exception exception, IFormatProvider formatProvider, string format, params object[] args)
{
if (IsWarnEnabled)
{
Logger.Log(DeclaringType, Level.Warn, new SystemStringFormat(formatProvider, format, args), exception);
}
}
}
}
\ No newline at end of file
using Castle.Core.Logging;
using log4net;
using log4net.Config;
using log4net.Repository;
using System;
using System.IO;
using System.Xml;
namespace Plus.Log4Net
{
/// <summary>
/// Log4NetLoggerFactory
/// </summary>
public class Log4NetLoggerFactory : AbstractLoggerFactory
{
internal const string DefaultConfigFileName = "log4net.config";
private readonly ILoggerRepository _loggerRepository;
public Log4NetLoggerFactory()
: this(DefaultConfigFileName)
{
}
public Log4NetLoggerFactory(string configFileName)
{
_loggerRepository = LogManager.CreateRepository(
typeof(Log4NetLoggerFactory).GetAssembly(),
typeof(log4net.Repository.Hierarchy.Hierarchy)
);
var log4NetConfig = new XmlDocument();
log4NetConfig.Load(File.OpenRead(configFileName));
XmlConfigurator.Configure(_loggerRepository, log4NetConfig["log4net"]);
}
public Log4NetLoggerFactory(string configFileName, bool reloadOnChange)
{
_loggerRepository = LogManager.CreateRepository(
typeof(Log4NetLoggerFactory).GetAssembly(),
typeof(log4net.Repository.Hierarchy.Hierarchy)
);
if (reloadOnChange)
{
XmlConfigurator.ConfigureAndWatch(_loggerRepository, new FileInfo(configFileName));
}
else
{
var log4NetConfig = new XmlDocument();
log4NetConfig.Load(File.OpenRead(configFileName));
XmlConfigurator.Configure(_loggerRepository, log4NetConfig["log4net"]);
}
}
public override ILogger Create(string name)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
}
return new Log4NetLogger(LogManager.GetLogger(_loggerRepository.Name, name), this);
}
public override ILogger Create(string name, LoggerLevel level)
{
throw new NotSupportedException("Logger levels cannot be set at runtime. Please review your configuration file.");
}
}
}
\ No newline at end of file
using Castle.Facilities.Logging;
namespace Plus.Log4Net
{
/// <summary>
/// LoggingFacilityExtensions
/// </summary>
public static class LoggingFacilityExtensions
{
public static LoggingFacility UseLog4Net(this LoggingFacility loggingFacility)
{
return loggingFacility.LogUsing<Log4NetLoggerFactory>();
}
}
}
\ No newline at end of file
......@@ -4,4 +4,13 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Castle.LoggingFacility" Version="5.0.0" />
<PackageReference Include="log4net" Version="2.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Plus\Plus.csproj" />
</ItemGroup>
</Project>
using Plus.Modules;
namespace Plus.Log4Net
{
/// <summary>
/// Castle Log4Net module
/// </summary>
[DependsOn(typeof(PlusLeadershipModule))]
public class PlusCastleLog4NetModule
{
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment