From d6ca4429d51d60708c1a00de75aab63b2c36aa26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Mon, 7 Nov 2022 01:31:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B1=9E=E6=80=A7=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E6=B3=A8=E5=85=A5=E3=80=81=E6=B7=BB=E5=8A=A0=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89=E6=97=A5=E5=BF=97=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/SwaggerDoc.xml | 16 +++++- .../Controllers/TestController.cs | 44 ++++++++++----- .../Yi.Framework.ApiMicroservice/Program.cs | 15 +++++- .../yi-sqlsugar-dev.db | Bin 241664 -> 241664 bytes .../Attribute/AutowiredAttribute.cs | 13 +++++ .../Yi.Framework.Service/UserService.cs | 1 + .../CustomAutofacAop.cs | 2 +- .../CustomAutofacModule.cs | 4 +- .../PropertiesAutowiredModule.cs | 51 ++++++++++++++++++ .../CustomHostingStartup.cs | 4 +- .../LogExtend/CustomLogger.cs | 29 ++++++++++ .../LogExtend/CustomLoggerExtensions.cs | 38 +++++++++++++ .../LogExtend/CustomLoggerProvider.cs | 22 ++++++++ 13 files changed, 218 insertions(+), 21 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.Common/Attribute/AutowiredAttribute.cs rename Yi.Framework.Net6/Yi.Framework.WebCore/{Utility => AutoFacExtend}/CustomAutofacAop.cs (92%) rename Yi.Framework.Net6/Yi.Framework.WebCore/{Utility => AutoFacExtend}/CustomAutofacModule.cs (97%) create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/PropertiesAutowiredModule.cs rename Yi.Framework.Net6/Yi.Framework.WebCore/{Utility => BuilderExtend}/CustomHostingStartup.cs (93%) create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLogger.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerExtensions.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerProvider.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml index e55e98e0..726f6ee9 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Config/SwaggerDoc.xml @@ -529,9 +529,9 @@ 测试控制器 - + - 依赖注入 + 依赖注入,优雅写法 @@ -652,5 +652,17 @@ + + + 自定义日志 + + + + + + 属性注入测试 + + + diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs index ccfec634..390ef8a8 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/TestController.cs @@ -37,9 +37,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers private IHubContext _hub; private ThumbnailSharpInvoer _thumbnailSharpInvoer; private CacheInvoker _cacheDb; + private ILogger _logger; + [Autowired] + public CacheInvoker CacheInvoker { get; set; } //你可以依赖注入服务层各各接口,也可以注入其他仓储层,怎么爽怎么来! /// - /// 依赖注入 + /// 依赖注入,优雅写法 /// /// /// @@ -50,22 +53,18 @@ namespace Yi.Framework.ApiMicroservice.Controllers /// /// public TestController(IHubContext hub, - ILogger logger, + ILogger logger, IRoleService iRoleService, IUserService iUserService, IStringLocalizer local, QuartzInvoker quartzInvoker, ThumbnailSharpInvoer thumbnailSharpInvoer, - CacheInvoker cacheInvoker) - { - _iUserService = iUserService; - _iRoleService = iRoleService; - _quartzInvoker = quartzInvoker; - _hub = hub; - _local = local; - _thumbnailSharpInvoer = thumbnailSharpInvoer; - _cacheDb = cacheInvoker; - } + CacheInvoker cacheInvoker) => + + (_logger,_iUserService, _iRoleService, _quartzInvoker, _hub, _local, _thumbnailSharpInvoer, _cacheDb) = + + (logger, iUserService, iRoleService, quartzInvoker, hub, local, thumbnailSharpInvoer, cacheInvoker); + /// /// swagger跳转 @@ -346,5 +345,26 @@ namespace Yi.Framework.ApiMicroservice.Controllers var data = _cacheDb.Get(key); return Result.Success().SetData(data); } + + /// + /// 自定义日志 + /// + /// + [HttpGet] + public Result CustomLogTest() + { + _logger.LogWarning("输出一条日志"); + return Result.Success(); + } + + /// + /// 属性注入测试 + /// + /// + [HttpGet] + public Result PropertyTest() + { + return Result.Success().SetStatus(CacheInvoker is not null); + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index babe4f1e..facff1e8 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -3,7 +3,6 @@ using Autofac.Extensions.DependencyInjection; using Yi.Framework.WebCore.BuilderExtend; using Yi.Framework.Core; using Yi.Framework.WebCore.MiddlewareExtend; -using Yi.Framework.WebCore.Utility; using Autofac; using Yi.Framework.Common.Models; using Yi.Framework.Language; @@ -15,6 +14,10 @@ using Yi.Framework.WebCore; using Microsoft.Extensions.DependencyInjection; using Yi.Framework.WebCore.DbExtend; using IPTools.Core; +using Yi.Framework.WebCore.LogExtend; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.AspNetCore.Mvc.Controllers; +using Yi.Framework.WebCore.AutoFacExtend; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddCommandLine(args); @@ -31,21 +34,29 @@ builder.Host.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(containerBuilder => { - #region //Moduleע #endregion containerBuilder.RegisterModule(); #region + //עģ + #endregion + //containerBuilder.RegisterModule(); + #region //ʹAppServiceŵĽԶע,ִʹøַʽԶע #endregion containerBuilder.AddAutoIocService("Yi.Framework.Repository", "Yi.Framework.Service"); }); +////ע룬mvcģתӸioc +//builder.Services.Replace(ServiceDescriptor.Transient()); + builder.Host.ConfigureLogging(loggingBuilder => { loggingBuilder.AddFilter("System", Microsoft.Extensions.Logging.LogLevel.Warning); loggingBuilder.AddFilter("Microsoft", Microsoft.Extensions.Logging.LogLevel.Warning); loggingBuilder.AddLog4Net("./Config/Log4net.config"); + //Զ־ + //loggingBuilder.AddCustomLogger(); }); #region // diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/yi-sqlsugar-dev.db index d02285a5cfd0570bc9951a213ec30fcacfd1c0ef..8aa31d6c5f18f103c57084003674265a14602bc6 100644 GIT binary patch delta 134 zcmZp8z}N7AZ-O*qz(g5m#(>6ztqF`v{Fwzg?5DE_Fe)-}*l!jT5ap1_6ck_+RF@Z5 z)MjvI009m$K8^Hbrs)%786Abp6pRe5OpL9Jjr0r+4K2(}&9|?LV2omC7UWl%%-*03 fG~*ZF3`HVo!wB0bD`Md)FRqP-k delta 73 zcmZp8z}N7AZ-O+V|3n#QM*qfytqF`v{FwzfR!wIQU{u^JDA2^QeN_Zw6g#s3zu{!| a29OXZ|K$4ftnFs`j6lq^-7KH^lmh@#z7})< diff --git a/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AutowiredAttribute.cs b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AutowiredAttribute.cs new file mode 100644 index 00000000..dd774be5 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Common/Attribute/AutowiredAttribute.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace System +{ + [AttributeUsage(AttributeTargets.Property)] + public class AutowiredAttribute : Attribute + { + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs index 89751d33..1bebffc3 100644 --- a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs +++ b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs @@ -15,6 +15,7 @@ using Yi.Framework.Repository; namespace Yi.Framework.Service { + public partial class UserService { public async Task> GetListInRole() diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacAop.cs similarity index 92% rename from Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs rename to Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacAop.cs index 85b0c251..1d7d0bf5 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacAop.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacAop.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -namespace Yi.Framework.WebCore.Utility +namespace Yi.Framework.WebCore.AutoFacExtend { public class CustomAutofacAop : IInterceptor { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs similarity index 97% rename from Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs rename to Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs index 847c69b4..4c6c0a85 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomAutofacModule.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/CustomAutofacModule.cs @@ -15,10 +15,10 @@ using Yi.Framework.Interface; using Yi.Framework.Job; using Yi.Framework.Repository; using Yi.Framework.Service; -using Yi.Framework.WebCore.Utility; +using Yi.Framework.WebCore.AutoFacExtend; using Module = Autofac.Module; -namespace Yi.Framework.WebCore.Utility +namespace Yi.Framework.WebCore.AutoFacExtend { public class CustomAutofacModule : Module { diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/PropertiesAutowiredModule.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/PropertiesAutowiredModule.cs new file mode 100644 index 00000000..b143fc81 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/AutoFacExtend/PropertiesAutowiredModule.cs @@ -0,0 +1,51 @@ +using Autofac; +using Autofac.Core; +using Autofac.Extras.DynamicProxy; +using Castle.DynamicProxy; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Yi.Framework.Interface; +using Yi.Framework.Job; +using Yi.Framework.Repository; +using Yi.Framework.Service; +using Module = Autofac.Module; + +namespace Yi.Framework.WebCore.AutoFacExtend +{ + public class PropertiesAutowiredModule : Module + { + + protected override void Load(ContainerBuilder containerBuilder) + { + //这里设置哪些程序集可以进行属性注入,默认控制器即可 + + // 获取所有控制器类型并使用属性注入 + var controllerBaseType = typeof(ControllerBase); + //先暂时获取这里的程序集,之后改造通过option进行配置 + containerBuilder.RegisterAssemblyTypes( Assembly.Load("Yi.Framework.ApiMicroservice")) + .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType) + .PropertiesAutowired(new AutowiredPropertySelector()); + + } + + } + + public class AutowiredPropertySelector : IPropertySelector + { + public bool InjectProperty(PropertyInfo propertyInfo, object instance) + { + return propertyInfo.CustomAttributes.Any(it => it.AttributeType == typeof(AutowiredAttribute)); + } + } + + +} \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/CustomHostingStartup.cs similarity index 93% rename from Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs rename to Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/CustomHostingStartup.cs index 02400da8..7e75cddf 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/Utility/CustomHostingStartup.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/BuilderExtend/CustomHostingStartup.cs @@ -7,9 +7,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -[assembly: HostingStartup(typeof(Yi.Framework.WebCore.Utility.CustomHostingStartup))] +[assembly: HostingStartup(typeof(Yi.Framework.WebCore.BuilderExtend.CustomHostingStartup))] //这里放上启动类下的dll,即自动侵入了,当然我们现在不会这样做。 -namespace Yi.Framework.WebCore.Utility +namespace Yi.Framework.WebCore.BuilderExtend { /// /// 必须实现IHostingStartup接口 diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLogger.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLogger.cs new file mode 100644 index 00000000..8a34d9ff --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLogger.cs @@ -0,0 +1,29 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.LogExtend +{ + public class CustomLogger : ILogger + { + public IDisposable BeginScope(TState state) => default!; + + public bool IsEnabled(LogLevel logLevel) + { + return logLevel == LogLevel.Warning; + } + + //真正日志执行的方法 + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) + { + if (!IsEnabled(logLevel)) + { + return; + } + Console.WriteLine($"你好,这里是自定义的日志哦~,{formatter(state, exception)}"); + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerExtensions.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerExtensions.cs new file mode 100644 index 00000000..6b4dba71 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerExtensions.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.LogExtend +{ + public static class CustomLoggerExtensions + { + public static ILoggingBuilder AddCustomLogger( + this ILoggingBuilder builder) + { + builder.AddConfiguration(); + + builder.Services.TryAddEnumerable( + ServiceDescriptor.Singleton()); + + //LoggerProviderOptions.RegisterProviderOptions(builder.Services); + + return builder; + } + + //public static ILoggingBuilder AddColorConsoleLogger( + // this ILoggingBuilder builder, + // Action configure) + //{ + // builder.AddColorConsoleLogger(); + // builder.Services.Configure(configure); + + // return builder; + //} + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerProvider.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerProvider.cs new file mode 100644 index 00000000..b69975d4 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/LogExtend/CustomLoggerProvider.cs @@ -0,0 +1,22 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.WebCore.LogExtend +{ + public class CustomLoggerProvider : ILoggerProvider + { + //创建我们自定义日志的方法 + public ILogger CreateLogger(string categoryName) + { + return new CustomLogger(); + } + + public void Dispose() + { + } + } +}