zoukankan      html  css  js  c++  java
  • C#中的日志类

         这段时间在做路测数据处理,每天都要导入千万条记录至数据库中。因为数据比较庞大,程序处理的指标也很多,厂家给的原始数据也不能保证百分百正确,那么在整个逻辑处理过程中难免会存在不确定性的BUG。为了高效的保证数据的处理,非常有必要将异常信息的现场情景记录下来,为后续分析提供依据,从而快速解决问题。

         日志类本身很简单,就是完成信息的记录功能,其实更重要的是保存哪些有用信息。下面是我参考了下前辈们写的东西修改而来的日志类:

        /// <summary>
        /// 日志类
        /// </summary>
        public class ImportDataLog
        {
            //日志文件所在路径
            private static string logPath = string.Empty;
            /// <summary>
            /// 保存日志的文件夹
            /// </summary>
            public static string LogPath
            {
                get
                {
                    if (logPath == string.Empty)
                    {
                       logPath = AppDomain.CurrentDomain.BaseDirectory;
                    }
                    return logPath;
                }
                set { logPath = value; }
            }
            //日志前缀说明信息
            private static string logFielPrefix = string.Empty;
            /// <summary>
            /// 日志文件前缀
            /// </summary>
            public static string LogFielPrefix
            {
                get { return logFielPrefix; }
                set { logFielPrefix = value; }
            }
            /// <summary>
            /// 写日志
            /// <param name="logType">日志类型</param>
            /// <param name="msg">日志内容</param>
            /// </summary>
            public static void WriteLog(string logType, string msg)
            {
                System.IO.StreamWriter sw=null;
                try
                {

          //同一天同一类日志以追加形式保存
                    sw = System.IO.File.AppendText(
                        LogPath + LogFielPrefix + "_" +
                        DateTime.Now.ToString("yyyyMMdd") + ".Log"
                        );
                    sw.WriteLine(logType + "#" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                }
                catch
                { }
                finally
                {
                    sw.Close();
                }
            }
            /// <summary>
            /// 写日志
            /// </summary>
            public static void WriteLog(LogType logType, string msg)
            {
                WriteLog(logType.ToString(), msg);
            }
        }
        /// <summary>
        /// 日志类型
        /// </summary>
        public enum LogType
        {
            Trace,  //堆栈跟踪信息
            Warning,//警告信息
            Error,  //错误信息应该包含对象名、发生错误点所在的方法名称、具体错误信息
            SQL    //与数据库相关的信息
        }

        在程序中,我是特别关注ERROR类型的信息。如注释所示,将引发错误的对象、方法与具体错误信息保存,对解决问题非常有帮助。

        定义Exception ex=new Exception() ,则:

        建议信息msg组成="Source:{" + ex.Source + "}" +                                                                        
                                               " StackTrace:{" + ex.StackTrace + "}" +
                                               " Message:{" + ex.Message + "}");

    http://u.huoban001.com/space.php
  • 相关阅读:
    带妹入坑,她该怎样提高自己的编程能力?
    性能测试--cpu使用率过高怎么办
    loadrunner Controller 删除available scripts中无用脚本
    loadrunner 立即执行+定时执行设置
    loadrunner11 :脚本日志打印设置及举例说明
    loadrunner录制chrome脚本:页面无响应
    Error -27492: "HttpSendRequest" failed, Windows error code=12152 (invalid server response) and retry。。。
    loadrunner11错误:Error -27776: Server "wsg.cmszmail.ad" shut connection during attempt to negotiate SSL session [MsgId: MERR-27776]解决办法
    loadrunner11 错误:Error -26377: No match found for the requested parameter XXXX. web_custom_request(XXX) highest severity level was "ERROR",....... [MsgId: MMSG-26387]
    loadrunner11 执行bat批处理文件时获取bat文件所在路径 正确方式与采坑说明
  • 原文地址:https://www.cnblogs.com/zpq521/p/1672752.html
Copyright © 2011-2022 走看看