Пример #1
0
        /// <summary>
        /// 安全地记录一个异常对象到文本文件。
        /// </summary>
        /// <param name="ex"></param>
        public static void SafeLogException(Exception ex)
        {
            if (ex is MyMessageException)
            {
                return;
            }

            if (ex is HttpException)
            {
                HttpException ee = ex as HttpException;
                if (ee.GetHttpCode() == 404)
                {
                    return;
                }
            }


            try {
                string logfilePath = Path.Combine(HttpRuntime.AppDomainAppPath, @"App_Data\ErrorLog.txt");
                string message     = ex.ToString() + "\r\n\r\n\r\n";
                if (HttpContext.Current != null)
                {
                    message = "Url: " + HttpContext.Current.Request.RawUrl + "\r\n" + message;
                }

                RetryFile.AppendAllText(logfilePath, message, System.Text.Encoding.UTF8);
            }
            catch { }
        }
Пример #2
0
        /// <summary>
        /// 批量写入日志信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        public void Write <T>(List <T> list) where T : Model.BaseInfo
        {
            if (list == null || list.Count == 0)
            {
                return;
            }

            // 注意:取类型名称时,不采用 info.GetType().Name ,因为可能有继承情况
            string filePath = GetFilePath(typeof(T));

            StringBuilder sb = new StringBuilder();

            foreach (T info in list)
            {
                string json     = ClownFish.Base.JsonExtensions.ToJson(info);
                string contents = json + "\r\n\r\n" + s_separateLine + "\r\n\r\n";
                sb.Append(contents);
            }


            if (sb.Length > 0)
            {
                RetryFile.AppendAllText(filePath, sb.ToString(), Encoding.UTF8);
            }
        }
Пример #3
0
        /// <summary>
        /// 写入单条日志信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        public void Write <T>(T info) where T : Model.BaseInfo
        {
            if (info == null)
            {
                return;
            }

            // 注意:取类型名称时,不采用 info.GetType().Name ,因为可能有继承情况
            string filePath = GetFilePath(typeof(T));


            string json = ClownFish.Base.JsonExtensions.ToJson(info);

            string contents = json + "\r\n\r\n" + s_separateLine + "\r\n\r\n";

            RetryFile.AppendAllText(filePath, contents, Encoding.UTF8);
        }
Пример #4
0
        /// <summary>
        /// 写入单条日志信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="info"></param>
        public void Write <T>(T info) where T : Model.BaseInfo
        {
            if (info == null)
            {
                return;
            }

            // 注意:取类型名称时,不采用 info.GetType().Name ,因为可能有继承情况
            string filePath = GetFilePath(typeof(T));


            string xml = XmlHelper.XmlSerialize(info, Encoding.UTF8);

            string contents = xml + "\r\n\r\n" + s_separateLine + "\r\n\r\n";

            RetryFile.AppendAllText(filePath, contents, Encoding.UTF8);
        }
Пример #5
0
        /// <summary>
        /// 写入一条消息到日志文件。
        /// 说明:为了防止程序突然崩溃,写入消息时,不做任何缓冲处理,且每次写入时打开文件
        /// </summary>
        /// <param name="category">消息类别,默认:INFO</param>
        /// <param name="message">消息文本</param>
        public virtual string Write(string message, string category = null)
        {
            // 扩展点:如果希望在写文件时,同时将消息输出到控制台,可以重写这个方法。

            string line = GetLine(message, category);

            if (line == null)
            {
                return(null);
            }

            if (_lock != null)
            {
                lock ( _lock ) {
                    RetryFile.AppendAllText(_filePath, line, Encoding.UTF8);
                }
            }
            else
            {
                RetryFile.AppendAllText(_filePath, line, Encoding.UTF8);
            }
            return(line);
        }