diff --git a/build/version.props b/build/version.props
index 7386057e..b68c5a39 100644
--- a/build/version.props
+++ b/build/version.props
@@ -3,7 +3,7 @@
1
0
11
- 20181122-1
+ 20181123-1
$(VersionMajor).$(VersionMinor).$(VersionPatch)
preview-$(VersionQuality)
diff --git a/src/Util.Webs/Filters/TraceLogAttribute.cs b/src/Util.Webs/Filters/TraceLogAttribute.cs
index 18703371..bc438af0 100644
--- a/src/Util.Webs/Filters/TraceLogAttribute.cs
+++ b/src/Util.Webs/Filters/TraceLogAttribute.cs
@@ -1,4 +1,5 @@
using System;
+using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Internal;
using Microsoft.AspNetCore.Mvc.Filters;
using Util.Helpers;
@@ -27,17 +28,33 @@ namespace Util.Webs.Filters {
///
private ILog Logger { get; set; }
+ ///
+ /// 执行
+ ///
+ public override async Task OnActionExecutionAsync( ActionExecutingContext context, ActionExecutionDelegate next ) {
+ if( context == null )
+ throw new ArgumentNullException( nameof( context ) );
+ if( next == null )
+ throw new ArgumentNullException( nameof( next ) );
+ ActionFilterAttribute actionFilterAttribute = this;
+ actionFilterAttribute.OnActionExecuting( context );
+ await OnActionExecutingAsync( context );
+ if( context.Result != null )
+ return;
+ var executedContext = await next();
+ actionFilterAttribute.OnActionExecuted( executedContext );
+ }
+
///
/// 执行前
///
- public override void OnActionExecuting( ActionExecutingContext context ) {
- base.OnActionExecuting( context );
+ protected async Task OnActionExecutingAsync( ActionExecutingContext context ) {
if( Ignore )
return;
Logger = GetLog();
if( Logger.IsTraceEnabled == false )
return;
- WriteLog( context );
+ await WriteLog( context );
}
///
@@ -55,34 +72,34 @@ namespace Util.Webs.Filters {
///
/// 执行前日志
///
- private void WriteLog( ActionExecutingContext context ) {
+ private async Task WriteLog( ActionExecutingContext context ) {
Logger.Caption( "WebApi跟踪-准备执行操作" )
.Class( context.Controller.SafeString() )
.Method( context.ActionDescriptor.DisplayName );
- AddRequestInfo( context );
+ await AddRequestInfo( context );
Logger.Trace();
}
///
/// 添加请求信息参数
///
- private void AddRequestInfo( ActionExecutingContext context ) {
+ private async Task AddRequestInfo( ActionExecutingContext context ) {
var request = context.HttpContext.Request;
Logger.Params( "Http请求方式", request.Method );
if( string.IsNullOrWhiteSpace( request.ContentType ) == false )
Logger.Params( "ContentType", request.ContentType );
- AddFormParams( request );
+ await AddFormParams( request );
AddCookie( request );
}
///
/// 添加表单参数
///
- private void AddFormParams( Microsoft.AspNetCore.Http.HttpRequest request ) {
+ private async Task AddFormParams( Microsoft.AspNetCore.Http.HttpRequest request ) {
if( IsMultipart( request.ContentType ) )
return;
request.EnableRewind();
- var result = File.CopyToString( request.Body );
+ var result = await File.ToStringAsync( request.Body, isCloseStream: false );
if( string.IsNullOrWhiteSpace( result ) )
return;
Logger.Params( "表单参数:" ).Params( result );
@@ -110,8 +127,7 @@ namespace Util.Webs.Filters {
///
/// 执行后
///
- public override void OnResultExecuted( ResultExecutedContext context ) {
- base.OnResultExecuted( context );
+ public override void OnActionExecuted( ActionExecutedContext context ) {
if( Ignore )
return;
if( Logger.IsTraceEnabled == false )
@@ -122,7 +138,7 @@ namespace Util.Webs.Filters {
///
/// 执行后日志
///
- private void WriteLog( ResultExecutedContext context ) {
+ private void WriteLog( ActionExecutedContext context ) {
Logger.Caption( "WebApi跟踪-执行操作完成" )
.Class( context.Controller.SafeString() )
.Method( context.ActionDescriptor.DisplayName );
@@ -134,7 +150,7 @@ namespace Util.Webs.Filters {
///
/// 添加响应信息参数
///
- private void AddResponseInfo( ResultExecutedContext context ) {
+ private void AddResponseInfo( ActionExecutedContext context ) {
var response = context.HttpContext.Response;
if( string.IsNullOrWhiteSpace( response.ContentType ) == false )
Logger.Content( $"ContentType: {response.ContentType}" );
@@ -144,7 +160,7 @@ namespace Util.Webs.Filters {
///
/// 记录响应结果
///
- private void AddResult( ResultExecutedContext context ) {
+ private void AddResult( ActionExecutedContext context ) {
if( !( context.Result is Result result ) )
return;
Logger.Content( $"响应消息: { result.Message}" )
diff --git a/src/Util/Helpers/File.cs b/src/Util/Helpers/File.cs
index 8a1c4181..6e0ce1b6 100644
--- a/src/Util/Helpers/File.cs
+++ b/src/Util/Helpers/File.cs
@@ -1,5 +1,6 @@
using System.IO;
using System.Text;
+using System.Threading.Tasks;
namespace Util.Helpers {
///
@@ -10,8 +11,24 @@ namespace Util.Helpers {
/// 流转换成字符串
///
/// 流
- public static string ToString( Stream stream ) {
- return ToString( stream, Encoding.UTF8 );
+ /// 字符编码
+ /// 缓冲区大小
+ /// 读取完成是否释放流,默认为true
+ public static string ToString( Stream stream, Encoding encoding = null, int bufferSize = 1024 * 2, bool isCloseStream = true ) {
+ if( stream == null )
+ return string.Empty;
+ if( encoding == null )
+ encoding = Encoding.UTF8;
+ if( stream.CanRead == false )
+ return string.Empty;
+ using( var reader = new StreamReader( stream, encoding, true, bufferSize, !isCloseStream ) ) {
+ if( stream.CanSeek )
+ stream.Seek( 0, SeekOrigin.Begin );
+ var result = reader.ReadToEnd();
+ if( stream.CanSeek )
+ stream.Seek( 0, SeekOrigin.Begin );
+ return result;
+ }
}
///
@@ -19,44 +36,48 @@ namespace Util.Helpers {
///
/// 流
/// 字符编码
- public static string ToString( Stream stream, Encoding encoding ) {
+ /// 缓冲区大小
+ /// 读取完成是否释放流,默认为true
+ public static async Task ToStringAsync( Stream stream, Encoding encoding = null,int bufferSize = 1024 * 2, bool isCloseStream = true ) {
if( stream == null )
return string.Empty;
+ if( encoding == null )
+ encoding = Encoding.UTF8;
if( stream.CanRead == false )
return string.Empty;
- using( var reader = new StreamReader( stream, encoding ) ) {
+ using( var reader = new StreamReader( stream, encoding, true, bufferSize, !isCloseStream ) ) {
if( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
- return reader.ReadToEnd();
+ var result = await reader.ReadToEndAsync();
+ if( stream.CanSeek )
+ stream.Seek( 0, SeekOrigin.Begin );
+ return result;
}
}
- ///
- /// 复制流并转换成字符串
- ///
- /// 流
- public static string CopyToString( Stream stream ) {
- return CopyToString( stream, Encoding.UTF8 );
- }
-
///
/// 复制流并转换成字符串
///
/// 流
/// 字符编码
- public static string CopyToString( Stream stream, Encoding encoding ) {
+ public static async Task CopyToStringAsync( Stream stream, Encoding encoding = null ) {
if( stream == null )
return string.Empty;
+ if( encoding == null )
+ encoding = Encoding.UTF8;
if( stream.CanRead == false )
return string.Empty;
- using ( var memoryStream = new MemoryStream() ) {
+ using( var memoryStream = new MemoryStream() ) {
using( var reader = new StreamReader( memoryStream, encoding ) ) {
if( stream.CanSeek )
stream.Seek( 0, SeekOrigin.Begin );
stream.CopyTo( memoryStream );
if( memoryStream.CanSeek )
memoryStream.Seek( 0, SeekOrigin.Begin );
- return reader.ReadToEnd();
+ var result = await reader.ReadToEndAsync();
+ if( stream.CanSeek )
+ stream.Seek( 0, SeekOrigin.Begin );
+ return result;
}
}
}
diff --git a/src/Util/Helpers/Web.cs b/src/Util/Helpers/Web.cs
index 36df862b..d344af7c 100644
--- a/src/Util/Helpers/Web.cs
+++ b/src/Util/Helpers/Web.cs
@@ -1,9 +1,11 @@
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Claims;
using System.Text;
+using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
@@ -102,12 +104,24 @@ namespace Util.Helpers {
public static string Body {
get {
Request.EnableRewind();
- return File.CopyToString( Request.Body );
+ return File.ToString( Request.Body, isCloseStream: false );
}
}
#endregion
+ #region GetBodyAsync(获取请求正文)
+
+ ///
+ /// 获取请求正文
+ ///
+ public static async Task GetBodyAsync() {
+ Request.EnableRewind();
+ return await File.ToStringAsync( Request.Body, isCloseStream: false );
+ }
+
+ #endregion
+
#region Client( Web客户端 )
///