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

📄 logger.cs

📁 Gibphone is CSharp Program, it can tell you how to design p2p chat.
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace GPCore
{
    /// <summary>
    /// A static class used if you want to Log the activity of 
    /// something
    /// </summary>
    public static class Logger
    {
        private static String m_last;

        /// <summary>
        /// This contains the last message that was Logged using any of the <see cref="Logger"/>.Log methods, including timestamp.
        /// </summary>
        public static String LastMessageLogged
        {
            get { return m_last; }
            set { m_last = value; }
        }

        private static String logfile = "Log.log";
        /// <summary>
        /// The file to log system events in. Default is "Log.log"
        /// </summary>
        public static String LogFile
        {
            get { return logfile; }
            set { logfile = value; }
        }
        private static String directory = Directory.GetCurrentDirectory();
        /// <summary>
        /// The directory where the log file is located. Default is the current directory.
        /// </summary>
        public static String LogFileDirectory
        {
            get { return directory; }
            set { directory = value; }
        }

        /// <summary>
        /// Adds the string <paramref name="s"/> to the Log
        /// </summary>
        /// <param name="s">The string to add</param>
        /// <returns>Returns the message that was logged </returns>
        public static string Log(string s)
        {
            using (StreamWriter sw = new StreamWriter(Path.Combine(Directory.GetCurrentDirectory(), "Log.log"), true))
            {
                s = "[" + DateTime.Now.ToShortTimeString() + "]" + s;
                sw.WriteLine(s);
                sw.WriteLine();
            }
            return LastMessageLogged = s;
        }
        /// <summary>
        /// Adds the formated string <paramref name="s"/> to the Log
        /// </summary>
        /// <param name="s">The formated string</param>
        /// <param name="o">The objects to fill the format</param>
        /// <returns>Returns the message that was logged </returns>
        public static string Log(string s, params object[] o)
        {
            return Log(string.Format(s, o));
        }
        /// <summary>
        /// Logs an Exception including the inner exception
        /// </summary>
        /// <param name="e">The exception to Log</param>
        /// <returns>Returns the message that was logged </returns>
        public static string Log(Exception e)
        {
            return Log(e, true);
        }
        /// <summary>
        /// Logs an Exception
        /// </summary>
        /// <param name="e">The exception to Log</param>
        /// <param name="inner">Sets whether to read the inner exception as well</param>
        /// <returns>Returns the message that was logged </returns>
        public static string Log(Exception e, bool inner)
        {
            if (!inner)
            {
                return Log(ReadException(e));
            }
            else
            {
                return Log(ReadEntireException(e));
            }
        }
        /// <summary>
        /// Formats the content of an Exception to a string in 
        /// the format of e.Source: e.Message \n e.StackTrace
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public static String ReadException(Exception e)
        {
            StackTrace st = new StackTrace(e);
            StringBuilder sb = new StringBuilder();
            foreach (StackFrame f in st.GetFrames())
            {
                if (!f.GetMethod().Module.FullyQualifiedName.StartsWith("System."))
                {
                    sb.AppendFormat("\t at {0}", f.ToString());
                }
            }
            return String.Format("{0}: {1}{2}{3}", e.Source, e.Message, Environment.NewLine, e.StackTrace);
        }
        /// <summary>
        /// Adds the content of the Entire Exception to a string in 
        /// the format of <see cref="ReadException"/>
        /// </summary>
        /// <param name="e">The Exception to read</param>
        /// <returns>Returns the Formated Exception String</returns>
        public static string ReadEntireException(Exception e)
        {
            return String.Format("{0}{1}", ReadException(e), (e.InnerException == null ? "" : ReadEntireException(e.InnerException, 1)));
        }

        private static string ReadEntireException(Exception e, int depth)
        {
            return String.Format("{0}{1}{2}", Environment.NewLine, ReadException(e), (e.InnerException == null ? "" : ReadEntireException(e.InnerException, depth + 1)));
        }

        private static string Repeat(string s, int count)
        {
            StringBuilder sb = new StringBuilder(s.Length * count);
            for (int i = 0; i < count; i++)
            {
                sb.Append(s);
            }
            return sb.ToString();
        }
        private static string Repeat(char c, int count)
        {
            return new StringBuilder().Append(c, count).ToString();
        }
    }
}

⌨️ 快捷键说明

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