描述
log4net配置正確,但是沒有日誌輸出。
示例代碼:
ILog log = LogManager.GetLogger(this.GetType());
log.Warn("bussinessException"); // 沒有輸出
分析
log4net配置文件所在Assembly 與 LogManager.GetLogger 方法指定的Assembly 不一致。LogManager.GetLogger方法有多個重載,一些重載的方法內部是通過Assembly.GetCallingAssembly()獲取的Assembly ,假如你的獲取ILog 實例的代碼與日誌輸出的代碼不在同一個程序集時可能出現該問題。
解決方式
給GetLogger方法指定正確的Assembly
實例代碼:
ILog log = LogManager.GetLogger(this.GetType().Assembly, this.GetType());
log.Warn("bussinessException"); // 搞定