能够因而日记追踪是哪位用户或ip恶意请求,调用这么些Action花了有些日子

log4net.config

在档次下有个log 文件夹

LoggerHelper

只要对您有扶助请动动鼠标右下方给自身来个赞,您的援救是本人最大的引力。

接下来引用监察和控制,在Global.asax 里增加那段

下一场引用监察和控制,在Global.asax 里增进这段

在类型下有个log 文件夹

图片 1图片 2

 

2017-11
代码及数据库文件已经上传至https://github.com/huangenai/WebAPI

 

图片 3图片 4

 

图片 5图片 6

图片 7图片 8

概念2个LoggerHelper,日志帮忙类

public class LoggerHelper
    {
        private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");
        private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");
        private static readonly log4net.ILog logmonitor = log4net.LogManager.GetLogger("logmonitor");

        public static void Error(string ErrorMsg, Exception ex = null)
        {
            if (ex != null)
            {
                logerror.Error(ErrorMsg, ex);
            }
            else
            {
                logerror.Error(ErrorMsg);
            }
        }

        public static void Info(string Msg)
        {
            loginfo.Info(Msg);
        }

        public static void Monitor(string Msg)
        {
            logmonitor.Info(Msg);
        }
    }

说到底在急需监控的控制器上丰盛WebApiTracker

此小说乃小编学习工作记录,如有疑问欢迎在底下评论,转发请标明出处。

/// <summary>    /// 监控日志对象    /// </summary>    public class WebApiMonitorLog    {        public string ControllerName { get; set; }        public string ActionName { get; set; }        public DateTime ExecuteStartTime { get; set; }        public DateTime ExecuteEndTime { get; set; }        /// <summary>        /// 请求的Action 参数        /// </summary>        public Dictionary<string, object> ActionParams { get; set; }        /// <summary>        /// Http请求头        /// </summary>        public string HttpRequestHeaders { get; set; }        /// <summary>        /// 请求方式        /// </summary>        public string HttpMethod { get; set; }        /// <summary>        /// 请求的IP地址        /// </summary>        public string IP { get; set; }        /// <summary>        /// 获取监控指标日志        /// </summary>        /// <param name="mtype"></param>        /// <returns></returns>        public string GetLoginfo()        {            string Msg = @"            Action执行时间监控:            ControllerName:{0}Controller            ActionName:{1}            开始时间:{2}            结束时间:{3}            总 时 间:{4}秒            Action参数:{5}            Http请求头:{6}            客户端IP:{7},            HttpMethod:{8}                    ";            return string.Format(Msg,                ControllerName,                ActionName,                ExecuteStartTime,                ExecuteEndTime,                (ExecuteEndTime - ExecuteStartTime).TotalSeconds,                GetCollections(ActionParams),                HttpRequestHeaders,                IP,                HttpMethod);        }        /// <summary>        /// 获取Action 参数        /// </summary>        /// <param name="Collections"></param>        /// <returns></returns>        public string GetCollections(Dictionary<string, object> Collections)        {            string Parameters = string.Empty;            if (Collections == null || Collections.Count == 0)            {                return Parameters;            }            foreach (string key in Collections.Keys)            {                Parameters += string.Format("{0}={1}&", key, Collections[key]);            }            if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&"))            {                Parameters = Parameters.Substring(0, Parameters.Length - 1);            }            return Parameters;        }        /// <summary>        /// 获取IP        /// </summary>        /// <returns></returns>        public string GetIP()        {            string ip = string.Empty;            if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);            if (string.IsNullOrEmpty                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);            return ip;        }    }

 

LoggerHelper

