惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园_首页
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Hugging Face - Blog
Hugging Face - Blog
量子位
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
阮一峰的网络日志
阮一峰的网络日志
D
Docker
罗磊的独立博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
IT之家
IT之家
MyScale Blog
MyScale Blog
Microsoft Azure Blog
Microsoft Azure Blog

博客园 - 天轰穿

如何摆脱对强者的畏惧--心灵修炼三 如何直面自己的自卑--心灵修炼(二) 如何正确的面对所受到的伤害和不公平--心灵修炼(一) 27Where条件筛选数据-简单(必学)-天轰穿sqlserver视频教程 26数据查询的各种小玩法-select 下(必学)-天轰穿sqlserver视频教程 25数据查询的各种小玩法-select上(必学)-天轰穿sqlserver视频教程 24单行插入与批量插入-insert(必学)-天轰穿sqlserver视频教程 23创建与设计表-约束(下)-天轰穿大话数据库视频教程 22约束-主键与外键(必学)-天轰穿大话数据库视频教程 21数据表的维护(必学)-天轰穿大话数据库视频教程 20数据表的创建-分区表(选学)-天轰穿大话数据库视频教程 19数据表的创建-普通表&临时表-天轰穿大话数据库视频教程 18数据表&E-R模型&概念数据模型-下(选学)-天轰穿大话数据库视频教程 17数据表&E-R模型&概念数据模型上-选学天轰穿大话数据库视频教程 16数据库编程前置知识汇总(必学)-天轰穿大话数据库视频教程 15系统函数&数据类型转换(必学)-大话数据库视频教程 14内置函数(必学)-天轰穿大话数据库视频系列视频教程 13数据类型(下)(必学)-天轰穿大话数据库视频教程 12数据类型(上)(必学)-天轰穿大话数据库视频教程
loghelper.cs 代码
天轰穿 · 2015-07-15 · via 博客园 - 天轰穿

Posted on 2015-07-15 18:30  天轰穿  阅读(3338)  评论()    收藏  举报

唉,网上到处找一圈,真是麻烦,自己结合别人写的,重新整理一个。 这个破玩意最大的作用就是写微信那种没法顺利断点调试的程序的时候,在需要的地方写日志,然后去查看。真是回到当年用DW4写php的年代了,可惜一点怀旧的喜悦都没有,有的只是慢慢的无奈

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

/// <summary>
/// 日志文件存放文件夹分类枚举
/// </summary>
    public enum LogType
    {
        /// <summary>
        /// 普通信息
        /// </summary>
        Info,
        /// <summary>
        /// 错误
        /// </summary>
        Error,
        /// <summary>
        /// 其他全部信息
        /// </summary>
        Overall,
    }

    public class LogHelper
    {
        /// <summary>
        /// 日志存放路径
        /// </summary>
        public static string LogPath
        {
            get
            {
                return AppDomain.CurrentDomain.BaseDirectory + @"\log";
            }
        }

        /// <summary>
        /// 信息类型
        /// </summary>
        public enum LogLevel
        {
            /// <summary>
            /// 普通信息
            /// </summary>
            Info,
            /// <summary>
            /// 错误
            /// </summary>
            Error
        }

        /// <summary>
        /// 普通信息写入日志
        /// </summary>
        /// <param name="message"></param>
        /// <param name="logType"></param>
        public static void Info(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "", message);
        }
        /// <summary>
        /// 自定义错误信息写入
        /// </summary>
        /// <param name="message">自定义消息</param>
        /// <param name="logType">存储目录类型</param>
        public static void Error(string message, LogType logType = LogType.Overall)
        {
            if (string.IsNullOrEmpty(message))
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", message);
        }
        /// <summary>
        /// 程序异常信息写入
        /// </summary>
        /// <param name="e">异常</param>
        /// <param name="logType">存储目录类型</param>
        public static void Error(Exception e, LogType logType = LogType.Overall)
        {
            if (e == null)
                return;
            var path = string.Format(@"\{0}\", logType.ToString());
            WriteLog(path, "Error ", e.Message);
        }
        /// <summary>
        /// 写日志的最终执行动作
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="prefix">前缀</param>
        /// <param name="message">内容</param>
        public static void WriteLog(string path, string prefix, string message)
        {
            path = LogPath + path;
            var fileName = string.Format("{0}{1}.log", prefix, DateTime.Now.ToString("yyyyMMdd"));

            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);

            using (FileStream fs = new FileStream(path + fileName, FileMode.Append, FileAccess.Write,
                                                  FileShare.Write, 1024, FileOptions.Asynchronous))
            {
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("HH:mm:ss") + " " + message + "\r\n");
                IAsyncResult writeResult = fs.BeginWrite(buffer, 0, buffer.Length,
                    (asyncResult) =>
                    {
                        var fStream = (FileStream)asyncResult.AsyncState;
                        fStream.EndWrite(asyncResult);
                    },

                    fs);
                fs.Close();
            }
        }
    }