[英]Apache log4net changing logging level text
在Apache Log4net日志记录实用程序中, %level
模式输出记录器的记录级别,例如:如果我调用log.Warn("")
,则输出为Warn
。 有什么办法可以更改输出文本。 例如, log.Info("")
输出Information
而不是INFO
。
您可以做的一件事是创建一个自定义日志事件,并在必要时修改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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.