Exemplo n.º 1
0
        public void RegistrarLog(ActionExecutedContext filterContext)
        {
            try
            {
                var modelJson = "";
                if (filterContext.HttpContext.Request.HttpMethod.ToLower() == "post")
                {
                    var form  = Form(filterContext.HttpContext);
                    var token = form.FirstOrDefault(c => c.Key == "__RequestVerificationToken");

                    if (token != null)
                    {
                        form.Remove(form.FirstOrDefault(c => c.Key == "__RequestVerificationToken"));
                    }

                    modelJson = form.Aggregate("{", (current, item) => current + string.Format("'{0}':" + "'{1}',", item.Key, item.Value)) + "}";
                }

                var log = new AuditoriaModel(
                    UsuarioLogado.Matricula,
                    GetIP(filterContext),
                    filterContext.HttpContext.Request.Browser.Browser,
                    filterContext.HttpContext.Request.Browser.Version,
                    filterContext.HttpContext.Request.Url.AbsoluteUri,
                    modelJson);

                _context.LogAuditoria.Add(log);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                // Gravar Log de Erro
                Logger.SetLogPath();

                var context    = HttpContext.Current;
                var statusCode = new HttpException(null, ex).GetHttpCode();

                var sb = new StringBuilder();

                sb.AppendLine(statusCode + ", " + context.Request.HttpMethod + ", " + context.Request.Url.ToString());

                if (statusCode == 500)
                {
                    sb.AppendLine(ex.ToString());
                    Logger.Error(sb.ToString());
                }
                else
                {
                    Logger.Warning(sb.ToString());
                }
            }
        }