-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2, 一些命名空间调整
- Loading branch information
Showing
41 changed files
with
189 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/ClownFish.Web/Aspnetcore/Filters/SimpleMvcLogFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#if NET6_0_OR_GREATER | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
|
||
namespace ClownFish.Web.Aspnetcore.Filters; | ||
|
||
public sealed class SimpleMvcLogFilter : IAsyncActionFilter | ||
{ | ||
private static readonly int s_frameworkBeforePerformanceThresholdMs = LocalSettings.GetInt("MVC_FrameworkBefore_PerformanceThresholdMs", 10); | ||
|
||
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | ||
{ | ||
HttpPipelineContext pipelineContext = HttpPipelineContext.Get(); | ||
if( pipelineContext == null ) | ||
throw new InvalidOperationException("当前请求没有关联到一个PipelineContext实例。"); | ||
|
||
NHttpContext httpContext = pipelineContext.HttpContext; | ||
|
||
Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor actionDescriptor | ||
= context.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor; | ||
|
||
// 补充 Controller/Action 信息 | ||
ActionDescription action = new ActionDescription(context.Controller, | ||
actionDescriptor.MethodInfo, | ||
actionDescriptor.ControllerTypeInfo); | ||
|
||
// 检查当前Action是否与登录有关,如果是,则做个标记,避免日志时记录敏感信息 | ||
bool isLogin = LoginActionAttribute.CurrentIsLogin(action); | ||
pipelineContext.SetAction(action, isLogin); | ||
|
||
|
||
// 登录请求一定不允许记录请求体,不管有没有 [LogRequestBody] 标记! | ||
if( isLogin ) { | ||
httpContext.Request.LogRequestBody = false; | ||
} | ||
else { | ||
// 非登录请求,并且【明确】要求记录请体 | ||
if( action.GetActionAttribute<LogRequestBodyAttribute>() != null ) { | ||
httpContext.Request.LogRequestBody = true; | ||
} | ||
} | ||
|
||
|
||
NHttpApplication app = AspnetCoreStarter.NApplication; | ||
app.PostFindAction(httpContext); | ||
app.AuthorizeRequest(httpContext); | ||
|
||
ControllerInit(httpContext); | ||
app.PreRequestExecute(httpContext); | ||
|
||
if( pipelineContext.OprLogScope.IsNull == false ) { | ||
WriteLog(pipelineContext); | ||
} | ||
|
||
httpContext.BeginExecuteTime = DateTime.Now; | ||
httpContext.TimeEvents?.Add(new NameTime("UserCode begin", httpContext.BeginExecuteTime)); | ||
await next(); | ||
httpContext.EndExecuteTime = DateTime.Now; | ||
httpContext.TimeEvents?.Add(new NameTime("UserCode end", httpContext.EndExecuteTime)); | ||
|
||
pipelineContext.ActionResult = context.Result; | ||
|
||
app.PostRequestExecute(httpContext); | ||
} | ||
|
||
private void ControllerInit(NHttpContext httpContext) | ||
{ | ||
ActionDescription action = httpContext.PipelineContext.Action; | ||
|
||
IControllerInit controller = action.Controller as IControllerInit; | ||
if( controller != null ) { | ||
controller.ControllerInit(httpContext); | ||
} | ||
|
||
IDisposable disposable = action.Controller as IDisposable; | ||
if( disposable != null ) { | ||
httpContext.RegisterForDispose(disposable); | ||
} | ||
} | ||
|
||
|
||
private void WriteLog(HttpPipelineContext pipelineContext) | ||
{ | ||
DateTime endTime = DateTime.Now; | ||
TimeSpan time = endTime - pipelineContext.StartTime; | ||
|
||
// 如果框架部分的执行时间较短就不记录到日志中 | ||
if( time.TotalMilliseconds < s_frameworkBeforePerformanceThresholdMs ) | ||
return; | ||
|
||
|
||
StepItem step = StepItem.CreateNew(pipelineContext.StartTime); | ||
|
||
step.StepKind = "framework"; | ||
step.StepName = "Framework_Before"; | ||
step.End(endTime); | ||
|
||
pipelineContext.OprLogScope.AddStep(step); | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ClownFish.Web.Aspnetcore; | ||
|
||
public interface IControllerInit | ||
{ | ||
void ControllerInit(NHttpContext httpContext); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示一个业务逻辑异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 验证参数时产生的异常 | ||
|
2 changes: 1 addition & 1 deletion
2
src/ClownFish.net/Base/Exceptions/ConfigurationErrorsException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示一个配置相关的错误 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 数据库没找到异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示数据库的重复插入异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示一个禁止访问的异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 获取异常的状态码接口定义 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示错误的代码异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 表示一个普通的消息异常,用于从代码中快速退出。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Base; | ||
namespace ClownFish.Base.Exceptions; | ||
|
||
/// <summary> | ||
/// 租户没找到异常 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Log; | ||
namespace ClownFish.Log.Attributes; | ||
|
||
/// <summary> | ||
/// 定义Action的功能性描述 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Log; | ||
namespace ClownFish.Log.Attributes; | ||
|
||
/// <summary> | ||
/// 指标某个消息类型在日志写入时【可以】做批次操作。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Log; | ||
namespace ClownFish.Log.Attributes; | ||
|
||
/// <summary> | ||
/// 定义Controller类型的功能性描述 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace ClownFish.Log; | ||
namespace ClownFish.Log.Attributes; | ||
|
||
/// <summary> | ||
/// 标记当前请求在记录日志时,将请求体也记录到日志中 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace ClownFish.Log.Attributes; | ||
|
||
/// <summary> | ||
/// 指示当前Action是不是登录相关的操作。 | ||
/// | ||
/// 登录请求会有二个不同的处理方式: | ||
/// 1、登录凭证不做自动续期处理 | ||
/// 2、日志记录时忽略请求体 | ||
/// </summary> | ||
/// <remarks> | ||
/// 注意: | ||
/// 在登录请求中,尤其是接收到用户提交的密码时,必须尽快验证处理,不要再传递到外部进程,否则可能造成密码泄露。 | ||
/// 例如在查询数据库对比密码时,应该先用HASH处理,避免使用明文。 | ||
/// </remarks> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] | ||
public sealed class LoginActionAttribute : Attribute | ||
{ | ||
internal static bool CurrentIsLogin(ActionDescription action) | ||
{ | ||
return action.MethodInfo.GetMyAttribute<LoginActionAttribute>() != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.