private async Task SafeLog(DateTime requestTime, long responseMillis, int statusCode, string method, string path, string queryString, string requestBody, string responseBody, string ipAddress, ApplicationUser user) { // Do not log these events login, logout, getuserinfo... if ((path.ToLower().StartsWith("/api/account/")) || (path.ToLower().StartsWith("/api/UserProfile/"))) { return; } if (requestBody.Length > 256) { requestBody = $"(Truncated to 200 chars) {requestBody.Substring(0, 200)}"; } // If the response body was an ApiResponse we should just save the Result object if (responseBody != null && responseBody.Contains("\"result\":")) { try { ApiResponse apiResponse = JsonConvert.DeserializeObject <ApiResponse>(responseBody); responseBody = Regex.Replace(apiResponse.Result.ToString(), @"(""[^""\\]*(?:\\.[^""\\]*)*"")|\s+", "$1"); } catch { } } if (responseBody != null && responseBody.Length > 256) { responseBody = $"(Truncated to 200 chars) {responseBody.Substring(0, 200)}"; } if (queryString.Length > 256) { queryString = $"(Truncated to 200 chars) {queryString.Substring(0, 200)}"; } // Pass in the context to resolve the instance, and save to a store? await _apiLogManager.Log(new ApiLogItem { RequestTime = requestTime, ResponseMillis = responseMillis, StatusCode = statusCode, Method = method, Path = path, QueryString = queryString, RequestBody = requestBody, ResponseBody = responseBody ?? String.Empty, IPAddress = ipAddress, ApplicationUserId = user == null ? Guid.Empty : user.Id }); }