logging - Log4net 创建自定义级别

标签 logging log4net

我知道可以在 log4net 中使用内置级别
信息、警告、错误和致命消息

是否可以创建新的?

最佳答案

这可以通过这里概述的扩展方法来完成:
http://rageshkrishna.com/2011/01/21/AddingCustomLogLevelsToLog4net.aspx

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

关于logging - Log4net 创建自定义级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1150999/

相关文章:

java - 限制 log4j2 中汇总日志文件的最大数量

logging - 如何从正在运行的 Docker 容器中读取文件和标准输出

android - 在 Android 上记录用户操作

vb.net - log4net smtpappender 自定义电子邮件收件人

sql-server - 使用 log4net 记录到 XML 列

performance - 记录用户事件 IP 地址的成本

logging - VBScript 是否有等效的 log4j?

c# - 使用 log4net 时记录异常的最佳方式是什么

c# - 是否需要创建 log4net 实例/对象?

c#-4.0 - Log4Net 与温莎城堡