ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Asp.NetCore 请求日志拦截

2019-05-30 16:38:46  阅读:583  来源: 互联网

标签:Asp NetCore enterTime Request Microsoft context using 日志 HttpContext


using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Dynamics.WebApi.Filters
{
public class CustomerActionFilter : ActionFilterAttribute
{
/// <summary>
///
/// </summary>
private const string key = "enterTime";
/// <summary>
///
/// </summary>
private readonly ILogger _logger;
/// <summary>
///
/// </summary>
/// <param name="loggerFactory"></param>
public CustomerActionFilter(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<CustomerActionFilter>();
}
/// <summary>
/// 执行前拦截记录请求参数
/// </summary>
/// <param name="context"></param>
public override void OnActionExecuting(ActionExecutingContext context)
{
var actionDescriptor = string.Empty;
try
{
#region 记录请求日志
string requestBody = "";
Stream stream = context.HttpContext.Request.Body;
long contentLen = context.HttpContext.Request.ContentLength == null ? 0 : context.HttpContext.Request.ContentLength.Value;
if (stream != null)
{
stream.Position = 0;
byte[] buffer = new byte[contentLen];
stream.Read(buffer, 0, buffer.Length);
// 转化为字符串
requestBody = Encoding.UTF8.GetString(buffer);
}
long enterTime = DateTime.Now.Ticks;
context.ActionDescriptor.Properties[key] = enterTime;
context.HttpContext.Request.Headers.Append(key, new Microsoft.Extensions.Primitives.StringValues(enterTime.ToString()));
//[POST_Request] {requestid} http://dev.localhost/messagr/api/message/send {data}
actionDescriptor = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName + "/" + ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName;
var url = context.HttpContext.Request.Host + context.HttpContext.Request.Path + context.HttpContext.Request.QueryString;
_logger.LogDebug(string.Format("[{0}_Request] [{1}] {2} {3}\r\n{4}"
, actionDescriptor
, context.HttpContext.Request.Method
, enterTime
, url, requestBody));
#endregion

#region 请求参数验证
if (!context.ModelState.IsValid)
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
var errors = context.ModelState.Values.Select(s => s.Errors.Select(t => t.ErrorMessage));
context.Result = new JsonResult(errors);
}
#endregion
}
catch (Exception e)
{
_logger.LogError(e.Message + e.StackTrace);
}
finally
{
base.OnActionExecuting(context);
}
}
/// <summary>
/// 执行后拦截记录返回结果耗时
/// </summary>
/// <param name="context"></param>
public override void OnActionExecuted(ActionExecutedContext context)
{
string responseBody = string.Empty;
try
{
long enterTime = 0;
var costTime = string.Empty;
if (context.HttpContext.Request.Headers.TryGetValue(key, out var request_entertime))
{
enterTime = Convert.ToInt64(request_entertime);
costTime = new TimeSpan(DateTime.Now.Ticks - enterTime).ToString();
}
dynamic result = context.Result.GetType().Name == "EmptyResult" ? new { Value = "无返回结果" } : context.Result as dynamic;
if (result != null)
{
responseBody = Newtonsoft.Json.JsonConvert.SerializeObject(result.Value);
}
var actionDescriptor = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName + "/" + ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName;
_logger.LogDebug(string.Format("[{0}_Response] {1} CostTime:{2}\r\n{3}"
, actionDescriptor
, enterTime
, costTime
, responseBody));
}
catch (Exception e)
{
_logger.LogError(e.Message + e.StackTrace);
}
finally
{
base.OnActionExecuted(context);
}
}
}
}

标签:Asp,NetCore,enterTime,Request,Microsoft,context,using,日志,HttpContext
来源: https://www.cnblogs.com/Nine4Cool/p/10950360.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有