The global filter is used in asp.net mvc to capture exceptions. asp. netmvc

Source: Internet
Author: User

The global filter is used in asp.net mvc to capture exceptions. asp. netmvc

Using SuperManCore; using System. web; using System. web. mvc; namespace SuperMan {public class FilterConfig {// <summary> // register the global filter add by caoheyang 20150205 /// </summary> /// <param name = "filters"> </param> public static void RegisterGlobalFilters (GlobalFilterCollection filters) {filters. add (new webhandleerrorattriters (), 1); filters. add (new HandleErrorAttribute (), 2 );}} /// <summary> /// add by caoheyang 20150205 // </summary> public class WebHandleErrorAttribute: handleErrorAttribute {// <summary> // rewrite the Exception Handling Method add by caoheyang 20150205 /// </summary> /// <param name = "filterContext"> context object class inherited from ControllerContext </param> public override void OnException (ExceptionContext filterContext) {LogHelper. logWriterFromFilter (filterContext. exception );}}}
LogHelper source code:
Public class LogHelper {public static Logger logger = LogManager. getLogger ("SuperManCore. logHelper "); /// <summary> /// remarks log /// </summary> /// <param name = "dec"> parameters to be captured (must be attribute classes) </param> // <param name = "rmark"> description operation </param> public new static void LogWriter (string rmark = "", object dec = null) {try {string logstr = "\ r \ n ----------------- start ---------------------- \ r \ n"; MethodBase m = new StackTrace (). getFrame (1 ). getMethod (); ParameterInfo [] pm = m. getParameters (); string classname = m. declaringType. toString (); string propertyName = m. name; logstr = logstr + "Remarks:" + rmark + "\ r \ n"; // write class Name logstr = logstr + "function class Name: "+ classname +" \ r \ n "; // write function method logstr = logstr +" function name: "+ propertyName +" \ r \ n "; for (int I = 0; I <pm. length; I ++) {logstr = logstr + "function parameters include:" + pm [I]. name. toString () + "\ r \ n";} JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); logstr = logstr + "function parameter value:" + jsonSerializer. serialize (dec) + "\ r \ n"; logstr + = "-------------------- end --------------------- \ r \ n"; // write the log logger. info (logstr) ;}catch (Exception) {throw ;}} /// <summary> /// exception capture log /// </summary> /// <param name = "ex"> exception object (must be an Attribute Class) </param> // <param name = "rmark"> brief description </param> public new static void LogWriter (Exception ex, string rmark = "") {try {string logstr = "\ r \ n ----------------- start ---------------------- \ r \ n"; MethodBase m = new StackTrace (). getFrame (1 ). getMethod (); ParameterInfo [] pm = m. getParameters (); string classname = m. declaringType. toString (); string propertyName = m. name; logstr = logstr + "Remarks:" + rmark + "\ r \ n"; // write class Name logstr = logstr + "function class Name: "+ classname +" \ r \ n "; // write function method logstr = logstr +" function name: "+ propertyName +" \ r \ n "; for (int I = 0; I <pm. length; I ++) {logstr = logstr + "function parameters include:" + pm [I]. name. toString () + "\ r \ n";} logstr = logstr + "function exception:" + ex. toString () + "\ r \ n"; logstr + = "-------------------- end --------------------- \ r \ n"; // write a log logger. info (logstr) ;}catch (Exception) {throw ;}} /// <summary> /// logs with parameter exceptions /// </summary> /// <param name = "dec"> exception parameter object (must be an Attribute Class) </param> // <param name = "ex"> log Exception object </param> public new static void LogWriter (object dec, Exception ex) {try {string logstr = "\ r \ n ----------------- start ---------------------- \ r \ n"; MethodBase m = new StackTrace (). getFrame (1 ). getMethod (); ParameterInfo [] pm = m. getParameters (); string classname = m. declaringType. toString (); string propertyName = m. name; // write class Name logstr = logstr + "function class Name:" + classname + "\ r \ n "; // function Writing Method logstr = logstr + "function name:" + propertyName + "\ r \ n"; for (int I = 0; I <pm. length; I ++) {logstr = logstr + "function parameters include:" + pm [I]. name. toString () + "\ r \ n";} JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); logstr = logstr + "function parameter value:" + jsonSerializer. serialize (dec) + "\ r \ n"; logstr = logstr + "function exception:" + ex. toString () + "\ r \ n"; logstr + = "-------------------- end --------------------- \ r \ n"; // write a log logger. info (logstr) ;}catch (Exception) {throw ;}} /// <summary> /// exception Log Capture with parameter description /// </summary> /// <param name = "dec"> parameters to be captured (must be attribute Class) </param> /// <param name = "ex"> exception object </param> /// <param name = "rmark"> description </param> public new static void LogWriter (object dec, exception ex, string rmark) {try {string logstr = "\ r \ n ----------------- start ---------------------- \ r \ n"; MethodBase m = new StackTrace (). getFrame (1 ). getMethod (); ParameterInfo [] pm = m. getParameters (); string classname = m. declaringType. toString (); string propertyName = m. name; // write remarks logstr = logstr + "Remarks:" + rmark + "\ r \ n"; // write class Name logstr = logstr + "function class Name: "+ classname +" \ r \ n "; // write function method logstr = logstr +" function name: "+ propertyName +" \ r \ n "; for (int I = 0; I <pm. length; I ++) {logstr = logstr + "function parameters include:" + pm [I]. name. toString () + "\ r \ n";} JavaScriptSerializer jsonSerializer = new JavaScriptSerializer (); logstr = logstr + "function parameter value:" + jsonSerializer. serialize (dec) + "\ r \ n"; logstr = logstr + "function exception:" + ex. toString () + "\ r \ n"; logstr + = "-------------------- end --------------------- \ r \ n"; // write a log logger. info (logstr) ;}catch (Exception) {throw ;}} /// <summary> /// capture global exceptions /// </summary> /// <param name = "error"> </param> public new static void LogWriterFromFilter (exception error) {try {string logstr = "\ r \ n ----------------- start ---------------------- \ r \ n"; // The logstr = logstr + "exception address:" + HttpContext. current. request. url. absoluteUri. toString () + "\ r \ n"; logstr = logstr + "request type:" + HttpContext. current. request. requestType. toString () + "\ r \ n"; logstr = logstr + "exception:" + error. toString () + "\ r \ n"; logstr + = "-------------------- end --------------------- \ r \ n"; // send an email if (ConfigSettings. instance. isSendMail = "true") {string emailToAddress = ConfigSettings. instance. emailToAdress; EmailHelper. sendEmailTo (logstr, emailToAddress);} // write the log logger. info (logstr) ;}catch (Exception) {throw ;}}}

Note: The class library where LogHelper is located must reference the NLog. dll file.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.