Note that there are some explanatory texts on larger screens.

plurals
  1. POLogging in Asp.net MVC application using log4net
    primarykey
    data
    text
    <p>Mine is a Asp.net MVC 4 application. I need to implement the following; As per log level set in web.config file (eg. 1/2/3/4)</p> <p>I have to log:</p> <p>1 :</p> <ul> <li>Methods start</li> <li>Methods end</li> <li>Application Warning</li> </ul> <p>Application Exception</p> <p>2:</p> <ul> <li>Application Warnings</li> <li>Application Exceptions</li> </ul> <p>3:</p> <ul> <li>Application Exceptions</li> </ul> <p>4:</p> <ul> <li>None</li> </ul> <p><strong>Further I need to achieve this using global filter(s).</strong></p> <p>kindly provide me some pointers.</p> <p>Thanks in advance.</p> <hr> <p><strong>Resolved</strong></p> <p>Following are my steps</p> <p>I created the following Filter class</p> <pre><code>[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(); } } </code></pre> <p>This class will check for loglevel in configuration file if found 1 Through OnActionExecuting() and OnActionExecuted() Will log for Method Start and Method End.</p> <p>Further following is the ExceptionFilter class for logging exception</p> <pre><code>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(); } } </code></pre> <p>This class as per log level, Logs All Exceptions or only Application exceptions.</p> <p>If Loglevel is none no conditions get true / nothing is logged.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload