2017-03-08 86 views
0

在Apache Log4net日志记录实用程序中,%level模式会输出日志记录级别,例如:如果我呼叫log.Warn(""),则输出为Warn。有没有办法改变输出文字。例如,log.Info("")输出Information而不是INFOApache log4net更改日志记录级别文本

+0

检查模式(方法)可被覆盖或不 –

回答

1

你可以做的一件事是create a custom log event,,如果需要修改你的PatternLayout格式。

用法示例(从链接):

你需要做的第一件事是与日志管理这样的创建和注册新的水平:

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);