马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

c#写日志类库【二尸兄】

  [复制链接]
查看7008 | 回复5 | 2014-9-22 10:42:52 | 显示全部楼层 |阅读模式
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Reflection;

namespace CoreMes.Commom
{
    public class SysLog
    {
        public delegate void ExceptionHandler(string title, string info, bool IsTerminating);
        private static bool bool_0;
        private static bool bool_1;
        private static string _logpath;
        private static string _timeformat = "HH:mm:ss,fff";
        private static string apppath = null;
        private static Queue<string> queue_log = new Queue<string>();

        public static event ExceptionHandler NewLog;

        public static void BindExceptionLog()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(SysLog.smethod_0);
            Application.ThreadException += new ThreadExceptionEventHandler(SysLog.smethod_1);
        }

        public static void RemoveExceptionLog()
        {
            AppDomain.CurrentDomain.UnhandledException -= new UnhandledExceptionEventHandler(SysLog.smethod_0);
            Application.ThreadException -= new ThreadExceptionEventHandler(SysLog.smethod_1);
        }

        private static void smethod_0(object sender, UnhandledExceptionEventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                Exception exception = null;
                for (exception = (Exception)e.ExceptionObject; exception != null; exception = exception.InnerException)
                {
                    builder.AppendFormat("\r\n{0}\r\n{1}\r\n{2}", exception.GetType().FullName, exception.Message, exception.StackTrace);
                }
            }
            catch
            {
                builder.Append("\r\n记录本条日志时也出现异常,导致捕获的信息不完整...");
            }
            smethod_2("发现应用程序域中未被捕获的异常", builder.ToString());
            bool_0 = true;
            while (queue_log.Count > 0)
            {
                smethod_4(null);
                Thread.Sleep(100);
            }
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private static void smethod_1(object sender, ThreadExceptionEventArgs e)
        {
            smethod_2("发现未被捕获的异常", string.Format("{0}\r\n{1}\r\n{2}", e.Exception.GetType().FullName, e.Exception.Message, e.Exception.StackTrace));
            bool_0 = true;
            while (queue_log.Count > 0)
            {
                smethod_4(null);
                Thread.Sleep(100);
            }
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private static void smethod_2(string string_2, string string_3)
        {
            smethod_3(DateTime.Now, string_2, string_3);
            smethod_5(string_2, string_3, true);
        }

        private static void smethod_3(DateTime dateTime_0, string string_2, string string_3)
        {
            if (!bool_0)
            {
                string str;
                Monitor.Enter(str = "Lock");
                try
                {
                    queue_log.Enqueue("[" + dateTime_0.ToString(TimeFormat) + "]  " + string_2 + "  " + string_3 + Environment.NewLine);
                }
                catch
                {
                }
                finally
                {
                    Monitor.Exit(str);
                }
            }
        }

        private static void smethod_4(object object_0)
        {
            if (!bool_1)
            {
                bool_1 = true;
                string path = LogPath + @"\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
                try
                {
                    using (StreamWriter writer = new StreamWriter(path, true, Encoding.Default))
                    {
                        while (queue_log.Count > 0)
                        {
                            string str2 = queue_log.Peek();
                            try
                            {
                                writer.Write(str2);
                                queue_log.Dequeue();
                                continue;
                            }
                            catch
                            {
                                break;
                            }
                        }
                        writer.Close();
                        writer.Dispose();
                    }
                }
                catch
                {
                }
                bool_1 = false;
            }
        }

        private static void smethod_5(string string_2, string string_3, bool bool_2)
        {
            if (NewLog != null)
            {
                NewLog(string_2, string_3, bool_2);
            }
        }

        public static string GetAppPath()
        {
            if (apppath == null)
            {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                string location = executingAssembly.Location;
                apppath = location.Remove(location.Length - executingAssembly.ManifestModule.Name.Length);
            }
            return apppath;
        }

       
        public static void WriteLog(string title, string info)
        {
            smethod_3(DateTime.Now, title, info);
            ThreadPool.QueueUserWorkItem(new WaitCallback(SysLog.smethod_4));
            smethod_5(title, info, false);
        }

        public static string LogPath
        {
            get
            {
                if ((_logpath == null) || (_logpath.Length == 0))
                {
                    _logpath = GetAppPath() + "SysLog";
                }
                if (!Directory.Exists(_logpath))
                {
                    Directory.CreateDirectory(_logpath);
                }
                return _logpath;
            }
            set
            {
                _logpath = value;
            }
        }

        public static string TimeFormat
        {
            get
            {
                return _timeformat;
            }
            set
            {
                _timeformat = value;
            }
        }

    }
}

功能就是把信息写到本文文件中。
天生我材,必有用!
桂林一枝花 | 2014-9-22 10:45:12 | 显示全部楼层
此帖仅作者可见

使用道具 举报

min | 2014-9-22 11:32:04 | 显示全部楼层
此帖仅作者可见

使用道具 举报

hcbin | 2014-11-12 19:58:24 | 显示全部楼层
此帖仅作者可见

使用道具 举报

Chinared | 2014-12-29 10:48:46 | 显示全部楼层
此帖仅作者可见

使用道具 举报

雳伏雄明 | 2016-8-23 14:38:02 | 显示全部楼层
此帖仅作者可见

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则