WebAPI exception capture process, in conjunction with logging log4net (WebAPI frame)

A: Exception capturing process

  First, we need to distinguish between the type of controller. Is all grass-roots controller, or Apicontroller. (Of course, a general API framework, are used Apicontroller). Both are different exception handling.

 

 

 1.apicontroller webapi frame error handling:

Start by adding a new class in App_Start, the class inherits from ExceptionFilterAttribute

public  class WebApiExceptionFilterAttribute: ExceptionFilterAttribute 
{ 
      // override base class exception processing method 
        #region 
        public  the override  void the OnException (HttpActionExecutedContext actionExecutedContext) 
        { 
            

            // 2. returns to the caller specific abnormality information 
            IF (actionExecutedContext.Exception IS the NotImplementedException) 
            { 
                var oResponse = new new HttpResponseMessage (HttpStatusCode.NotImplemented);
                 // oResponse.Content StringContent object, new new = ( "method not supported"); 
                oResponse.Content = new newStringContent object, (actionExecutedContext.Exception.Message); 
                oResponse.ReasonPhrase = " This Func Not the Supported IS " ; 
                actionExecutedContext.Response = oResponse; 
            } 
            the else  IF (actionExecutedContext.Exception IS a TimeoutException) 
            { 
                actionExecutedContext.Response = new new HttpResponseMessage (HttpStatusCode.RequestTimeout); 
            } 
            // ..... where the client may be required to return to specific items according to the status code. If you can not find the corresponding exception, unified server returns an error 500 
            the else 
            { 
                //= new new HttpResponseMessage actionExecutedContext.Response (HttpStatusCode.InternalServerError); 


                // Comment out error message display, error display uniform 
                var the Message = JsonHelper.GetJsonString ( new new {Errorcode = HttpStatusCode.InternalServerError, Message = actionExecutedContext.Exception.Message});
                 // write the log 
                ILog log = LogManager.GetLogger ( typeof (GoodsController)); 
                log.error (the message, new new Exception ( " error abnormal " )); 

                // customize the error message in the message
                 //var Message = JsonHelper.GetJsonString (new {Errorcode = HttpStatusCode.InternalServerError, message = " interface call fails, check the incoming parameters are correct"}); 


                var oResponse = new new HttpResponseMessage (HttpStatusCode.InternalServerError) 
                { 
                  // the Content = new new StringContent object, (actionExecutedContext.Exception.Message), 
                  the Content = new new StringContent object, (the Message), 
                  reasonPhrase = " Error " 
                }; 
                actionExecutedContext.Response = oResponse; 
            } 

            Base .OnException (actionExecutedContext); 
        }
        #endregion 

}

Look at individual needs of a global catch errors or region capture.

Global: Global sentence plus years. GlobalConfiguration.Configuration.Filters.Add (new WebApiExceptionFilterAttribute ());

 

 

 Area: This ApiController or add a feature before the method name on OK.

 

 

 2. A successor controller or MVC that set (add Application_Error method in the Global)

 

 

 

