Skip to content

Commit

Permalink
1, add SimpleMvcLogFilter
Browse files Browse the repository at this point in the history
2, 一些命名空间调整
  • Loading branch information
fish-li committed Sep 1, 2023
1 parent 651c919 commit 9e953c2
Show file tree
Hide file tree
Showing 41 changed files with 189 additions and 54 deletions.
4 changes: 2 additions & 2 deletions src/ClownFish.Office/ClownFish.Office.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFramework>net7.0</TargetFramework>
<RootNamespace>ClownFish.Office</RootNamespace>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<Version>7.23.830.100</Version>
<FileVersion>7.23.830.100</FileVersion>
<Version>7.23.901.100</Version>
<FileVersion>7.23.901.100</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Tracing/ClownFish.Tracing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<RootNamespace>ClownFish.Tracing</RootNamespace>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<Version>7.23.830.100</Version>
<FileVersion>7.23.830.100</FileVersion>
<Version>7.23.901.100</Version>
<FileVersion>7.23.901.100</FileVersion>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<ApplicationIcon />
<OutputType>Library</OutputType>
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.Web/Aspnetcore/ExceptionModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public override void OnError(NHttpContext httpContext)

httpContext.TimeEvents?.Add(new NameTime("ExceptionModule.OnError begin"));

if( ex is ClownFish.Base.HttpException ex2 ) {
if( ex is HttpException ex2 ) {
httpContext.Response.StatusCode = ex.GetErrorCode();
httpContext.Response.ContentType = ResponseContentType.TextUtf8;
httpContext.Response.WriteAll(ex2.Message.GetBytes());
Expand Down
100 changes: 100 additions & 0 deletions src/ClownFish.Web/Aspnetcore/Filters/SimpleMvcLogFilter.cs
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
2 changes: 1 addition & 1 deletion src/ClownFish.Web/Aspnetcore/Filters/StatusCodeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private IActionResult CreateResult400(ResultExecutingContext context)

ErrorPageModel model = new ErrorPageModel {
Message = message,
ExceptionType = typeof(ValidationException).FullName,
ExceptionType = typeof(System.ComponentModel.DataAnnotations.ValidationException).FullName,
RequestId = HttpPipelineContext.Get()?.ProcessId,
StatusCode = 400
};
Expand Down
6 changes: 6 additions & 0 deletions src/ClownFish.Web/Aspnetcore/IControllerInit.cs
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);
}
4 changes: 2 additions & 2 deletions src/ClownFish.Web/ClownFish.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<RootNamespace>ClownFish.Web</RootNamespace>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<Version>7.23.830.100</Version>
<FileVersion>7.23.830.100</FileVersion>
<Version>7.23.901.100</Version>
<FileVersion>7.23.901.100</FileVersion>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Expand Down
3 changes: 3 additions & 0 deletions src/ClownFish.Web/GlobalUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@
global using System.Xml.Serialization;

global using ClownFish.Base;
global using ClownFish.Base.Exceptions;
global using ClownFish.Base.Reflection;
global using ClownFish.Base.WebClient;
global using ClownFish.Data;
global using ClownFish.Http.Pipleline;
global using ClownFish.Http.Proxy;
global using ClownFish.Log;
global using ClownFish.Log.Logging;
global using ClownFish.Log.Models;
global using ClownFish.Log.Attributes;

global using ClownFish.Web.Aspnetcore;
global using ClownFish.Web.AspnetCore.Objects;
Expand Down
4 changes: 1 addition & 3 deletions src/ClownFish.net/Base/Config/EnvironmentVariables.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Collections.Generic;

namespace ClownFish.Base;
namespace ClownFish.Base;

