首页 > 其他 > 详细

简单日志LogHelper

时间:2017-04-28 09:51:35      阅读:182      评论:0      收藏:0      [点我收藏+]
 public static class LogHelper
    {
        //日志存储路径
        private static string LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, System.Configuration.ConfigurationManager.AppSettings["LogPath"]);

        private static object LogLock = new object();//日志锁

        /// <summary>
        /// 添加正常信息
        /// </summary>
        /// <param name="message"></param>
        public static void AddInfo(string message)
        {
            string fileName = DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称
            string fullName = Path.Combine(LogPath, fileName);

            lock (LogLock)
            {
                if (!Directory.Exists(LogPath))//如果目录不存在 创建目录
                {
                    Directory.CreateDirectory(LogPath);
                }
                using (var stream = File.AppendText(fullName))
                {
                    stream.WriteLine(message);
                }
                Console.WriteLine(message);
            }
        }

        /// <summary>
        /// 添加错误信息
        /// </summary>
        /// <param name="errorMessage"></param>
        public static void AddError(string errorMessage)
        {
            string fileName ="Error_"+ DateTime.Now.ToString("yyyyMMdd") + ".txt";//日志名称
            string fullName = Path.Combine(LogPath, fileName);

            lock (LogLock)
            {
                if (!Directory.Exists(LogPath))//如果目录不存在 创建目录
                {
                    Directory.CreateDirectory(LogPath);
                }
                using (var stream = File.AppendText(fullName))
                {
                    stream.WriteLine(errorMessage);
                }
                Console.WriteLine(errorMessage);
            }
        }
    }

 

简单日志LogHelper

原文:http://www.cnblogs.com/marshhu/p/6780138.html

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