日志文件

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Configuration;

namespace Dingy.Log
{
    public class Logs
    {
        /// <summary>
        /// 日志目录名称,最后不含目录名称
        /// <AppSettings>
        /// <add key="LogDir" value="目录名称,最后不含斜杠"/>
        /// </AppSettings>
        /// </summary>
        private static string path = ConfigurationManager.AppSettings["LogDir"];
        private static string PathFile = null;
        private static StreamWriter sw = null;

        // Methods
        public Logs()
        {
            //path = Directory.GetCurrentDirectory();
            if (!Directory.Exists(path + @"/logs"))
            {
                Directory.CreateDirectory(path + @"/logs");
            }
        }

        public Logs(string _path)
        {
            path = _path;
            if (!Directory.Exists(path + @"/logs"))
            {
                Directory.CreateDirectory(path + @"/logs");
            }
        }

        private static void CheckXmlFile()
        {
            PathFile = Directory.GetCurrentDirectory() + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".xml";
            if (!File.Exists(PathFile))
            {
                XmlTextWriter writer = new XmlTextWriter(path, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.WriteStartDocument();
                writer.WriteStartElement("Logs");
                writer.WriteEndElement();
                writer.WriteEndDocument();
                writer.Close();
            }
        }

        public static void writeLog(string message)
        {
            sw = new StreamWriter(path + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log", true, Encoding.GetEncoding("GB2312"), 0x400);
            sw.AutoFlush = true;
            sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "/t" + message);
            sw.Close();
        }

        public static void writeLog(StringBuilder message)
        {
            sw = new StreamWriter(path + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log", true, Encoding.GetEncoding("GB2312"), 0x400);
            sw.AutoFlush = true;
            sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "/t" + message.ToString());
            sw.Close();
        }

        public static void writeLog(string message, bool flag)
        {
            if (flag)
            {
                sw = new StreamWriter(path + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log", true, Encoding.GetEncoding("GB2312"), 0x400);
                sw.AutoFlush = true;
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "/t" + message);
            }
            else
            {
                sw = new StreamWriter(path + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + "_err.log", true, Encoding.GetEncoding("GB2312"), 0x400);
                sw.AutoFlush = true;
                sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "/t" + message);
            }
            sw.Close();
        }

        public static void writeLog(string message, string file)
        {
            sw = new StreamWriter(path + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + "_" + file + ".log", true, Encoding.GetEncoding("GB2312"), 0x400);
            sw.AutoFlush = true;
            sw.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "/t" + message);
            sw.Close();
        }

        public static void writeLog(string ElementName, string InnerText, FileFormat Fileformat)
        {
            if (Fileformat == FileFormat.Xml)
            {
                CheckXmlFile();
                PathFile = Directory.GetCurrentDirectory() + @"/logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".xml";
                XmlDocument document = new XmlDocument();
                document.Load(path);
                XmlNode node = document.SelectSingleNode("Logs");
                XmlElement newChild = document.CreateElement(ElementName);
                newChild.SetAttribute("Time", DateTime.Now.ToString("HH:mm:ss"));
                newChild.InnerText = InnerText;
                node.AppendChild(newChild);
                FileStream w = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                XmlTextWriter writer = new XmlTextWriter(w, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                document.WriteContentTo(writer);
                writer.Close();
                w.Close();
            }
        }

        // Nested Types
        public enum FileFormat
        {
            Text,
            Xml
        }
    }
}

 

 

 

 

using System;
using System.Collections.Generic;
using System.Text;

namespace Dingy.Log
{
    public class LogInfo
    {
        //Fields
        private string _Content;
        private string _File;

        //Properties
        public string Content
        {
            get
            {
                return _Content;
            }
            set
            {
                _Content = value;
            }
        }

        public string File
        {
            get
            {
                return _File;
            }
            set
            {
                _File = value;
            }
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Timers;

namespace Dingy.Log
{
    public class DealLog
    {
        // Fields
        public static ArrayList AlLog = new ArrayList(0x1f);
        private Logs logit;
        private Timer timer;

        // Methods
        public DealLog()
        {
            this.timer = new Timer();
            this.logit = new Logs();
            this.timer.Interval = 300.0;
            this.timer.Elapsed += new ElapsedEventHandler(this.timer_Elapsed);
            this.timer.Start();
        }

        public DealLog(string path)
        {
            this.timer = new Timer();
            this.logit = new Logs(path);
            this.timer.Interval = 300.0;
            this.timer.Elapsed += new ElapsedEventHandler(this.timer_Elapsed);
            this.timer.Start();
        }

        public static void AddToArray(string Content)
        {
            lock (AlLog)
            {
                LogInfo info = new LogInfo();
                info.Content = Content;
                info.File = string.Empty;
                AlLog.Add(info);
            }
        }

        public static void AddToArray(string Content, string File)
        {
            lock (AlLog)
            {
                LogInfo info = new LogInfo();
                info.Content = Content;
                info.File = File;
                AlLog.Add(info);
            }
        }

        public void Stop()
        {
            this.WriteLog();
            this.timer.Stop();
        }

        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            this.WriteLog();
        }

        private void WriteLog()
        {
            try
            {
                if (AlLog.Count > 0)
                {
                    lock (AlLog)
                    {
                        for (int i = 0; i < AlLog.Count; i++)
                        {
                            LogInfo info = (LogInfo)AlLog[i];
                            if (info.File == string.Empty)
                            {
                                Logs.writeLog(info.Content);
                            }
                            else
                            {
                                Logs.writeLog(info.Content, info.File);
                            }
                        }
                        AlLog.Clear();
                    }
                }
            }
            catch (Exception exception)
            {
                Logs.writeLog(exception.Message, "写入日志信息时错误!");
            }
        }
    }
}

 

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值