.netcore 日志使用Log4Net的时候,封装个帮助类,偶尔会有这样的异常。
“Repository [NETCoreRepository] is already defined. Repositories cannot be redefined.”
private static ILog log
{
get
{
try
{
if (_log == null)
{
Configure();
}
return _log;
}
catch (Exception ex)
{
return null;
}
}
}
public static void Configure(string repositoryName = "NETCoreRepository", string configFile = "Config/log4net.config")
{
try
{
repository = LogManager.CreateRepository(repositoryName);
XmlConfigurator.Configure(repository, new FileInfo(configFile));
_log = LogManager.GetLogger(repositoryName, repositoryName);
}
catch (LogException ex)
{
//没有数据
_log = LogManager.GetLogger(repositoryName, repositoryName);
}
catch (Exception ex2)
{
//出现异常
}
}
我猜是因为多个线程同时调用,执行了多遍repository = LogManager.CreateRepository(repositoryName);
,加个小锁试试