2017-03-08 8 views
0

Apache Log4netロギングユーティリティでは、%levelパターンはロガーのロギングレベルを出力します。log.Warn("")を呼び出すと、出力はWarnになります。出力テキストを変更する方法はありますか?たとえば、log.Info("")INFOの代わりにInformationを出力します。Apache log4netロギングレベルテキストの変更

+0

パターン(メソッド)をチェックをオーバーライドできるか否か –

答えて

1

あなたができることは、create a custom log event,であり、必要に応じてPatternLayoutフォーマットを変更してください。 (リンクから)

使用例:

最初にやるべきことは、このようなLogManagerので、あなたの新しいレベルを作成し、登録することである。

log4net.Core.Level authLevel = new log4net.Core.Level(50000, "Auth"); 
log4net.LogManager.GetRepository().LevelMap.Add(authLevel); 
It’s important that you do this before configuring log4net. 

いくつかの拡張方法を追加すると、新しいログレベルを使い始めるのが簡単にできなくなります。

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); 
関連する問題