| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.Mvc.Filters;
- using Microsoft.Extensions.Configuration;
- using NLog;
- namespace MadRunFabric.Common
- {
- public class ActionFilter: ActionFilterAttribute
- {
- IConfiguration _configuration;
- public ActionFilter(IConfiguration configuration )
- {
- _configuration = configuration;
- }
- public override void OnActionExecuting(ActionExecutingContext context)
- {
- //try
- //{
- // if (_configuration["IsLogin"] == "0")
- // {
- // context.Result = new ContentResult() { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "系统异常,请稍后重试" }.ToJson() };
- // return;
- // }
- //}
- //catch
- //{
- //}
- //var dateauth = DateTime.Parse(_configuration["AuthDate"]);
- //if ( DateTime.Now> dateauth) {
- // context.Result = new ContentResult() { Content = new AjaxResult { state = ResultTypes.notoken.ToString(), message = "授权已到期" }.ToJson() };
- // return;
- //}
-
- var request = context.HttpContext.Request;
- var log = LogManager.GetCurrentClassLogger();
- string path = context.ActionDescriptor.AttributeRouteInfo.Template;
- if (!string.IsNullOrEmpty(path))
- {
- log = LogManager.GetLogger(path);
- }
- bool islog = false;
- try
- {
- if (request.Method == "GET")
- {
- islog = request.Query.ContainsKey("log");
- }
- else
- {
- islog = request.Form.ContainsKey("log");
- }
- }
- catch
- {
- log.Error("ContentType:" + request.ContentType );
- }
- if (islog)
- {
- var ip = request.Headers["X-Forwarded-For"];
- if (string.IsNullOrEmpty(ip))
- {
- ip = context.HttpContext.Connection.RemoteIpAddress.ToString();
- }
- string userinfo = string.Empty;
- var claims = context.HttpContext.User.Claims.GetEnumerator();
- while (claims.MoveNext())
- {
- if (claims.Current.Type.IndexOf("/") >= 0)
- {
- userinfo += claims.Current.Type.Substring(claims.Current.Type.LastIndexOf('/') + 1) + ":" + claims.Current.Value + ";";
- }
- }
- var param = string.Empty;
- if (request.Method == "GET")
- {
- param = request.Query.ToJson();
- }
- else
- {
- param = request.Form.ToJson();
- }
- log.Error("IP:" + ip.ToString() + "|||UserInfo:" + userinfo + "|||ContentType:" + request.ContentType + "|||Params:" + param);
- }
- base.OnActionExecuting(context);
- }
- public override void OnActionExecuted(ActionExecutedContext context)
- {
- var request = context.HttpContext.Request;
- bool islog = false;
- try
- {
- if (request.Method == "GET")
- {
- islog = request.Query.ContainsKey("log");
- }
- else
- {
- islog = request.Form.ContainsKey("log");
- }
- }
- catch
- {
- islog = true;
- }
- if (islog)
- {
- var log = LogManager.GetCurrentClassLogger();
- string path = context.ActionDescriptor.AttributeRouteInfo.Template;
- if (!string.IsNullOrEmpty(path))
- {
- log = LogManager.GetLogger(path);
- }
- log.Error(context.Result.ToJson());
- }
- base.OnActionExecuted(context);
- }
- }
- }
|