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

Log4Net

parent 707e93cb
This diff is collapsed.
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