您可以做的一件事是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);