2009-07-19 92 views

回答

0

現在已經有幾年了,但this線程討論了您要做的事情。

+3

鏈接死的,但可用的[互聯網檔案館] (https://web.archive.org/web/20130528024223/http://www.l4ndash.com/Log4NetMailArchive/tabid/70/forumid/1/postid/14714/view/topic/Default.aspx) – stuartd 2014-08-18 10:55:47

+0

And a幾年後...兩個鏈接都死了。耶爲鏈接腐! – WernerCD 2017-11-29 14:43:17

13

這可與一種擴展方法來實現所列出的位置: http://rageshkrishna.com/2011/01/21/AddingCustomLogLevelsToLog4net.aspx

添加一些擴展方法使得開始使用 新的日誌級別變得非常簡單:

public static class SecurityExtensions 
{ 
    static readonly log4net.Core.Level authLevel = new log4net.Core.Level(50000, "Auth"); 

    public static void Auth(this ILog log, string message) 
    { 
     log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
      authLevel, message, null); 
    } 

    public static void AuthFormat(this ILog log, string message, params object[] args) 
    { 
     string formattedMessage = string.Format(message, args); 
     log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, 
      authLevel, formattedMessage, null); 
    } 

} 

這就是它 - 現在我可以開始使用我的新的「驗證」日誌級別 這樣的ILog的任何實例:

SecurityLogger.AuthFormat("User logged in with id {0} from IP address {1}", id, Request.UserHostAddress);