[C#] 在Abstract class 中設定logger

為了不用重覆define variables, 在 OO 中通常會將common property / method 放到abstract class 中, 再讓其他child class 繼承. 而在log4net 中, 要設定其logger, 若用回之前的設定, 便會指向其parent class.

若要解決的話, 其實只須叫用logger 時改用GetType()便可. 例如:

public abstract class BaseController
    {
        private ILog _log;
        /// <summary>
        /// Logger defined in base class for troubleshooting.
        /// </summary>
        protected ILog _logger
        {
            get
            {
                if (_log == null)
                {
                    _log = LogManager.GetLogger(this.GetType());
                }
                return _log;
            }
        }
    }

 

About C.H. Ling 260 Articles
a .net / Java developer from Hong Kong and currently located in United Kingdom. Thanks for Google because it solve many technical problems so I build this blog as return. Besides coding and trying advance technology, hiking and traveling is other favorite to me, so I will write down something what I see and what I feel during it. Happy reading!!!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.