예제 #1
0
파일: Program.cs 프로젝트: MatasGos/BMAN
        public static void MediatorDemo()
        {
            ILogMediator mediator = new LogMediator();

            ConsoleLogger consoleLog = new ConsoleLogger(mediator);

            LogPlayer playerLog = new LogPlayer(mediator);

            mediator.addLogReceiver(consoleLog);
            mediator.addLogSender(playerLog);

            playerLog.sendMessage("Player killed Player 2");

            consoleLog.sendMessage("error: console error");
        }
예제 #2
0
        /// <summary>
        /// 拦截方法
        /// </summary>
        /// <param name="context">方法元数据</param>
        public void Advise(MethodAdviceContext context)
        {
            this._runningLog.BuildRuningInfo(context);
            this._runningLog.BuildBasicInfo(context);
            this._runningLog.BuildMethodArgsInfo(context);

            context.Proceed();

            this._runningLog.BuildReturnValueInfo(context);

            //无需事务
            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
            {
                //持久化
                LogMediator.Write(this._runningLog);

                scope.Complete();
            }
        }
예제 #3
0
        /// <summary>
        /// 发生异常事件
        /// </summary>
        /// <param name="context">方法元数据</param>
        /// <param name="exception">异常实例</param>
        protected virtual void OnException(MethodAdviceContext context, Exception exception)
        {
            if (!context.TargetMethod.IsDefined(typeof(SkipExceptionAttribute), true))
            {
                //初始化异常日志
                this._exceptionLog.BuildBasicInfo(context);
                this._exceptionLog.BuildMethodArgsInfo(context);
                this._exceptionLog.BuildExceptionInfo(exception);

                //无需事务
                using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled))
                {
                    //插入数据库
                    Guid newId = LogMediator.Write(this._exceptionLog);

                    scope.Complete();

                    //初始化异常消息
                    this._exceptionMessage = new ExceptionMessage(exception.Message, newId);
                }
            }
        }