.NetCore中使用AspectCore、ExceptionLess 實現AOP操作日志記錄


結合前面封裝的ExceptionLess,接下來使用 AspectCore 實現AOP日志處理

nuget導入AspectCore.Core 、AspectCore.Extensions.DependencyInjection

重寫抽象類AbstractInterceptorAttribute

 public class OperatorLogAttribute : AbstractInterceptorAttribute
{

 public async override Task Invoke(AspectContext context, AspectDelegate next)
        {
            //加載Exceptionless服務
 var elg = context.ServiceProvider.GetService(typeof(IELLog)) as IELLog;
          //具體記錄情況略
        }

}

接下來只需要在Startup中添加動態代理即可,在ConfigServices 最后加上帶返回值為IServiceProvider

 return services.ConfigureDynamicProxy().BuildDynamicProxyServiceProvider();
ConfigureDynamicProxy中可添加動態代理配置具體參考資料如下:
參考資料:https://www.cnblogs.com/liuhaoyang/p/aspectcore-introduction-1.html

在我們需要記錄操作日志的地方加上標簽OperatorLog即可,當我們操作了某一個功能后,我們在ExecptionLess就可以看到相關操作日志

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM