Mat*_*ock 20
这可以使用此处概述的扩展方法完成: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);
}
}
Run Code Online (Sandbox Code Playgroud)
就是这样 - 现在我可以开始在任何ILog实例上使用我的新"Auth"日志记录级别,如下所示:
SecurityLogger.AuthFormat("User logged in with id {0} from IP address {1}", id, Request.UserHostAddress);
Run Code Online (Sandbox Code Playgroud)