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, "写入日志信息时错误!");
}
}
}
}