和netframework不一樣的是,netcore 沒有assembly文件。所以配置的時候需要注意:
1.配置文件可以沒有
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections>
這一段代碼。
2.要保證 LogManager.GetLogger(typeof(Respository));在
var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly()); XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
這兩句之后執行,可以加到startup的構造函數里,參考:
https://stackify.com/making-log4net-net-core-work/
3.記得在Startup類加入[assembly: XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]標記
其中2和3步驟是保證IsErrorEnabled等屬性不為false的關鍵