?? impslogger.cs
字號(hào):
?namespace Imps.Client.Logger
{
using Imps.Client.Base;
using Imps.Client.Core;
using Imps.Client.Pc;
using Imps.Client.Utils;
using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
public class ImpsLogger : IImpsLogger
{
private volatile bool _CreateNewLogFile = true;
private Regex _ExtRegex = new Regex(@"<ExtData\s+(.*?)>(.*?)</ExtData>", RegexOptions.Compiled | RegexOptions.Multiline);
private IFrameworkWindow _host;
private bool _IsLogBiz = true;
private bool _IsLogConnection = true;
private bool _IsLogException = true;
private bool _IsLogGeneral = true;
private bool _IsLogInstall = true;
private bool _IsLogLiveUpdate = true;
private bool _IsLogLogin = true;
private bool _IsLogSipc = true;
private string _LogFile;
private Hashtable _LogFileNames = new Hashtable();
private string _LogFolder;
private Imps.Client.Logger.LogHeader _LogHeader = new Imps.Client.Logger.LogHeader();
private Imps.Client.Utils.LogType _LogType = (Imps.Client.Utils.LogType.SingleFile | Imps.Client.Utils.LogType.GlobalFile);
private int _MaxLogNo = 9;
private static string _processId = string.Empty;
private string _ServerUrl = string.Empty;
private int _ThresholdLevel;
private const string FileName = "Imps";
private const int PostMaxSize = 0x800;
public ImpsLogger(IFrameworkWindow host)
{
this._host = host;
this.LogHeader.TimeStamp = DateTime.Now;
this.LogHeader.Version = "1.0";
this.LogHeader.MachineEnv.OS = Environment.OSVersion.ToString();
this.LogHeader.MachineEnv.Language = CultureInfo.CurrentCulture.Name;
this.LogHeader.MachineEnv.IE = string.Empty;
this.LogHeader.MachineEnv.MemorySize = GetMemorySize();
this.LogHeader.MachineEnv.VideoCard = GetVideoCard();
}
private string CombineFileName(int no)
{
return Path.Combine(this.LogFolder, "Imps." + no.ToString("000") + ".log");
}
public void CreateLogFile()
{
if (this._CreateNewLogFile)
{
if (!Directory.Exists(this.LogFolder))
{
Directory.CreateDirectory(this.LogFolder);
}
string str = string.Empty;
for (int i = 1; i <= this.MaxLogNo; i++)
{
if (!System.IO.File.Exists(str = this.CombineFileName(i)))
{
break;
}
str = string.Empty;
}
if (string.IsNullOrEmpty(str))
{
for (int j = 2; j <= this.MaxLogNo; j++)
{
string path = this.CombineFileName(j);
string str3 = this.CombineFileName(j - 1);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(str3);
System.IO.File.Move(path, str3);
}
}
str = this.CombineFileName(this.MaxLogNo);
}
using (StreamWriter writer = new StreamWriter(str, false, Encoding.UTF8))
{
writer.WriteLine(this.LogHeader.ToString());
}
this._CreateNewLogFile = false;
this._LogFile = str;
}
}
private string GetCallStack()
{
StackTrace trace = new StackTrace(2, true);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
if (i != 0)
{
builder.Append(" ");
}
builder.Append(frame.GetMethod());
string fileName = frame.GetFileName();
if (!string.IsNullOrEmpty(fileName))
{
builder.Append("; " + fileName);
builder.Append(": " + frame.GetFileLineNumber());
}
builder.AppendLine();
}
return builder.ToString();
}
private string GetLogFileNameByCategory(string category)
{
category = category.ToLower();
if (!this._LogFileNames.ContainsKey(category))
{
string path = Path.Combine(this.LogFolder, category + ".log");
using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
{
writer.WriteLine(this.LogHeader.ToString());
}
this._LogFileNames.Add(category, path);
}
return (this._LogFileNames[category] as string);
}
private static uint GetMemorySize()
{
MEMORY_INFO meminfo = new MEMORY_INFO();
GlobalMemoryStatus(ref meminfo);
return meminfo.dwTotalPhys;
}
private static string GetVideoCard()
{
return string.Empty;
}
[DllImport("kernel32")]
private static extern void GlobalMemoryStatus(ref MEMORY_INFO meminfo);
private bool IsLog(string category, int infoLevel)
{
if ((this._host != null) && (this._host.AccountManager != null))
{
User currentUser = this._host.AccountManager.CurrentUser;
if ((currentUser != null) && currentUser.Configuration.SystemSetting.RecordMyLog)
{
return true;
}
}
if (infoLevel >= this.ThresholdLevel)
{
switch (category)
{
case "General":
return this.IsLogGeneral;
case "Biz":
return this.IsLogBiz;
case "Connection":
return this.IsLogConnection;
case "LogIn":
return this.IsLogLogin;
case "Sipc":
return this.IsLogSipc;
case "Exception":
return this.IsLogException;
}
}
return false;
}
private bool IsNeedCallStack(string category, int level)
{
bool flag = ((level == 20) || (level == 0x63)) || (level == 30);
string str = category;
if (str == null)
{
return flag;
}
if (!(str == "Exception"))
{
if (str == "Sipc")
{
return false;
}
return flag;
}
return true;
}
private void PostFile(ImpsLoggerSender sender, string logFile)
{
if (System.IO.File.Exists(logFile))
{
byte[] buffer = new byte[0x800];
using (FileStream stream = new FileStream(logFile, FileMode.Open))
{
int num;
while ((num = stream.Read(buffer, 0, buffer.Length)) > 0)
{
sender.Post(buffer, 0, num);
}
}
}
}
public void SendToServer(Imps.Client.Utils.LogType logType)
{
if (logType != Imps.Client.Utils.LogType.None)
{
if (string.IsNullOrEmpty(this.ServerUrl))
{
throw new ApplicationException("未指定服務(wù)器URL");
}
ImpsLoggerSender sender = new ImpsLoggerSender(this._ServerUrl);
sender.Post(this.LogHeader.ToArray());
if ((logType | Imps.Client.Utils.LogType.GlobalFile) != Imps.Client.Utils.LogType.None)
{
this.PostFile(sender, this._LogFile);
}
if ((logType | Imps.Client.Utils.LogType.SingleFile) != Imps.Client.Utils.LogType.None)
{
foreach (string str in this._LogFileNames.Values)
{
this.PostFile(sender, str);
}
}
sender.PostEnd();
}
}
private string TrimValue(string value)
{
if (value.Length > 1)
{
if ((value[0] == '"') && (value[value.Length - 1] == '"'))
{
return value.Substring(1, value.Length - 2);
}
if ((value[0] == '\'') && (value[value.Length - 1] == '\''))
{
return value.Substring(1, value.Length - 2);
}
}
return value;
}
public void WriteLine(string category, int infoLevel, string summary, string detail, string ext)
{
if (this.IsLog(category, infoLevel))
{
try
{
if (this._CreateNewLogFile && ((this.LogType & Imps.Client.Utils.LogType.GlobalFile) != Imps.Client.Utils.LogType.None))
{
this.CreateLogFile();
}
if (this.LogType != Imps.Client.Utils.LogType.None)
{
MemoryStream w = new MemoryStream();
using (XmlTextWriter writer = new XmlTextWriter(w, Encoding.UTF8))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("Record");
writer.WriteAttributeString("pid", CurrentProcessId);
writer.WriteAttributeString("timestamp", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
writer.WriteAttributeString("level", infoLevel.ToString());
writer.WriteAttributeString("category", category);
writer.WriteStartElement("Summary");
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -