记录日志文件


    class CommonFuc
    { 
  private static readonly string strLogPath = System.Configuration.ConfigurationSettings.AppSettings["logPath"];//日志路徑

        ///
        /// 用于記錄日志
        ///
        /// 日志path
        /// 日志內容
        public static void WriteLog(string logType, string logInfo)
        {
            string filename = string.Empty;
            switch (logType)
            {
                case "MailLog":
                    filename = strLogPath + @"\MailLogs\" + DateTime.Now.ToString("yyyyMMdd") + "-Notes.log"; ;
                    break;
                case "RuningLog":
                    filename = strLogPath + @"\ServiceLogs\" + DateTime.Now.ToString("yyyyMMdd") + "--B2CService.log";
                    break;
            }
            FileInfo f = new FileInfo(filename);
            FileStream fs = null;
            StreamWriter sw = null;
            Monitor.Enter(typeof(CommonFuc));
            try
            {
                if (f.Exists)
                {
                    fs = new FileStream(filename,FileMode.Append, FileAccess.Write, FileShare.Write);
                }
                else
                {
                    fs = new FileStream(filename,FileMode.Create, FileAccess.Write, FileShare.Write);
                }
                sw = new StreamWriter(fs);
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss :") + logInfo);
                sw.Flush();
            }
            catch (IOException e)
            {

            }
            finally
            {
                sw.Close();
                fs.Close();
                Monitor.Exit(typeof(CommonFuc));
            }
        }

        ///
        /// DELETE LOG
        ///
        ///
        public static void DeleHistoryLog(double days)
        {
            string del = "ServiceLogs,MailContent1,MailLogs";
            foreach (string item in del.Split(','))
            {
                DirectoryInfo dir = new DirectoryInfo(strLogPath + @"\" + item);
                FileInfo[] files = dir.GetFiles();
                foreach (FileInfo f in files)
                {
                    if (DateTime.Now.AddDays(days).CompareTo(f.CreationTime) > 0)
                    {
                        f.Delete();
                    }
                }
            } 
        }
}
 //在config中添加路径
 
 <!--日志路徑--&gt
   
 

//调用

            catch (Exception ex)
            {
                CommonFuc.WriteLog("RuningLog", "異常======>>" + ex.Message + "  SP======>>" + "pk_automial.p_show_mail_info");
            }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22392018/viewspace-772685/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/22392018/viewspace-772685/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值