gpt4 book ai didi

logging - Log4net 创建自定义级别

转载 作者:行者123 更新时间:2023-12-04 01:43:32 25 4
gpt4 key购买 nike

我知道可以在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com