添加属性依赖注入、添加自定义日志扩展
This commit is contained in:
@@ -529,9 +529,9 @@
|
||||
测试控制器
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.#ctor(Microsoft.AspNetCore.SignalR.IHubContext{Yi.Framework.WebCore.SignalRHub.MainHub},Microsoft.Extensions.Logging.ILogger{Yi.Framework.Model.Models.UserEntity},Yi.Framework.Interface.IRoleService,Yi.Framework.Interface.IUserService,Microsoft.Extensions.Localization.IStringLocalizer{Yi.Framework.Language.LocalLanguage},Yi.Framework.Core.QuartzInvoker,Yi.Framework.Core.ThumbnailSharpInvoer,Yi.Framework.Core.CacheInvoker)">
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.#ctor(Microsoft.AspNetCore.SignalR.IHubContext{Yi.Framework.WebCore.SignalRHub.MainHub},Microsoft.Extensions.Logging.ILogger{Yi.Framework.ApiMicroservice.Controllers.TestController},Yi.Framework.Interface.IRoleService,Yi.Framework.Interface.IUserService,Microsoft.Extensions.Localization.IStringLocalizer{Yi.Framework.Language.LocalLanguage},Yi.Framework.Core.QuartzInvoker,Yi.Framework.Core.ThumbnailSharpInvoer,Yi.Framework.Core.CacheInvoker)">
|
||||
<summary>
|
||||
依赖注入
|
||||
依赖注入,优雅写法
|
||||
</summary>
|
||||
<param name="hub"></param>
|
||||
<param name="logger"></param>
|
||||
@@ -652,5 +652,17 @@
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.CustomLogTest">
|
||||
<summary>
|
||||
自定义日志
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.Framework.ApiMicroservice.Controllers.TestController.PropertyTest">
|
||||
<summary>
|
||||
属性注入测试
|
||||
</summary>
|
||||
<returns></returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
|
||||
@@ -37,9 +37,12 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
private IHubContext<MainHub> _hub;
|
||||
private ThumbnailSharpInvoer _thumbnailSharpInvoer;
|
||||
private CacheInvoker _cacheDb;
|
||||
private ILogger<TestController> _logger;
|
||||
[Autowired]
|
||||
public CacheInvoker CacheInvoker { get; set; }
|
||||
//你可以依赖注入服务层各各接口,也可以注入其他仓储层,怎么爽怎么来!
|
||||
/// <summary>
|
||||
/// 依赖注入
|
||||
/// 依赖注入,优雅写法
|
||||
/// </summary>
|
||||
/// <param name="hub"></param>
|
||||
/// <param name="logger"></param>
|
||||
@@ -50,22 +53,18 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
/// <param name="thumbnailSharpInvoer"></param>
|
||||
/// <param name="cacheInvoker"></param>
|
||||
public TestController(IHubContext<MainHub> hub,
|
||||
ILogger<UserEntity> logger,
|
||||
ILogger<TestController> logger,
|
||||
IRoleService iRoleService,
|
||||
IUserService iUserService,
|
||||
IStringLocalizer<LocalLanguage> 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);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// swagger跳转
|
||||
@@ -346,5 +345,26 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
var data = _cacheDb.Get<string>(key);
|
||||
return Result.Success().SetData(data);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 自定义日志
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public Result CustomLogTest()
|
||||
{
|
||||
_logger.LogWarning("输出一条日志");
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 属性注入测试
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public Result PropertyTest()
|
||||
{
|
||||
return Result.Success().SetStatus(CacheInvoker is not null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>(containerBuilder =>
|
||||
{
|
||||
|
||||
#region
|
||||
//<2F><><EFBFBD><EFBFBD>Module<6C><65><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>
|
||||
#endregion
|
||||
containerBuilder.RegisterModule<CustomAutofacModule>();
|
||||
#region
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>ģ<EFBFBD><C4A3>
|
||||
#endregion
|
||||
//containerBuilder.RegisterModule<PropertiesAutowiredModule>();
|
||||
#region
|
||||
//ʹ<><CAB9>AppService<63><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ŵĽ<C5B5><C4BD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>ע<EFBFBD><D7A2>,<2C>ִ<EFBFBD><D6B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD>ø<EFBFBD><C3B8>ַ<EFBFBD>ʽ<EFBFBD>Զ<EFBFBD>ע<EFBFBD><D7A2>
|
||||
#endregion
|
||||
containerBuilder.AddAutoIocService("Yi.Framework.Repository", "Yi.Framework.Service");
|
||||
});
|
||||
////<2F><><EFBFBD><EFBFBD>ע<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>mvcģ<63><C4A3>ת<EFBFBD>Ӹ<EFBFBD>ioc
|
||||
//builder.Services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
|
||||
|
||||
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");
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>־
|
||||
//loggingBuilder.AddCustomLogger();
|
||||
});
|
||||
#region
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
Binary file not shown.
@@ -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
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ using Yi.Framework.Repository;
|
||||
|
||||
namespace Yi.Framework.Service
|
||||
{
|
||||
|
||||
public partial class UserService
|
||||
{
|
||||
public async Task<List<UserEntity>> GetListInRole()
|
||||
|
||||
@@ -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
|
||||
{
|
||||
@@ -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
|
||||
{
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 必须实现IHostingStartup接口
|
||||
@@ -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>(TState state) => default!;
|
||||
|
||||
public bool IsEnabled(LogLevel logLevel)
|
||||
{
|
||||
return logLevel == LogLevel.Warning;
|
||||
}
|
||||
|
||||
//真正日志执行的方法
|
||||
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
|
||||
{
|
||||
if (!IsEnabled(logLevel))
|
||||
{
|
||||
return;
|
||||
}
|
||||
Console.WriteLine($"你好,这里是自定义的日志哦~,{formatter(state, exception)}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<ILoggerProvider, CustomLoggerProvider>());
|
||||
|
||||
//LoggerProviderOptions.RegisterProviderOptions<ColorConsoleLoggerConfiguration, ColorConsoleLoggerProvider>(builder.Services);
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
//public static ILoggingBuilder AddColorConsoleLogger(
|
||||
// this ILoggingBuilder builder,
|
||||
// Action<ColorConsoleLoggerConfiguration> configure)
|
||||
//{
|
||||
// builder.AddColorConsoleLogger();
|
||||
// builder.Services.Configure(configure);
|
||||
|
||||
// return builder;
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user