public void WriteLog(ApiLogType logtype, ApiLogEntry log, Exception ex)
        {
            switch (logtype)
            {
                case ApiLogType.Information:
                    Log.Logger.Information(log.Message + " {@ApiLogEntry}", log, ex);
                    break;
                case ApiLogType.Error:
                    Log.Logger.Error(log.Message + " {@ApiLogEntry}{@Exception}", log, ex);
                    break;
                case ApiLogType.Warning:
                    Log.Logger.Warning(log.Message + " {@ApiLogEntry}{@Exception}", log, ex);
                    break;
                case ApiLogType.Fatal:
                    Log.Logger.Fatal(log.Message + " {@ApiLogEntry}{@Exception}", log, ex);
                    break;
                case ApiLogType.Debug:
                    Log.Logger.Debug(log.Message + " {@ApiLogEntry}{@Exception}", log, ex);
                    break;
                case ApiLogType.Verbose:
                    Log.Logger.Verbose(log.Message + " {@ApiLogEntry}{@Exception}", log, ex);
                    break;
                default:
                    Log.Logger.Fatal("Can't identify a logging type!!!!" + " {@ApiLogEntry}", log);
                    break;

            }


            Log.CloseAndFlush();
            return;
        }
        public async Task<ApiLogEntry> GenerateLog(HttpRequestMessage request, HttpResponseMessage response, ApiBaseLog baseLog)
        {
            if (request != null)
            {
                var user = request.GetRequestContext().Principal as ClaimsPrincipal;
                var routeData = request.GetRouteData();
                var parsedRouteData = "";
                var responseContentType = "";
                var responseContent = "";
                var responseStatusCode = "";
                var responseHEaders = "";
                var requestContent = "";

                using (var stream = new MemoryStream())
                {
                    var context = (HttpContextBase)request.Properties["MS_HttpContext"];
                    context.Request.InputStream.Seek(0, SeekOrigin.Begin);
                    context.Request.InputStream.CopyTo(stream);
                    requestContent = Encoding.UTF8.GetString(stream.ToArray());
                }
              


                if (response != null)
                {

                    if (response?.Content?.Headers?.ContentType?.MediaType != null)
                    {
                        responseContentType = response.Content.Headers.ContentType.MediaType;
                    }

                    if (response?.Content != null)
                    {
                        responseContent = await response.Content.ReadAsStringAsync();
                    }


                    responseStatusCode = ((int) response?.StatusCode).ToString();
                    responseHEaders = SerializeHeaders(response.Headers);
                }
               


                if (routeData.Values.Any())
                {
                    parsedRouteData = SerializeRouteData(routeData);
                }
                //create the log
                var logEntry = new ApiLogEntry
                {
                    Application = request.Headers.UserAgent.ToString(),
                    DatabaseConnectionString = baseLog.ConnectionString,
                    RequestIpAddress = HttpContext.Current.Request.UserHostAddress ?? "",
                    RequestMethod = request.Method.Method ?? "",
                    RequestUri = request.RequestUri.AbsoluteUri ?? "",
                    Message = baseLog.Message ?? "",
                    CallingClass = string.Format("{0}", baseLog.CurrentClass),
                    CallingMethod = baseLog.CurrentMethod,
                 
                    Machine = HttpContext.Current.Request.UserHostName ?? "",
                    AuthorizationType = request?.Headers?.Authorization?.Scheme ?? "",
                    RequestTimestamp = DateTime.Now,

                    RequestContentType = request.Content?.Headers?.ContentType?.MediaType ?? "",
                    RequestContentBody = requestContent ?? "",

                    RequestHeaders = SerializeHeaders(request.Headers) ?? "",

                    //RESPONSE
                    ResponseTimestamp = DateTime.Now,
                    ResponseHeaders = responseHEaders,
                    ResponseContentType = responseContentType ?? "",
                    ResponseContentBody = responseContent ?? "",
                    RequestRouteData = parsedRouteData,
                    ResponseStatusCode = responseStatusCode


                };


                if (user?.Identity?.Name != null)
                {
                    logEntry.User = user.Identity.GetUserId();
                    logEntry.UserName = user.Identity.Name.Trim().ToLower();


                }
                else
                {
                    logEntry.User = Guid.Empty.ToString();
                    logEntry.UserName = "******";
                }

                return logEntry;

            }

            throw new Exception("Request was null, couldn't generate log entry!");
        }