日期:2014-05-17  浏览次数:20859 次

C# 自定义异常 实现日志记录通用功能

  我想做个自定义的异常基类,首先肯定是要实现相关的4个构造函数,对吧。但是我想做到另外一个功能,

  可以选择性的实现日志记录的功能。在这个基类中如何实现?本人菜鸟,求高手指点。

 
C# code

    public class ParaException : ApplicationException//由用户程序引发,用于派生自定义的异常类型
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ParaException() { }
        public ParaException(string message)
            : base(message) { }
        public ParaException(string message, Exception inner)
            : base(message, inner) { }
        public ParaException(System.Runtime.Serialization.SerializationInfo info,
            System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }
    }
    


  怎么才能实现所谓的可选择性的添加日志功能?

------解决方案--------------------
可选择何意?
日志类型可使用枚举类型设置
------解决方案--------------------
在你的ParaException类中写一个日志记录的方法就可以了
捕获到异常后调用该方法。
C# code

            try
            {
                   //some code
            }
            catch (ParaException ex)
            {
                ex.WriteLog(ex.Message);
            }

------解决方案--------------------
C# code
namespace ESWS
{
    using System;
    using System.IO;
    using System.Text;
    using System.Threading;

    internal sealed class CustomException : Exception
    {
        private string errorMessage;
        private string methodName;
        private string stackTrace;

        internal CustomException()
        {
        }

        internal CustomException(string errorMessage) : base(errorMessage)
        {
        }

        internal CustomException(string errorMessage, Exception innerException) : base(errorMessage, innerException)
        {
            this.methodName = innerException.TargetSite.Name;
            this.errorMessage = errorMessage;
            this.stackTrace = innerException.StackTrace;
            new Thread(new ThreadStart(this.WriteLog)).Start();
        }

        public override Exception GetBaseException()
        {
            return this;
        }

        public override string ToString()
        {
            return base.GetType().FullName;
        }

        private void WriteLog()
        {
            this.WriteLog(this.methodName, this.errorMessage, this.stackTrace);
        }

        private void WriteLog(string methodName, string errorMessage, string stackTrace)
        {
            try
            {
                string str = string.Format(@"{0}\Log", Global.MainDirectoryName);
                string path = string.Format(@"{0}\{1}→{2}.log", str, methodName, DateTime.Now.ToString("yyyy-MM-dd HH"));
                if (!Directory.Exists(str))
                {
                    Directory.CreateDirectory(str);
                }
                using (FileStream stream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                {
                    string s = string.Concat(new object[] { "输出时间:", DateTime.Now, Environment.NewLine, " → 输出信息:", errorMessage, Environment.NewLine, " → 堆栈信息:", stackTrace, Environment.NewLine, Environment.NewLine });
                    byte[] bytes = Encoding.UTF8.GetBytes(s);
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            catch (IOException exception)
            {
                throw new IOException(exception.Message, exception);
            }
        }
    }
}

------解决方案--------------------
探讨

引用:
不知道你所谓的“可选择性的添加日志功能”是个神马功能。


就是我如果想把这个异常记录的时候,可以直接调用基类里面的记录日志的方法,直接保存异常信息