Exemple #1
0
        public override void OnResultExecuted(ResultExecutedContext context)
        {
            var appUser = new AppUser
            {
                UserName    = context.HttpContext.User.Identity.Name,
                LoginDate   = DateTime.Now,
                Path        = context.HttpContext.Request.Path,
                Method      = context.HttpContext.Request.Method,
                QueryString = context.HttpContext.Request.QueryString.ToString()
            };


            // check if the Request is a POST call
            // since we need to read from the body
            if (context.HttpContext.Request.Method == "POST")
            {
                context.HttpContext.Request.EnableBuffering();
                string body = new StreamReader(context.HttpContext.Request.Body)
                              .ReadToEnd();
                context.HttpContext.Request.Body.Position = 0;
                appUser.Payload = body;
            }

            appUser.RequestedOn = DateTime.Now;

            LoggerDbContext logger = new LoggerDbContext();

            logger.AppUsers.Add(appUser);
            logger.SaveChanges();
        }
Exemple #2
0
        private void addLog(string msj, string tipo)
        {
            Logs l = new Logs();

            l.Message = msj;
            l.When    = GetCurrentTime();
            l.Type    = tipo;
            _db.Logs.Add(l);
            _db.SaveChanges();
        }
Exemple #3
0
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            AppUser appUser = new Models.AppUser()
            {
                UserName  = context.HttpContext.User.Identity.Name,
                LoginDate = DateTime.Now,
            };

            LoggerDbContext logger = new LoggerDbContext();

            logger.AppUsers.Add(appUser);
            logger.SaveChanges();
        }
 public void AddLog(Log log)
 {
     using (var db = new LoggerDbContext())
         try
         {
             db.LogSystem.Add(log);
             db.SaveChanges();
         }
         catch (Exception e)
         {
             throw new LoggerException("Error al agregar log al sistema", e);
         }
 }
        protected override void Write(LogEventInfo logEvent)
        {
            var jsonLogEntry = base.RenderLogEvent(Layout, logEvent);

            var logEntry = JObject
                           .Parse(jsonLogEntry)
                           .ToObject <LogEntry>();

            using (var context = new LoggerDbContext(ConnectionStringName))
            {
                context
                .LogEntries
                .Add(logEntry);

                context.SaveChanges();
            }
        }
Exemple #6
0
        public void Log <TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func <TState, Exception, string> formatter)
        {
            string login = httpContextAccessor.HttpContext.User.Identity.Name;

            if (!IsEnabled(logLevel))
            {
                return;
            }

            if (formatter == null)
            {
                throw new ArgumentNullException(nameof(formatter));
            }
            var message = formatter(state, exception);

            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            if (exception != null)
            {
                message += "\n" + exception.ToString();
            }
            try
            {
                //throw new Exception("test");
                _context.EventLog.Add(new EventLog
                {
                    Message     = $"*** {login} ***{Environment.NewLine}{message}",
                    EventId     = eventId.Id,
                    LogLevel    = logLevel.ToString(),
                    CreatedTime = DateTime.Now
                });
                _context.SaveChanges();
            }
            catch (Exception)
            {
                throw;
                //_emailSender.SendEmailAsync("*****@*****.**", "Exam error", ex.Message).Wait();
                //_emailSender.SendEmailAsync("*****@*****.**", "Exam error", ex.Message).Wait();
            }
        }