.net core 3.1拡張AOPログ

6707 ワード

.net core 3.1拡張AOPログ


交流QQ群:555913397何か問題があったら、みんなと一緒に交流しましょう.

前回AutofacのAOPを拡張しましたが、AOP拡張にログコンポーネントを注入できないことがわかりました。手動で静的ログクラスを書くしかありません。くだらないことは言わないで、次はコードをつけます。


1.ログクラスの作成


1.1 NugetにNlogを追加する必要がある
using System;
using NLog;

namespace CoreMvc.Utinity.Helper
{
    public class LoggerHelper
    {
        /// 
        /// Fatal  
        /// 
        ///     
        ///        
        ///   
        public static void LogFatal(string message, string methodType = "", params object[] args)
        {
            LogFatal(message, null, methodType, args);
        }

        /// 
        /// Critical  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogFatal(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Fatal(exception, message, args);
        }

        /// 
        /// Debug  
        /// 
        ///     
        ///        
        ///   
        public static void LogDebug(string message, string methodType = "", params object[] args)
        {
            LogDebug(message, null, methodType, args);
        }

        /// 
        /// Debug  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogDebug(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Debug(exception, message, args);
        }

        /// 
        /// Error  
        /// 
        ///     
        ///        
        ///   
        public static void LogError(string message, string methodType = "", params object[] args)
        {
            LogError(message, null, methodType, args);
        }

        /// 
        /// Error  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogError(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Error(exception, message, args);
        }

        /// 
        /// Information  
        /// 
        ///     
        ///        
        ///   
        public static void LogInformation(string message, string methodType = "", params object[] args)
        {
            LogInformation(message, null, methodType, args);
        }

        /// 
        /// Information  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogInformation(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Info(exception, message, args);
        }

        /// 
        /// Trace  
        /// 
        ///     
        ///        
        ///   
        public static void LogTrace(string message, string methodType = "", params object[] args)
        {
            LogTrace(message, null, methodType, args);
        }

        /// 
        /// Trace  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogTrace(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Trace(exception, message, args);
        }

        /// 
        /// Warning  
        /// 
        ///     
        ///        
        ///   
        public static void LogWarning(string message, string methodType = "", params object[] args)
        {
            LogWarning(message, null, methodType, args);
        }

        /// 
        /// Warning  
        /// 
        ///     
        ///     
        ///        
        ///   
        public static void LogWarning(string message, Exception exception, string methodType = "", params object[] args)
        {
            LogManager.GetLogger(methodType).Warn(exception, message, args);
        }
    }
}


2.テストコールログ

using Castle.DynamicProxy;
using CoreMvc.Utinity.Helper;

namespace CoreMvc.Utinity.IOC.AopExtension
{
    public class AutofacAopExtension : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            LoggerHelper.LogDebug("   ", $"{invocation.TargetType}.{invocation.Method.Name}");
            invocation.Proceed();
            LoggerHelper.LogDebug("   ", $"{invocation.TargetType}.{invocation.Method.Name}");
        }
    }
}
 

3.ログ印刷結果

2020-03-10 09:37:06.5125||DEBUG|CoreMvc.Program|init main |url: |action: 
2020-03-10 09:37:09.1012||DEBUG|CoreMvc.Service.PeopleAppService.Show|    |url: http://localhost/|action: Index
2020-03-10 09:37:09.1098||DEBUG|CoreMvc.Service.PeopleAppService|People |url: http://localhost/|action: Index
2020-03-10 09:37:09.1098||DEBUG|CoreMvc.Service.PeopleAppService.Show|    |url: http://localhost/|action: Index
2020-03-10 09:37:09.1098||DEBUG|CoreMvc.Service.CompanyAppService.Show|    |url: http://localhost/|action: Index
2020-03-10 09:37:09.1098||DEBUG|CoreMvc.Service.CompanyAppService.Show|    |url: http://localhost/|action: Index


4.autofacaoplog構成チュートリアル
.net core 3.1 Nlogログを追加する.Netcore 3.1 Autofac AOPを拡張する.Netcore 3.1 Autofacコンテナ詳細の追加