protected void Application_Error(object sender, EventArgs e)
        {
            // 在出现未处理的错误时运行的代码
            HttpContext ctx = HttpContext.Current;
            Exception exception = ctx.Server.GetLastError();
            Exception exception_s = Server.GetLastError();
            HttpException ex = exception_s as HttpException;
            if (exception != null)
            {
                string m = exception.Message;
                string errorInfo = "URL:<strong>" + ctx.Request.RawUrl.ToString() + "<strong><br/>Source:<strong>" + exception.Source
                                   + "<strong><br/>Message:<strong>" + exception.Message + "<>";
                if (!m.Contains("不存在"))
                {
                    if (exception.InnerException != null)
                        errorInfo += "<br/>错误信息为:<strong>" + exception.InnerException.Message + "<>";
                }
                if (exception is HttpException)
                {
                    HttpException ex2 = exception as HttpException;
                    int httpCode = ex2.GetHttpCode();
                    errorInfo += "<br />Code:<strong>" + httpCode.ToString() + "<>";
                }

                //写日志
                ILog log = LogManager.GetLogger(typeof(GoodsController)); 
                log.Error(errorInfo, new Exception("Error异常")); ctx.Items.Add("LastError", errorInfo); ctx.Server.ClearError(); 
}

try { //写逻辑 ctx.Response.Redirect("/Home/Index"); } catch { }
}

 

二:log4net日志管理。配合WebApi框架,个人感觉十分方便

打开NuGet,添加log4net。成功之后,可以直接处理

使用方法:

 [HttpGet,Route("LogTest")]
        public IHttpActionResult LogTest()
        {
            ILog log = LogManager.GetLogger(typeof(GoodsController)); // 下面是日志处理
            log.Debug("测试debug", new Exception("debug异常"));
            log.Info("测试Info", new Exception("Info异常"));
            log.Warn("测试Warn", new Exception("Warn异常"));

            log.Error("测试Error", new Exception("Error异常"));
            log.Fatal("测试Fatal", new Exception("Fatal异常"));
            return Json(new {result = "Complent"});
        }

log4net需要配置文件,才能输出。添加Log4net.config文件。然后在程序集AssemblyInfo.cs中,添加 [assembly: XmlConfigurator(Watch = true, ConfigFile = "Log4Net.config")]

 

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>


  <log4net>
    <!--记录所有的完整日志-->
    <appender name="AllLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--或者是文件名或是文件夹(没有后缀)Gets or sets the path to the file that logging will be written to.,-->
      <file value="log/all/log_" />
      <!--是否总是写在一个文件里Gets or sets a value indicating whether to always log to the same file.-->
      <staticLogFileName value="false" />
      <!--Gets or sets a flag that indicates whether the file should be appended to or overwritten.-->
      <appendToFile value="true" />
      <!--可设置为Size、Date,即大小/日期超出一定范围后就新建一个日志文件-->
      <rollingStyle value="Date" />
      <!--一天最多保存多少Gets or sets the maximum number of backup files that are kept before the oldest is erased.-->
      <maxSizeRollBackups value="10" />
      <!--每个文件最大大小,单位可是MB,KBGets or sets the maximum size that the output file is allowed to reach before being rolled over to backup files.-->
      <maximumFileSize value="5MB" />
      <!--设置用来生产文件的日期格式Gets or sets the date pattern to be used for generating file names when rolling over on date.-->
      <datePattern value="yyyy-MM-dd'.log'"/>
      <!--日志输入的通用格式(日志的内容格式)-->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter,log4net">
        <!--级别由低往高依次是
        ALL
        DEBUG
        INFO
        WARN
        ERROR
        FATAL
        None-->
        <levelMin value="DEBUG" />
        <levelMax value="Warn" />
      </filter>
    </appender>

    <!--记录错误日志,这些错误往往是一个程序bug或是要注意的-->
    <appender name="ErrorLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--或者是文件名或是文件夹(没有后缀)Gets or sets the path to the file that logging will be written to.,-->
      <file value="log/error/error_" />
      <!--是否总是写在一个文件里Gets or sets a value indicating whether to always log to the same file.-->
      <staticLogFileName value="false" />
      <!--Gets or sets a flag that indicates whether the file should be appended to or overwritten.-->
      <appendToFile value="true" />
      <!--可设置为Size、Date,即大小/日期超出一定范围后就新建一个日志文件-->
      <rollingStyle value="Date" />
      <!--一天最多保存多少Gets or sets the maximum number of backup files that are kept before the oldest is erased.-->
      <maxSizeRollBackups value="10" />
      <!--每个文件最大大小,单位可是MB,KBGets or sets the maximum size that the output file is allowed to reach before being rolled over to backup files.-->
      <maximumFileSize value="5MB" />
      <!--设置用来生产文件的日期格式Gets or sets the date pattern to be used for generating file names when rolling over on date.-->
      <datePattern value="yyyy-MM-dd'.log'"/>
      <!--日志输入的通用格式(日志的内容格式)-->
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter,log4net">
        <levelMin value="ERROR" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <!--Set root logger level to DEBUG and its only appender to A1-->
    <root>
      <!--控制级别,由低到高: ALL|DEBUG|INFO|WARN|ERROR|FATAL|OFF-->
      <level value="ALL" />
      <appender-ref ref="AllLogFileAppender" />
      <appender-ref ref="ErrorLogFileAppender" />
    </root>
  </log4net>
  <system.web>
      <compilation debug="true" targetFramework="4.5.2" />
      <httpRuntime targetFramework="4.5.2" />
    </system.web>

</configuration>

 上面只是写了一个,测试方法而已。WebAPI实际使用本文全局错误处理 + log4net,日志模块简直起飞,全部搞定,当甩手掌柜吧。

 

Guess you like

Origin www.cnblogs.com/cr-cool/p/12073052.html