/// <summary>
    /// 监控日志对象
    /// </summary>
    public class WebApiMonitorLog
    {
        public string ControllerName { get; set; }
        public string ActionName { get; set; }

        public DateTime ExecuteStartTime { get; set; }
        public DateTime ExecuteEndTime { get; set; }

        /// <summary>
        /// 请求的Action 参数
        /// </summary>
        public Dictionary<string, object> ActionParams { get; set; }

        /// <summary>
        /// Http请求头
        /// </summary>
        public string HttpRequestHeaders { get; set; }

        /// <summary>
        /// 请求方式
        /// </summary>
        public string HttpMethod { get; set; }

        /// <summary>
        /// 请求的IP地址
        /// </summary>
        public string IP { get; set; }

        /// <summary>
        /// 获取监控指标日志
        /// </summary>
        /// <param name="mtype"></param>
        /// <returns></returns>
        public string GetLoginfo()
        {
            string Msg = @"
            Action执行时间监控:
            ControllerName:{0}Controller
            ActionName:{1}
            开始时间:{2}
            结束时间:{3}
            总 时 间:{4}秒
            Action参数:{5}
            Http请求头:{6}
            客户端IP:{7},
            HttpMethod:{8}
                    ";
            return string.Format(Msg,
                ControllerName,
                ActionName,
                ExecuteStartTime,
                ExecuteEndTime,
                (ExecuteEndTime - ExecuteStartTime).TotalSeconds,
                GetCollections(ActionParams),
                HttpRequestHeaders,
                IP,
                HttpMethod);
        }

        /// <summary>
        /// 获取Action 参数
        /// </summary>
        /// <param name="Collections"></param>
        /// <returns></returns>
        public string GetCollections(Dictionary<string, object> Collections)
        {
            string Parameters = string.Empty;
            if (Collections == null || Collections.Count == 0)
            {
                return Parameters;
            }
            foreach (string key in Collections.Keys)
            {
                Parameters += string.Format("{0}={1}&", key, Collections[key]);
            }
            if (!string.IsNullOrWhiteSpace(Parameters) && Parameters.EndsWith("&"))
            {
                Parameters = Parameters.Substring(0, Parameters.Length - 1);
            }
            return Parameters;
        }

        /// <summary>
        /// 获取IP
        /// </summary>
        /// <returns></returns>
        public string GetIP()
        {
            string ip = string.Empty;
            if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
            if (string.IsNullOrEmpty(ip))
                ip = Convert.ToString(System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
            return ip;
        }
    }

多谢谢谢!!

假诺对你有帮扶请动动鼠标右下方给本身来个赞,您的帮忙是本身最大的引力。

log4net.config

 

图片 9

谢谢感激!!

日志记录
图片 10

GlobalConfiguration.Configuration.Filters.Add(new WebApiTrackerAttribute());
            AreaRegistration.RegisterAllAreas();

图片 11图片 12

2017-11
代码及数据库文件已经上传至 https://github.com/huangenai/WebAPI

GlobalConfiguration.Configuration.Filters.Add(new WebApiTrackerAttribute;            AreaRegistration.RegisterAllAreas();

图片 13

public class LoggerHelper    {        private static readonly log4net.ILog loginfo = log4net.LogManager.GetLogger("loginfo");        private static readonly log4net.ILog logerror = log4net.LogManager.GetLogger("logerror");        private static readonly log4net.ILog logmonitor = log4net.LogManager.GetLogger("logmonitor");        public static void Error(string ErrorMsg, Exception ex = null)        {            if (ex != null)            {                logerror.Error(ErrorMsg, ex);            }            else            {                logerror.Error;            }        }        public static void Info(string Msg)        {            loginfo.Info;        }        public static void Monitor(string Msg)        {            logmonitor.Info;        }    }

老是在调用这些监察和控制下的Action 时,都会有日记记录,像那样滴

概念三个WebApiMonitorLog ,监控日志对象

 

概念八个WebApiTrackerAttribute类,继承于ActionFilterAttribute

 

参考于:http://www.cnblogs.com/lc-chenlong/p/4228639.html log4net
记录MVC监察和控制日志

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <log4net>
    <!--错误日志-->
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <file value="log\\LogError\\"/>
      <appendToFile value="true"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>
      <staticLogFileName value="false"/>
      <param name="MaxSizeRollBackups" value="100"/>
      <layout type="log4net.Layout.PatternLayout">
        <!--每条日志末尾的文字说明-->
        <!--输出格式-->
        <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->
        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n错误描述:%message%newline %n"/>
      </layout>
    </appender>
    <!--Info日志-->
    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\LogInfo\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n日志描述:%message%newline %n"/>
      </layout>
    </appender>

    <!--监控日志-->
    <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\LogMonitor\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n跟踪描述:%message%newline %n"/>
      </layout>
    </appender>
    <!--Error日志-->
    <logger name="logerror">
      <level value="ERROR" />
      <appender-ref ref="RollingLogFileAppender" />
    </logger>
    <!--Info日志-->
    <logger name="loginfo">
      <level value="INFO" />
      <appender-ref ref="InfoAppender" />
    </logger>
    <!--监控日志-->
    <logger name="logmonitor">
      <level value="Monitor" />
      <appender-ref ref="MonitorAppender" />
    </logger>
  </log4net>
</configuration>

每回在调用那些监察和控制下的Action 时,都会有日记记录,像那样滴

WebApiTrackerAttribute

图片 14

参考于:http://www.cnblogs.com/lc-chenlong/p/4228639.html  log4net
记录MVC监察和控制日志
 

在伸手WebApi
的时候,大家更想领悟在伏乞数据的时候,调用了哪个接口传了何等参数过来,调用这一个Action花了多少日子,有没有人恶意请求。大家得以通过记录日志,对Action进行优化,可以经过日记追踪是哪位用户或ip恶意请求。

新建一个log4net.config

此小说乃小编学习工作记录,如有疑问欢迎在底下评论,转发请标明出处。

在伏乞WebApi
的时候,我们更想明白在呼吁数据的时候,调用了哪位接口传了如何参数过来,调用那个Action花了有个别日子,有没有人恶意请求。大家能够通过记录日志,对Action实行优化,能够经过日记追踪是哪些用户或ip恶意请求。

在类型中援引log4net.dll

图片 15

<?xml version="1.0"?><configuration>  <configSections>    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>  </configSections>  <log4net>    <!--错误日志-->    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">      <file value="log\\LogError\\"/>      <appendToFile value="true"/>      <rollingStyle value="Date"/>      <datePattern value="yyyy\\yyyyMM\\yyyyMMdd'.txt'"/>      <staticLogFileName value="false"/>      <param name="MaxSizeRollBackups" value="100"/>      <layout type="log4net.Layout.PatternLayout">        <!--每条日志末尾的文字说明-->        <!--输出格式-->        <!--样例:2008-03-26 13:42:32,111 [10] INFO  Log4NetDemo.MainClass [(null)] - info-->        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n错误描述:%message%newline %n"/>      </layout>    </appender>    <!--Info日志-->    <appender name="InfoAppender" type="log4net.Appender.RollingFileAppender">      <param name="File" value="Log\\LogInfo\\" />      <param name="AppendToFile" value="true" />      <param name="MaxFileSize" value="10240" />      <param name="MaxSizeRollBackups" value="100" />      <param name="StaticLogFileName" value="false" />      <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />      <param name="RollingStyle" value="Date" />      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n日志描述:%message%newline %n"/>      </layout>    </appender>    <!--监控日志-->    <appender name="MonitorAppender" type="log4net.Appender.RollingFileAppender">      <param name="File" value="Log\\LogMonitor\\" />      <param name="AppendToFile" value="true" />      <param name="MaxFileSize" value="10240" />      <param name="MaxSizeRollBackups" value="100" />      <param name="StaticLogFileName" value="false" />      <param name="DatePattern" value="yyyy\\yyyyMM\\yyyyMMdd'.txt'" />      <param name="RollingStyle" value="Date" />      <layout type="log4net.Layout.PatternLayout">        <conversionPattern value="%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别:  %-5level %n跟踪描述:%message%newline %n"/>      </layout>    </appender>    <!--Error日志-->    <logger name="logerror">      <level value="ERROR" />      <appender-ref ref="RollingLogFileAppender" />    </logger>    <!--Info日志-->    <logger name="loginfo">      <level value="INFO" />      <appender-ref ref="InfoAppender" />    </logger>    <!--监控日志-->    <logger name="logmonitor">      <level value="Monitor" />      <appender-ref ref="MonitorAppender" />    </logger>  </log4net></configuration>

概念八个WebApiMonitorLog ,监察和控制日志对象

新建三个log4net.config

 

WebApiMonitorLog

 

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]    public class WebApiTrackerAttribute : ActionFilterAttribute    {        private readonly string Key = "_thisWebApiOnActionMonitorLog_";        public override void OnActionExecuting(HttpActionContext actionContext) {            base.OnActionExecuting(actionContext);            WebApiMonitorLog MonLog = new WebApiMonitorLog();            MonLog.ExecuteStartTime = DateTime.Now;            //获取Action 参数            MonLog.ActionParams = actionContext.ActionArguments;            MonLog.HttpRequestHeaders = actionContext.Request.Headers.ToString();            MonLog.HttpMethod = actionContext.Request.Method.Method;            actionContext.Request.Properties[Key] = MonLog;            var form = System.Web.HttpContext.Current.Request.Form;            #region 如果参数是实体对象,获取序列化后的数据            Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result;            Encoding encoding = Encoding.UTF8;            stream.Position = 0;            string responseData = "";            using (StreamReader reader = new StreamReader(stream, encoding)) {                responseData = reader.ReadToEnd().ToString();            }            if (!string.IsNullOrWhiteSpace(responseData) && !MonLog.ActionParams.ContainsKey("__EntityParamsList__")) {                MonLog.ActionParams["__EntityParamsList__"] = responseData;            }            #endregion        }        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) {            WebApiMonitorLog MonLog = actionExecutedContext.Request.Properties[Key] as WebApiMonitorLog;            MonLog.ExecuteEndTime = DateTime.Now;            MonLog.ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;            MonLog.ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;            LoggerHelper.Monitor(MonLog.GetLoginfo;            if (actionExecutedContext.Exception != null) {                string Msg = string.Format(@"                请求【{0}Controller】的产生异常:                Action参数:{2}               Http请求头:{3}                客户端IP:{4},                HttpMethod:{5}                    ", MonLog.ControllerName, MonLog.ActionName, MonLog.GetCollections(MonLog.ActionParams), MonLog.HttpRequestHeaders, MonLog.GetIP(), MonLog.HttpMethod);                LoggerHelper.Error(Msg, actionExecutedContext.Exception);            }        }    }

 

WebApiTrackerAttribute

日志记录
图片 16

概念贰个WebApiTrackerAttribute类,继承于ActionFilterAttribute

概念2个LoggerHelper,日志协助类

在类型中引用log4net.dll

最后在要求监察和控制的控制器上添加 WebApiTracker

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
    public class WebApiTrackerAttribute : ActionFilterAttribute
    {
        private readonly string Key = "_thisWebApiOnActionMonitorLog_";
        public override void OnActionExecuting(HttpActionContext actionContext) {
            base.OnActionExecuting(actionContext);
            WebApiMonitorLog MonLog = new WebApiMonitorLog();
            MonLog.ExecuteStartTime = DateTime.Now;
            //获取Action 参数
            MonLog.ActionParams = actionContext.ActionArguments;
            MonLog.HttpRequestHeaders = actionContext.Request.Headers.ToString();
            MonLog.HttpMethod = actionContext.Request.Method.Method;

            actionContext.Request.Properties[Key] = MonLog;
            var form = System.Web.HttpContext.Current.Request.Form;
            #region 如果参数是实体对象,获取序列化后的数据
            Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result;
            Encoding encoding = Encoding.UTF8;
            stream.Position = 0;
            string responseData = "";
            using (StreamReader reader = new StreamReader(stream, encoding)) {
                responseData = reader.ReadToEnd().ToString();
            }
            if (!string.IsNullOrWhiteSpace(responseData) && !MonLog.ActionParams.ContainsKey("__EntityParamsList__")) {
                MonLog.ActionParams["__EntityParamsList__"] = responseData;
            }
            #endregion
        }

        public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) {
            WebApiMonitorLog MonLog = actionExecutedContext.Request.Properties[Key] as WebApiMonitorLog;
            MonLog.ExecuteEndTime = DateTime.Now;
            MonLog.ActionName = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;
            MonLog.ControllerName = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
            LoggerHelper.Monitor(MonLog.GetLoginfo());
            if (actionExecutedContext.Exception != null) {
                string Msg = string.Format(@"
                请求【{0}Controller】的【{1}】产生异常:
                Action参数:{2}
               Http请求头:{3}
                客户端IP:{4},
                HttpMethod:{5}
                    ", MonLog.ControllerName, MonLog.ActionName, MonLog.GetCollections(MonLog.ActionParams), MonLog.HttpRequestHeaders, MonLog.GetIP(), MonLog.HttpMethod);
                LoggerHelper.Error(Msg, actionExecutedContext.Exception);
            }
        }
    }

图片 17图片 18

图片 19图片 20

WebApiMonitorLog

 

 

图片 21图片 22

 

相关文章