Exemple #1
0
        public async Task Log(ApiLogItem logitem)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Time [");
            sb.Append(string.Format("{0:dd/MM/yyyy HH:mm:ss}", DateTime.UtcNow));
            sb.Append("]");
            sb.Append(Environment.NewLine);


            sb.Append("Path [");
            sb.Append(logitem.Path);
            sb.Append("]");
            sb.Append(Environment.NewLine);

            sb.Append("Method [");
            sb.Append(logitem.Method);
            sb.Append("]");
            sb.Append(Environment.NewLine);


            if (logitem.Method == "GET")
            {
                if (!string.IsNullOrEmpty(logitem.QueryString))
                {
                    sb.Append("Query Srting [");
                    sb.Append(logitem.QueryString);
                    sb.Append("]");
                    sb.Append(Environment.NewLine);
                }
            }
            else
            {
                sb.Append("Request Body [");
                sb.Append(logitem.RequestBody);
                sb.Append("]");
                sb.Append(Environment.NewLine);
            }

            sb.Append("Response Body [");
            sb.Append(logitem.ResponseBody);
            sb.Append("]");
            sb.Append(Environment.NewLine);

            sb.Append(Environment.NewLine);
            sb.Append("---------------------------------------------------------------");
            sb.Append(Environment.NewLine);


            var filename = "Log_" + string.Format("{0:dd/MM/yyyy}", DateTime.UtcNow) + ".txt";
            var filepath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Log", filename);

            if (!File.Exists(filepath))
            {
                File.Create(filepath);
            }
            File.AppendAllText(filepath, sb.ToString());
            sb.Clear();
        }
 public async Task Log(ApiLogItem apiLogItem, Guid userId)
 {
     using (ApplicationDbContext _dbContext = new ApplicationDbContext(_optionsBuilder.Options))
     {
         if (userId != Guid.Empty)
         {
             apiLogItem.ApplicationUser = await _userManager.FindByIdAsync(userId.ToString());
         }
         _dbContext.ApiLogs.Add(apiLogItem);
         await _dbContext.SaveChangesAsync();
     }
 }
        public async Task Log(ApiLogItem apiLogItem)
        {
            using (ApplicationDbContext _dbContext = new ApplicationDbContext(_optionsBuilder.Options))
            {
                if (apiLogItem.ApplicationUserId == Guid.Empty)
                {
                    apiLogItem.ApplicationUserId = null;
                }

                _dbContext.ApiLogs.Add(apiLogItem);
                await _dbContext.SaveChangesAsync();
            }
        }
        /// <summary>
        /// Log
        /// </summary>
        /// <param name="apiLogItem"></param>
        /// <returns></returns>
        public async Task Log(ApiLogItem apiLogItem)
        {
            try
            {
                _logger.LogInformation(apiLogItem.RequestBody);
                await _db.ApiLogItems.AddAsync(apiLogItem);

                await _db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message + " : " + ex.StackTrace);
            }
        }
 public async Task <bool> Log(ApiLogItem apiLogItem)
 {
     try
     {
         // TODO Fix Entity Framework
         _db.ApiLogs.Add(apiLogItem);
         await _db.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         string message = ex.Message;
     }
     return(true);
 }
 public async Task Log(ApiLogItem apiLogItem)
 {
     try
     {
         using (ApplicationDbContext _dbContext = new ApplicationDbContext(_optionsBuilder.Options))
         {
             _dbContext.ApiLogs.Add(apiLogItem);
             await _dbContext.SaveChangesAsync();
         }
     }
     catch (Exception ex)
     {
         //TODO Error Fix "Violation of PRIMARY KEY constraint 'PK_AspNetUsers'. Cannot insert duplicate key in object 'dbo.AspNetUsers'. The statement has been terminated."
         string test = ex.Message + " " + ex.InnerException;
     }
 }
        public async Task Log(ApiLogItem apiLogItem)
        {
            if (apiLogItem.User != null)
            {
                //TODO populate _userSession??

                //var currentUser = _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
                //UserSession userSession = new UserSession();
                //if (currentUser != null)
                //{
                //    userSession = new UserSession(currentUser.Result);
                //}
            }
            else
            {
                apiLogItem.User = null;
            }
            await _dao.SaveOrUpdateAsync(apiLogItem);

            await _dao.FlushChangesAsync();
        }
Exemple #8
0
        public async Task Log(ApiLogItem apiLogItem)
        {
            if (apiLogItem.ApplicationUserId != Guid.Empty)
            {
                //TODO populate _userSession??

                //var currentUser = _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);
                //UserSession userSession = new UserSession();
                //if (currentUser != null)
                //{
                //    userSession = new UserSession(currentUser.Result);
                //}
            }
            else
            {
                apiLogItem.ApplicationUserId = null;
            }

            using (ApplicationDbContext _dbContext = new ApplicationDbContext(_optionsBuilder.Options, _userSession))
            {
                _dbContext.ApiLogs.Add(apiLogItem);
                await _dbContext.SaveChangesAsync();
            }
        }
 public async Task Log(ApiLogItem apiLogItem)
 {
     _logger.LogInformation(apiLogItem.ToString());
     await Task.CompletedTask;
 }
 public async Task Log(ApiLogItem apiLogItem)
 {
     _db.ApiLogs.Add(apiLogItem);
     await _db.SaveChangesAsync();
 }
Exemple #11
0
        private void VIPLog(DateTime requestTime,
                            long responseMillis,
                            int statusCode,
                            string method,
                            string path,
                            string queryString,
                            string requestBody,
                            string responseBody)
        {
            if (path.ToLower().Contains("token"))
            {
                requestBody  = "(Request logging disabled for token)";
                responseBody = "(Response logging disabled for token)";
            }

            if (path.ToLower().Contains("upload") || requestBody.Contains("FileBytes"))
            {
                requestBody = "(Request logging disabled for upload file)";
            }


            if (requestBody != null && requestBody.ToLower().Contains("password"))
            {
                dynamic jsonObj = JsonConvert.DeserializeObject(requestBody.ToLower());
                if (jsonObj["password"] != null)
                {
                    jsonObj["password"] = "******";
                }
                if (jsonObj["newpassword"] != null)
                {
                    jsonObj["newpassword"] = "******";
                }
                if (jsonObj["ownerpassword"] != null)
                {
                    jsonObj["ownerpassword"] = "******";
                }
                if (jsonObj["ownerconfirmpassword"] != null)
                {
                    jsonObj["ownerconfirmpassword"] = "******";
                }
                string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
                requestBody = output;
            }



            if (!IsSuccessStatusCode(statusCode))
            {
                var apiLog = new ApiLogItem
                {
                    RequestTime    = requestTime,
                    ResponseMillis = responseMillis,
                    StatusCode     = statusCode,
                    Method         = method,
                    Path           = path,
                    QueryString    = queryString,
                    RequestBody    = requestBody,
                    ResponseBody   = responseBody
                };

                try
                {
                    logger.Error(JsonConvert.SerializeObject(apiLog));
                }
                catch (Exception ex)

                {
                    logger.Error($"Exception on writing log: {ex.Message}");
                }
            }
        }