📄 log.cs
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace WebHelper.Logs
{
/// <summary>
/// 日志
/// </summary>
public class Log:IDisposable
{
public Log(Model.CustomService service)
{
_service = service;
this.Initialize();
}
private void Initialize()
{
path = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["Path"], string.Format("{0}\\{1}{2}.xml", DateTime.Now.ToString("yyyyMM"), DateTime.Now.ToString("yyyyMMdd"), this.Service.CustomServiceID));
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(path));
if (!di.Exists) di.Create();
_xmlDoc = new XmlDocument();
if (File.Exists(path))
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
_xmlDoc.Load(fs);
}
}
else
{
using (FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read))
{
_xmlDoc.AppendChild(_xmlDoc.CreateXmlDeclaration("1.0", "Utf-8", null));
XPathNavigator navigator = _xmlDoc.CreateNavigator();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<Logs ServiceId='{0}' Title='{1}' TrueName='{2}' ClassId='{3}' Date='{4}'></Logs>"
, this.Service.CustomServiceID, this.Service.Title, this.Service.TrueName, this.Service.ClassID, DateTime.Now);
navigator.AppendChild(sb.ToString());
_xmlDoc.Save(fs);
fs.Flush();
}
}
_navigator = _xmlDoc.CreateNavigator();
}
~Log()
{
this.Dispose();
}
#region fields
private string path;
private System.Threading.Thread th, thSplit;
private Model.CustomService _service;
/// <summary>
/// 客服
/// </summary>
public Model.CustomService Service
{
get { return _service; }
}
private XmlDocument _xmlDoc;
/// <summary>
/// 日志Xml文档
/// </summary>
public XmlDocument XmlDoc
{
get { return _xmlDoc; }
}
private XPathNavigator _navigator;
/// <summary>
/// 日志导航
/// </summary>
public XPathNavigator Navigator
{
get { return _navigator; }
}
#endregion
/// <summary>
/// 写会话日志(如果是新客户必需调用NewCustomer创建会话)
/// </summary>
/// <param name="data">会话内容</param>
/// <param name="isService">是否是客服(true 客服 false非客服)</param>
/// <param name="guid">此次会话的Guid</param>
public bool Write(string data, bool isService, string guid)
{
try
{
lock (lockMark)
{
XmlNode node = this.XmlDoc.SelectSingleNode(string.Format("//Conversation[@Guid='{0}']", guid));
if (node == null) if ((node = this.CreateCustomerByLast(guid)) == null) return false;
XmlNode n = this.XmlDoc.CreateNode(XmlNodeType.Element, isService ? "Service" : "Customer", string.Empty);
XmlAttribute at = this.XmlDoc.CreateAttribute("Date");
at.Value = DateTime.Now.ToString();
n.Attributes.Append(at);
XmlNode value = this.XmlDoc.CreateNode(XmlNodeType.CDATA, string.Empty, string.Empty);
value.Value = data;
n.AppendChild(value);
node.AppendChild(n);
return true;
}
}
catch { return false; }
}
/// <summary>
/// 与一个新客户会话
/// </summary>
/// <param name="xml">客户提交的表单</param>
/// <param name="name">客户名字</param>
/// <param name="guid">此次会话的Guid</param>
public bool NewCustomer(string form, string name, string guid)
{
try
{
lock (newMark)
{
XPathNavigator _navigator = this.Navigator.SelectSingleNode(string.Format("//Conversation[@Guid='{0}']", guid));
if (_navigator == null)
{
_navigator = this.Navigator.SelectSingleNode("//Logs");
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<Conversation Guid='{0}' Customer='{1}'>{2}</Conversation>", guid, name, form);
_navigator.AppendChild(sb.ToString());
return true;
}
}
}
catch { }
return false;
}
/// <summary>
/// 根据最后的日志创建客户信息
/// </summary>
private XmlNode CreateCustomerByLast(string guid)
{
try
{
DateTime lastDate = DateTime.Now.AddDays(-1);
string lastPath = Path.Combine(System.Configuration.ConfigurationManager.AppSettings["Path"], string.Format("{0}\\{1}{2}.xml", lastDate.ToString("yyyyMM"), lastDate.ToString("yyyyMMdd"), this.Service.CustomServiceID));
if (!File.Exists(lastPath)) return null;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(lastPath);
XPathNavigator nav = xmlDoc.CreateNavigator();
XPathNavigator _nav = nav.SelectSingleNode(string.Format("//Conversation[@Guid='{0}']", guid));
if (_nav != null)
{
string customerName = _nav.GetAttribute("Customer", string.Empty);
string xml=string.Empty;
if (_nav.MoveToChild("Form", string.Empty)) xml = _nav.InnerXml;
this.NewCustomer(xml, customerName, guid);
return this.XmlDoc.SelectSingleNode(string.Format("//Conversation[@Guid='{0}']", guid));
}
}
catch { }
return null;
}
private object lockMark = new object();
private object newMark = new object();
private object saveMark = new object();
/// <summary>
/// 保存日志
/// </summary>
public void Save()
{
lock (saveMark)
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
this.XmlDoc.Save(fs);
}
}
}
#region IDisposable 成员
public void Dispose()
{
this.Save();
this._xmlDoc = null;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -