在Apache Log4net日志记录实用程序中,%level
模式输出记录器的日志记录级别,如下所示:如果我调用log.Warn("")
,则输出为Warn
。有没有办法改变输出文本。例如,log.Info("")
输出Information
而不是INFO
。
答案 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实例上使用我的新“Auth”日志记录级别,如下所示:
SecurityLogger.AuthFormat("User logged in with id {0} from IP address {1}", id, Request.UserHostAddress);