我正在编写自定义Log4Net附加程序,以使用以下配置在.Net Core 2 API中调用WCF服务。我无法多次调用或调用自定义附加程序。
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
ILoggerRepository repository = log4net.LogManager.GetRepository(Assembly.GetCallingAssembly());
log4net.Config.XmlConfigurator.Configure(repository, new System.IO.FileInfo("log4net.config"));
loggerFactory.AddLog4Net();
app.UseMvc();
}
自定义Appender
public class Log4NetWCFAppender : AppenderSkeleton
{
private readonly ICustomLog _customLog;
public Log4NetWCFAppender (ICustomLog customLog) { _customLog = customLog; }
public Log4NetWCFAppender() : this(new CustomLogger())// TODO: Use DI
{ }
protected override void Append(LoggingEvent loggingEvent)
{
_customLog.LogInformation("Event started at" + loggingEvent.StartTime);
}
API控制器
public class ValuesController : ControllerBase
{
private ILogger<ValuesController> _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
[HttpGet]
public ActionResult Get()
{
_logger.LogInformation("test");
return null;
}
log4net.config位于项目的根目录
<?xml version="1.0" encoding="utf-8"?>
<log4net debug="true">
<root>
<level value="ALL" />
<appender-ref ref="AuditAppender" />
</root>
<appender name="AuditAppender" type="Logger.Log4NetWCFAppender,Logger">
</appender>
</log4net>