首页 > 其他 > 详细

日志帮助类

时间:2015-05-13 02:08:48      阅读:254      评论:0      收藏:0      [点我收藏+]
public class LogHelper
    {
        private static readonly object obj = new object();
        /// <summary>  
        /// 操作日志  
        /// </summary>  
        /// <param name="s">日志能容</param>  
        public static void WriteLog(string title, string content)
        {
            WriteLogs(title, content, "操作日志");
        }
        /// <summary>  
        /// 错误日志  
        /// </summary>  
        /// <param name="s">日志内容</param>  
        public static void WriteError(string title, string content)
        {
            WriteLogs(title, content, "错误日志");
        }

        public static void WriteLogs(string title, string content, string type)
        {
            lock (obj)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (!string.IsNullOrEmpty(path))
                {
                    path = AppDomain.CurrentDomain.BaseDirectory + "log";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + DateTime.Now.ToString("yyMM");
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" +"log"+ DateTime.Now.ToString("yyMMdd") + ".txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        sw.WriteLine(DateTime.Now + " " + title);
                        sw.WriteLine("日志类型:" + type);
                        sw.WriteLine("详情:" + content);
                        sw.WriteLine("----------------------------------------");
                        sw.Close();
                    }
                }
            }
        }
    }

?

日志帮助类

原文:http://zxlyecf2.iteye.com/blog/2210566

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!