幸福框架:用户想看到的操作日志也要使用AOP吗?

背景

日志无论是对于开发人员、运维人员和最终用户都是一笔财富,是不是所有类型的日志都要AOP呢?本着交流的目的,这里先说一些看法,希望大家多批评。

常见的日志类型

异常日志

概念:记录异常的日志。

考虑:日志框架需要对不同的异常采用不同的日志方式,比如:那些为了向UI层返回消息的异常是不用记录到日志的,对于未期望异常也需要有不同的日志输入方式。

方式:AOP,适合在边界类使用(靠近系统边界的地方)。

事务:不需要参与业务事务。

服务对象:开发人员、运维人员。

示例:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Web.Mvc;
 7 
 8 using Common.Logging;
 9 using Happy.Web.Mvc.Newtonsoft;
10 
11 namespace Happy.Web.Mvc.ExceptionHanding
12 {
13     /// <summary>
14     /// 处理应用程序未捕获的异常。
15     /// </summary>
16     [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
17     public class WriteExceptionResultAttribute : FilterAttribute, IExceptionFilter
18     {
19         /// <inheritdoc />
20         public void OnException(ExceptionContext filterContext)
21         {
22             var exception = filterContext.Exception;
23 
24             this.LogException(exception);
25 
26             filterContext.Result = ExceptionInformationProviderRegistry.CreateErrorResult(exception);
27 
28             filterContext.ExceptionHandled = true;
29         }
30 
31         private void LogException(Exception exception)
32         {
33             if (FriendlyExceptionRegistry.Contains(exception.GetType()))
34             {
35                 return;
36             }
37 
38             LogManager.GetCurrentClassLogger().Error(exception);
39         }
40     }
41 }

操作日志

概念:记录用户操作的日志。

考虑:这类日志的格式很难统一,每个操作都需要自己的消息格式和参数,因为最终用户要看,还需要提供友好的界面。

方式:帮助方法,适合在应用层使用。

事务:要参与业务事务。

服务对象:运维人员、最终用户。

示例:省略。

性能监控日志、程序状态跟踪等

概念:记录开发人员感兴趣的任何内容。

考虑:监控的级别可能是整个业务事务或某一层的调用,甚至是某个方法。

方式:AOP或帮助方法。

事务:不要参与业务事务。

服务对象:运维人员、开发人员。

示例:省略。

备注

系统设计的时候最好就确定日志的需求。

 

posted on 2013-07-12 09:10  幸福框架  阅读(1775)  评论(0编辑  收藏  举报

导航

我要啦免费统计