首先创建配置文件,代码如下:

View Code
<exceptionHandling>
        <exceptionPolicies>
            <add name="Default Policy">
                <exceptionTypes>
                    <add name="All Exceptions" type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                        postHandlingAction="NotifyRethrow">
                        <exceptionHandlers>
                            <add name="Logging Exception Handler" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                                logCategory="Exception" eventId="100" severity="Error" title="Enterprise Library Exception Handling"
                                formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.TextExceptionFormatter, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"
                                priority="0" />
                        </exceptionHandlers>
                    </add>
                </exceptionTypes>
            </add>
        </exceptionPolicies>
    </exceptionHandling>

  两个公开方法,全部代码如下:

namespace Tmac.Utilities
{
    /// <summary>
    /// 异常处理操作类
    /// </summary>
    public class ExceptionHandlingUtil
    {
        //默认策略,与配置文件中定义的一致
        private const string defaultPolicy = "Default Policy";

        /// <summary>
        /// 记录异常日志
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static bool HandleException(Exception ex)
        {
            return HandleException(ex,defaultPolicy);
        }

        public static bool HandleException(Exception ex, string policy)
        {
            return ExceptionPolicy.HandleException(ex, policy);  
        }
    }
}
posted on 2013-03-19 22:08  永远的麦子  阅读(989)  评论(0编辑  收藏  举报