251 lines
7.2 KiB
C#
251 lines
7.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Runtime.CompilerServices;
|
|||
|
|
|
|||
|
|
namespace EAS.Distributed
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 文本文件日志记录器。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <remarks>
|
|||
|
|
/// 日志输出到文本文件。
|
|||
|
|
/// </remarks>
|
|||
|
|
class TextLogger:IDisposable
|
|||
|
|
{
|
|||
|
|
private string path = "logs";
|
|||
|
|
private bool m_PathOK = false;
|
|||
|
|
|
|||
|
|
#region 公共属性
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 日志路径。
|
|||
|
|
/// </summary>
|
|||
|
|
public string Path
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return this.path;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
this.path = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
void Initialize()
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 文件名称。
|
|||
|
|
/// </summary>
|
|||
|
|
public string FileName
|
|||
|
|
{
|
|||
|
|
get;
|
|||
|
|
set;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region 求基础路径
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 求日志记录的基础路径。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
string GetBasePath()
|
|||
|
|
{
|
|||
|
|
return XUpdate.Instance.GetBaseDirectory();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region ILogger 成员
|
|||
|
|
|
|||
|
|
public void Info(string message)
|
|||
|
|
{
|
|||
|
|
if (message == null) return;
|
|||
|
|
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
sb.AppendLine(string.Format("{0},Info - {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
|
|||
|
|
this.AppendLineText(sb);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Warn(string message)
|
|||
|
|
{
|
|||
|
|
if (message == null) return;
|
|||
|
|
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
sb.AppendLine(string.Format("{0},Warn - {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
|
|||
|
|
this.AppendLineText(sb);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Debug(string message)
|
|||
|
|
{
|
|||
|
|
if (message == null) return;
|
|||
|
|
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
sb.AppendLine(string.Format("{0},Debug - {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
|
|||
|
|
//sb.AppendLine();
|
|||
|
|
this.AppendLineText(sb);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Error(string message)
|
|||
|
|
{
|
|||
|
|
if (message == null) return;
|
|||
|
|
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
sb.AppendLine(string.Format("{0},Error - {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message));
|
|||
|
|
//sb.AppendLine();
|
|||
|
|
this.AppendLineText(sb);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Error(Exception exc)
|
|||
|
|
{
|
|||
|
|
if (exc == null) return;
|
|||
|
|
|
|||
|
|
System.Exception exc2 = exc;
|
|||
|
|
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
long ml = GC.GetTotalMemory(false);
|
|||
|
|
double memory = (double)ml / (1024 * 1024);
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
|
|||
|
|
if (exc2 is System.Reflection.TargetInvocationException)
|
|||
|
|
{
|
|||
|
|
//continue;
|
|||
|
|
}
|
|||
|
|
else if (exc2 is System.Reflection.TargetParameterCountException)
|
|||
|
|
{
|
|||
|
|
//continue;
|
|||
|
|
}
|
|||
|
|
else if (exc2 is System.Reflection.TargetException)
|
|||
|
|
{
|
|||
|
|
//continue;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
sb.AppendLine("[类型]:Error");
|
|||
|
|
sb.AppendLine("[线程]:" + Thread.CurrentThread.ManagedThreadId.ToString());
|
|||
|
|
sb.AppendLine("[内存]:" + memory.ToString("F2") + "M");
|
|||
|
|
sb.AppendLine("[时间]:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|||
|
|
sb.AppendLine("[内容]:" + exc2.Message);
|
|||
|
|
sb.AppendLine("[来源]:" + exc2.Source);
|
|||
|
|
sb.AppendLine("[目标]:" + exc2.TargetSite);
|
|||
|
|
sb.AppendLine("[堆栈]:" + exc2.StackTrace);
|
|||
|
|
this.AppendLineText(sb);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (exc2.InnerException == null)
|
|||
|
|
break;
|
|||
|
|
exc2 = exc2.InnerException;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 2013-09-30,多线程排列写日志会导致页面转向之后会话和应用结束和重启,shit,Fuck iis。
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="sb"></param>
|
|||
|
|
/// <param name="logType"></param>
|
|||
|
|
[MethodImpl(MethodImplOptions.Synchronized)]
|
|||
|
|
void AppendLineText(StringBuilder sb)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (StreamWriter writer = this.GetStreamWriter())
|
|||
|
|
{
|
|||
|
|
lock (writer)
|
|||
|
|
{
|
|||
|
|
writer.WriteLine(sb);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (System.Exception exc)
|
|||
|
|
{
|
|||
|
|
//
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
StreamWriter GetStreamWriter()
|
|||
|
|
{
|
|||
|
|
StreamWriter m_Writer = null;
|
|||
|
|
|
|||
|
|
string m_Directory = this.Path;
|
|||
|
|
|
|||
|
|
#region 处理路径
|
|||
|
|
|
|||
|
|
if (!this.m_PathOK)
|
|||
|
|
{
|
|||
|
|
if (m_Directory.StartsWith("..\\"))
|
|||
|
|
{
|
|||
|
|
string m_Path = this.GetBasePath();
|
|||
|
|
while (true)
|
|||
|
|
{
|
|||
|
|
m_Path = Directory.GetParent(m_Path).ToString();
|
|||
|
|
m_Directory = m_Directory.Substring(3, m_Directory.Length - 3);
|
|||
|
|
if (!m_Directory.StartsWith("..\\"))
|
|||
|
|
{
|
|||
|
|
m_Directory = System.IO.Path.Combine(m_Path, m_Directory);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (this.Path.IndexOf(":") < 0)
|
|||
|
|
{
|
|||
|
|
string m_Path = this.GetBasePath();
|
|||
|
|
m_Directory = System.IO.Path.Combine(m_Path, this.Path);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (m_Directory.StartsWith("file:\\"))
|
|||
|
|
{
|
|||
|
|
m_Directory = m_Directory.Substring(6, m_Directory.Length - 6);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.path = m_Directory;
|
|||
|
|
this.m_PathOK = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
if (!Directory.Exists(m_Directory))
|
|||
|
|
Directory.CreateDirectory(m_Directory);
|
|||
|
|
|
|||
|
|
//升级日志。
|
|||
|
|
m_Directory = System.IO.Path.Combine(m_Directory, "update");
|
|||
|
|
if (!Directory.Exists(m_Directory))
|
|||
|
|
Directory.CreateDirectory(m_Directory);
|
|||
|
|
|
|||
|
|
//当天记录。
|
|||
|
|
if (!string.IsNullOrEmpty(this.FileName))
|
|||
|
|
{
|
|||
|
|
m_Directory = System.IO.Path.Combine(m_Directory,string.Format("{0}.log",this.FileName));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
m_Directory = System.IO.Path.Combine(m_Directory, string.Format("{0}.log",DateTime.Now.ToString("yyyyMMdd")));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (File.Exists(m_Directory))
|
|||
|
|
m_Writer = File.AppendText(m_Directory);
|
|||
|
|
else
|
|||
|
|
m_Writer = File.CreateText(m_Directory);
|
|||
|
|
return m_Writer;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region IDisposable 成员
|
|||
|
|
|
|||
|
|
void IDisposable.Dispose()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|