Jelajahi Sumber

2020/04/24 Jessica
Actions
1. 更改Release 設定,Release前一律刪除Publish資料
2. 紀錄 Global Exception in APILog

Jessica.Tseng 4 tahun lalu
induk
melakukan
3e144f3c4e

+ 43 - 22
EVCB_OCPP.WEBAPI/Handlers/APILogHandler.cs

@@ -42,35 +42,56 @@ namespace EVCB_OCPP.WEBAPI.Handlers
 
                 return await base.SendAsync(request, cancellationToken)
                     .ContinueWith(task =>
-                    {                      
-
-                        var response = task.Result;
-
-                        if (response.Content != null)
+                    {
+                     
+                        if(task.IsFaulted)
                         {
-                            apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result;
-                            apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType;
-                            apiLogEntry.ResponseHeaders = WebApiHelper.SerializeHeaders(response.Content.Headers);
-                        }
+                            var result = new CPOOuterResponse() { StatusCode = (int)CPO_StatusCode.UnexpectedError, StatusMessage = CPO_StatusMessage.ERROR_MSG_Global_UNEXPECTEDERROR };
 
-                        int errorMessageIndex = apiLogEntry.ResponseContentBody.IndexOf(",\"ErrorDetail\"");
-                        string endTag = apiLogEntry.ResponseContentBody.Last().ToString();
+                            apiLogEntry.ResponseContentBody = JsonConvert.SerializeObject(result, EVCBConfiguration.JSONSERIALIZER_FORMAT);
+                            apiLogEntry.ResponseContentType = "";
+                            apiLogEntry.ResponseHeaders = "";
 
-                        if (errorMessageIndex > 0)
-                        {
-                            string errorMsg = apiLogEntry.ResponseContentBody.Substring(errorMessageIndex).Remove(0, 16);
-                            apiLogEntry.ResponseContentBody = apiLogEntry.ResponseContentBody.Remove(errorMessageIndex).Remove(0, 1);
                             apiLogEntry.ErrorOn = DateTime.Now;
-                            apiLogEntry.ErrorMsg = errorMsg.Remove(errorMsg.Count() - 2).Replace("'"," ");
-                           
-                            response = request.CreateResponse(response.StatusCode, JsonConvert.DeserializeObject("{" + apiLogEntry.ResponseContentBody + "}"));
+                            apiLogEntry.ErrorMsg = task.Exception.ToString().Replace("'", " ");
+
+                            apiLogEntry.ResponseStatusCode = (int) HttpStatusCode.InternalServerError;
+                            apiLogEntry.ResponseTimestamp = DateTime.Now;
+                            WriteLog(apiLogEntry);
+                            return request.CreateResponse(HttpStatusCode.InternalServerError, result);
                         }
+                        else
+                        {
+                            var response = task.Result;
+
+                            if (response.Content != null)
+                            {
+                                apiLogEntry.ResponseContentBody = response.Content.ReadAsStringAsync().Result;
+                                apiLogEntry.ResponseContentType = response.Content.Headers.ContentType.MediaType;
+                                apiLogEntry.ResponseHeaders = WebApiHelper.SerializeHeaders(response.Content.Headers);
+                            }
+
+                            int errorMessageIndex = apiLogEntry.ResponseContentBody.IndexOf(",\"ErrorDetail\"");
+                            string endTag = apiLogEntry.ResponseContentBody.Last().ToString();
 
-                        apiLogEntry.ResponseStatusCode = (int)response.StatusCode;
-                        apiLogEntry.ResponseTimestamp = DateTime.Now;
-                        WriteLog(apiLogEntry);
+                            if (errorMessageIndex > 0)
+                            {
+                                string errorMsg = apiLogEntry.ResponseContentBody.Substring(errorMessageIndex).Remove(0, 16);
+                                apiLogEntry.ResponseContentBody = apiLogEntry.ResponseContentBody.Remove(errorMessageIndex).Remove(0, 1);
+                                apiLogEntry.ErrorOn = DateTime.Now;
+                                apiLogEntry.ErrorMsg = errorMsg.Remove(errorMsg.Count() - 2).Replace("'", " ");
 
-                        return response;
+                                response = request.CreateResponse(response.StatusCode, JsonConvert.DeserializeObject("{" + apiLogEntry.ResponseContentBody + "}"));
+                            }
+
+                            apiLogEntry.ResponseStatusCode = (int)response.StatusCode;
+                            apiLogEntry.ResponseTimestamp = DateTime.Now;
+                            WriteLog(apiLogEntry);
+
+                            return response;
+                        }
+                     
+                            
                     }, cancellationToken);
             }
 

+ 1 - 1
EVCB_OCPP.WEBAPI/Properties/PublishProfiles/FolderProfile.pubxml

@@ -13,6 +13,6 @@
     <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
     <ExcludeApp_Data>False</ExcludeApp_Data>
     <publishUrl>bin\Release\Publish</publishUrl>
-    <DeleteExistingFiles>False</DeleteExistingFiles>
+    <DeleteExistingFiles>True</DeleteExistingFiles>
   </PropertyGroup>
 </Project>