/// <summary>
/// 读取环境变量的工具类
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示一个业务逻辑异常
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/ClientDataException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 验证参数时产生的异常
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示一个配置相关的错误
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 数据库没找到异常
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示数据库的重复插入异常
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/ForbiddenException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示一个禁止访问的异常
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/HttpException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
///
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/IErrorCode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 获取异常的状态码接口定义
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/InvalidCodeException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示错误的代码异常
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/MessageException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示一个普通的消息异常,用于从代码中快速退出。
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Base/Exceptions/RemoteWebException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 表示在HTTP调用时发生的远程服务端异常。例如:HTTP 404, HTTP500 之类的异常。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Base;
namespace ClownFish.Base.Exceptions;

/// <summary>
/// 租户没找到异常
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// 验证参数时产生的异常
/// </summary>
public sealed class ValidationException : Exception, IErrorCode
public sealed class ValidationException2 : Exception, IErrorCode
{
/// <summary>
/// StatusCode, default value: 400
Expand All @@ -16,7 +16,7 @@ public sealed class ValidationException : Exception, IErrorCode
/// 构造方法
/// </summary>
/// <param name="message"></param>
public ValidationException(string message) : base(message)
public ValidationException2(string message) : base(message)
{
}

Expand All @@ -25,7 +25,7 @@ public ValidationException(string message) : base(message)
/// </summary>
/// <param name="message"></param>
/// <param name="innerException"></param>
public ValidationException(string message, Exception innerException) : base(message, innerException)
public ValidationException2(string message, Exception innerException) : base(message, innerException)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.net/ClownFish.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net48;netcoreapp3.1;net6.0;net7.0</TargetFrameworks>
<RootNamespace>ClownFish</RootNamespace>
<AssemblyVersion>7.0.0.0</AssemblyVersion>
<Version>7.23.830.100</Version>
<FileVersion>7.23.830.100</FileVersion>
<Version>7.23.901.100</Version>
<FileVersion>7.23.901.100</FileVersion>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
Expand Down
2 changes: 2 additions & 0 deletions src/ClownFish.net/GlobalUsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
global using ClownFish.Base.Reflection;
global using ClownFish.Base.WebClient;
global using ClownFish.Base.Xml;
global using ClownFish.Base.Exceptions;
global using ClownFish.Data;
global using ClownFish.Data.Internals;
global using ClownFish.Http.Pipleline;
global using ClownFish.Log;
global using ClownFish.Log.Logging;
global using ClownFish.Log.Models;
global using ClownFish.Log.Configuration;
global using ClownFish.Log.Attributes;
global using Newtonsoft.Json;
2 changes: 1 addition & 1 deletion src/ClownFish.net/Log/Attributes/ActionTitleAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Log;
namespace ClownFish.Log.Attributes;

/// <summary>
/// 定义Action的功能性描述
Expand Down
2 changes: 1 addition & 1 deletion src/ClownFish.net/Log/Attributes/BatchWritableAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Log;
namespace ClownFish.Log.Attributes;

/// <summary>
/// 指标某个消息类型在日志写入时【可以】做批次操作。
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Log;
namespace ClownFish.Log.Attributes;

/// <summary>
/// 定义Controller类型的功能性描述
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace ClownFish.Log;
namespace ClownFish.Log.Attributes;

/// <summary>
/// 标记当前请求在记录日志时,将请求体也记录到日志中
Expand Down
22 changes: 22 additions & 0 deletions src/ClownFish.net/Log/Attributes/LoginActionAttribute.cs
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;
}
}
1 change: 1 addition & 0 deletions src/ClownFish.netfx/WebHost/Config/ServerOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using System.Xml.Serialization;
using ClownFish.Base;
using ClownFish.Base.Exceptions;
using ClownFish.Base.Xml;

namespace ClownFish.WebHost.Config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using ClownFish.Base;
using ClownFish.WebHost.Utils;
using ClownFish.Http.Pipleline;
using ClownFish.Base.Exceptions;

namespace ClownFish.WebHost.Config
{
Expand Down
Loading

0 comments on commit 9e953c2

Please sign in to comment.