Asp.net mvc 4 使用log4net登录Asp.net MVC应用程序
Mine是一个Asp.NETMVC4应用程序。我需要实现以下几点:; 根据web.config文件中设置的日志级别(例如1/2/3/4) 我必须记录: 1:Asp.net mvc 4 使用log4net登录Asp.net MVC应用程序,asp.net-mvc-4,logging,log4net,error-logging,global-filter,Asp.net Mvc 4,Logging,Log4net,Error Logging,Global Filter,Mine是一个Asp.NETMVC4应用程序。我需要实现以下几点:; 根据web.config文件中设置的日志级别(例如1/2/3/4) 我必须记录: 1: 方法开始 方法结束 应用程序警告 应用程序异常 2: 应用程序警告 应用程序例外 3: 应用程序例外 4: 没有 此外,我需要使用全局过滤器来实现这一点。 请给我一些建议 提前谢谢 已解决 以下是我的步骤 我创建了以下过滤器类 [AttributeUsage(AttributeTargets.Class | Attribu
- 方法开始
- 方法结束
- 应用程序警告
- 应用程序警告
- 应用程序例外
- 应用程序例外
- 没有
已解决 以下是我的步骤 我创建了以下过滤器类
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class LoggerFilterAttribute : ActionFilterAttribute
{
private ILog _objLog4Net = null;
string logLevel = string.Empty;
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
_objLog4Net = LogManager.GetLogger(filterContext.RouteData.Values["controller"].ToString());
logLevel = GetLogLevel();
if (logLevel == "1")
{
_objLog4Net.Debug(string.Concat("Entered ", filterContext.Controller.GetType().ToString(), "'s ", filterContext.ActionDescriptor.ActionName, " method"));
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
_objLog4Net = LogManager.GetLogger(filterContext.RouteData.Values["controller"].ToString());
logLevel = GetLogLevel();
if (logLevel == "1")
{
_objLog4Net.Debug(string.Concat("Existing ", filterContext.Controller.GetType().ToString(), "'s ", filterContext.ActionDescriptor.ActionName, " method"));
}
}
private string GetLogLevel()
{
return ConfigurationManager.AppSettings["LOGLEVEL"].ToLower().ToString();
}
}
如果通过1找到,则此类将检查配置文件中的日志级别
OnActionExecuting()
和OnActionExecuted()
将记录方法开始和方法结束
下面是用于记录异常的ExceptionFilter类
public class ExceptionFilterAttribute : HandleErrorAttribute
{
private ILog _objLog4Net = null;
string logLevel = string.Empty;
public override void OnException(ExceptionContext filterContext)
{
_objLog4Net = LogManager.GetLogger(filterContext.RouteData.Values["controller"].ToString());
logLevel = GetLogLevel();
if (logLevel == "1" || logLevel == "2")
{
_objLog4Net.Error(filterContext.Exception.Message, filterContext.Exception);
}
if (logLevel == "3")
{
if (filterContext.Exception.GetType().IsSubclassOf(typeof(ApplicationException)))
{
_objLog4Net.Error(filterContext.Exception.Message, filterContext.Exception);
}
}
}
private string GetLogLevel()
{
return ConfigurationManager.AppSettings["LOGLEVEL1"].ToLower().ToString();
}
}
此类根据日志级别记录所有异常或仅记录应用程序异常
如果Loglevel为none,则无条件变为true/未记录任何内容。这些类型的问题将被否决,您很快就会看到。。。如果您想知道这是为什么,您共享了可能是唯一的需求,但是您没有共享您所花费的分析工作。因此,您提出的问题属于一般类别。我认识到作为这个网站的新成员,需要一些时间才能得到这个。所以我这边的一点帮助是
您好,我已经为异常记录实现了一个全局过滤器(继承自HandleErrorAttribute),它工作正常(记录异常)。问题在于通过全局过滤器记录Logger.Info()/Warn()Debug()。需要通过GloabalFilter在这方面提供帮助吗?建议参考log4net的标准用法。我怀疑它与log4net毫无关系。理想情况下,我们设置一个用于日志记录的记录器,该记录器应该可以从任何需要日志记录功能的组件访问。对于402和500,我使用MagicalUnicornCustomErrorHandlingNuget包。请您添加更多关于您正试图实现的目标的信息(您可以更新原始问题以添加更多信息)。