⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 impslogger.cs

📁 破解的飞信源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
namespace Imps.Client.Logger
{
    using Imps.Client.Base;
    using Imps.Client.Core;
    using Imps.Client.Pc;
    using Imps.Client.Utils;
    using Imps.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 path = string.Empty;
                for (int i = 1; i <= this.MaxLogNo; i++)
                {
                    if (!File.Exists(path = this.CombineFileName(i)))
                    {
                        break;
                    }
                    path = string.Empty;
                }
                if (string.IsNullOrEmpty(path))
                {
                    for (int j = 2; j <= this.MaxLogNo; j++)
                    {
                        string text2 = this.CombineFileName(j);
                        string text3 = this.CombineFileName(j - 1);
                        if (File.Exists(text2))
                        {
                            File.Delete(text3);
                            File.Move(text2, text3);
                        }
                    }
                    path = this.CombineFileName(this.MaxLogNo);
                }
                using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8))
                {
                    writer.WriteLine(this.LogHeader.ToString());
                }
                this._CreateNewLogFile = false;
                this._LogFile = path;
            }
        }

        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;
            switch (category)
            {
                case "Exception":
                    return true;

                case "Sipc":
                    return false;
            }
            return flag;
        }

        private void PostFile(ImpsLoggerSender sender, string logFile)
        {
            if (File.Exists(logFile))
            {
                byte[] buffer = new byte[0x800];
                using (FileStream stream = new FileStream(logFile, FileMode.Open))
                {
                    int length;
                    while ((length = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        sender.Post(buffer, 0, length);
                    }
                }
            }
        }

        public void SendToServer(Imps.Client.Utils.LogType logType)
        {
            if (logType != Imps.Client.Utils.LogType.None)
            {
                if (string.IsNullOrEmpty(this.ServerUrl))
                {
                    throw new ApplicationException("未指定服务器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 text in this._LogFileNames.Values)
                    {
                        this.PostFile(sender, text);
                    }
                }
                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");
                            writer.WriteString(summary);
                            writer.WriteEndElement();
                            writer.WriteStartElement("Detail");
                            if (detail.IndexOfAny("<>".ToCharArray()) >= 0)
                            {
                                writer.WriteCData(detail.Replace("]]>", "?]>"));
                            }
                            else
                            {
                                writer.WriteString(detail);
                            }
                            writer.WriteEndElement();
                            writer.WriteStartElement("CallStack");
                            writer.WriteCData(this.IsNeedCallStack(category, infoLevel) ? DebugHelper.GetCallStackString(1) : string.Empty);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -