Apache log4net 更改日志级别文本

Apache log4net changing logging level text

在 Apache Log4net 日志实用程序中,%level 模式输出记录器的日志级别,如:如果我调用 log.Warn(""),输出为 Warn。有什么办法可以更改输出文本。例如,log.Info("") 输出 Information 而不是 INFO

您可以做的一件事是 create a custom log event, 并在必要时修改您的 PatternLayout 格式。

用法示例(来自link):

The first thing you need to do is to create and register your new levels with the LogManager like this:

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.

Adding some extension methods makes it dead simple to start using the new log levels:

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

}

And that’s it – now I can start using my new “Auth” logging level on any instance of ILog like this:

SecurityLogger.AuthFormat("User logged in with id {0} from IP address {1}", id, Request.UserHostAddress);