diff --git a/Common/lib/HybirdFrameworkCore.dll b/Common/lib/HybirdFrameworkCore.dll index dfff4f4..e9227ea 100644 Binary files a/Common/lib/HybirdFrameworkCore.dll and b/Common/lib/HybirdFrameworkCore.dll differ diff --git a/Common/lib/HybirdFrameworkDriver.dll b/Common/lib/HybirdFrameworkDriver.dll index a593e5b..89ee193 100644 Binary files a/Common/lib/HybirdFrameworkDriver.dll and b/Common/lib/HybirdFrameworkDriver.dll differ diff --git a/WebStarter/MyFilter/RequestFilter.cs b/WebStarter/MyFilter/RequestFilter.cs new file mode 100644 index 0000000..4746638 --- /dev/null +++ b/WebStarter/MyFilter/RequestFilter.cs @@ -0,0 +1,43 @@ +using HybirdFrameworkCore.Entity; +using log4net; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace WebStarter.MyFilter; + +public class RequestFilter : Attribute, IActionFilter, IExceptionFilter, IResultFilter +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(RequestFilter)); + + public void OnActionExecuting(ActionExecutingContext context) + { + Log.Info($"OnActionExecuting {context.HttpContext.Request.Path}"); + } + + public void OnActionExecuted(ActionExecutedContext context) + { + Log.Info($"OnActionExecuted {context.HttpContext.Request.Path}, result = {context.Result}"); + } + + public void OnException(ExceptionContext context) + { + Log.Error($"{context.Exception.StackTrace}"); + if (!context.ExceptionHandled) + { + context.ExceptionHandled = true; + Result result = Result.Fail("系统异常"); + context.Result = new ObjectResult(result) + { + StatusCode = StatusCodes.Status500InternalServerError + }; + } + } + + public void OnResultExecuting(ResultExecutingContext context) + { + } + + public void OnResultExecuted(ResultExecutedContext context) + { + } +} diff --git a/WebStarter/Program.cs b/WebStarter/Program.cs index 0cffc4c..3f4f0bc 100644 --- a/WebStarter/Program.cs +++ b/WebStarter/Program.cs @@ -19,6 +19,7 @@ using Service.Plc.Client; using Service.RealTime; using SqlSugar; using SqlSugar.IOC; +using WebStarter.MyFilter; var builder = WebApplication.CreateBuilder(args); var log = LogManager.GetLogger(typeof(Program)); @@ -85,7 +86,10 @@ builder.Services.AddControllers().AddJsonOptions(configure => // configure.JsonSerializerOptions.Converters.Add(new LongJsonConverter()); }); -builder.Services.AddControllers(); +builder.Services.AddControllers(op => +{ + op.Filters.Add(); +}); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer();