日志管理

一个简单的日志管理,日志清理部分并未实测

class Log4c
{
    #region 单例
    public static Log4c instance = new Log4c();
    private Log4c() { }
    #endregion

    #region 写操作监控 INFO
    object _lock4info = new object();
    public void Info(String infomsg)
    {
        try
        {
            lock (_lock4info)
            {
                string strPath = Application.StartupPath + @"\Log\" + System.DateTime.Now.ToString("yyyy-MM-dd") + "\\" + System.DateTime.Now.ToString("yyyyMMddHH") + ".log";
                if (Directory.Exists(Application.StartupPath + @"\Log\" + System.DateTime.Now.ToString("yyyy-MM-dd")) == false)
                {
                    Directory.CreateDirectory(Application.StartupPath + @"\Log\" + System.DateTime.Now.ToString("yyyy-MM-dd"));
                }
                if (!File.Exists(strPath))
                {
                    using (FileStream fs = File.Create(strPath))
                    {
                        StringBuilder sbmsg = new StringBuilder();
                        sbmsg.Append("************************************************\r\n");
                        sbmsg.Append("描述:大家好!这是操作监控日志文件!\r\n");
                        sbmsg.Append("创建时间:" + DateTime.Now.ToString() + "\r\n");
                        sbmsg.Append("************************************************\r\n");
                        for (int i = 0; i < 2; i++)
                        {
                            sbmsg.Append("        \r\n");
                        }
                        Byte[] byteInfo = new UTF8Encoding(true).GetBytes(sbmsg.ToString());
                        fs.Write(byteInfo, 0, byteInfo.Length);
                        fs.Close();
                    }
                }
                using (FileStream fs = File.Open(strPath, FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    StringBuilder sbmsg = new StringBuilder();
                    sbmsg.Append("-----------" + "发生时间:" + DateTime.Now.ToString() + "----------" + "\r\n");
                    sbmsg.Append("操作记录:" + infomsg + "\r\n");
                    //sbmsg.Append("------------------------------------------------\r\n");
                    for (int i = 0; i < 2; i++)
                    {
                        sbmsg.Append("        \r\n");
                    }
                    Byte[] byteInfo = new UTF8Encoding(true).GetBytes(sbmsg.ToString());
                    fs.Write(byteInfo, 0, byteInfo.Length);
                    fs.Close();
                }
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    #region 写异常监控 ERROR
    object _lock4error = new object();
    public void Error(String errormsg, Exception exception)
    {
        try
        {
            lock (_lock4error)
            {
                string strPath = Application.StartupPath + @"\Error\" + System.DateTime.Now.ToString("yyyy-MM-dd") + "\\" + System.DateTime.Now.ToString("yyyyMMddHH") + ".log";
                if (Directory.Exists(Application.StartupPath + @"\Error\" + System.DateTime.Now.ToString("yyyy-MM-dd")) == false)
                {
                    Directory.CreateDirectory(Application.StartupPath + @"\Error\" + System.DateTime.Now.ToString("yyyy-MM-dd"));
                }
                if (!File.Exists(strPath))
                {
                    // Create the file.
                    using (FileStream fs = File.Create(strPath))
                    {
                        StringBuilder sbmsg = new StringBuilder();
                        sbmsg.Append("************************************************\r\n");
                        sbmsg.Append("描述:大家好!这是异常监控日志文件!\r\n");
                        sbmsg.Append("创建时间:" + DateTime.Now.ToString() + "\r\n");
                        sbmsg.Append("************************************************\r\n");
                        for (int i = 0; i < 2; i++)
                        {
                            sbmsg.Append("        \r\n");
                        }
                        Byte[] byteInfo = new UTF8Encoding(true).GetBytes(sbmsg.ToString());
                        fs.Write(byteInfo, 0, byteInfo.Length);
                        fs.Close();
                    }
                }
                using (FileStream fs = File.Open(strPath, FileMode.Append, FileAccess.Write, FileShare.None))
                {
                    StringBuilder sbmsg = new StringBuilder();
                    sbmsg.Append("***********" + "发生时间:" + DateTime.Now.ToString() + "***********" + "\r\n");
                    sbmsg.Append("描述:" + errormsg + "\r\n");
                    sbmsg.Append("异常:" + exception.ToString() + "\r\n");
                    sbmsg.Append("************************************************\r\n");
                    for (int i = 0; i < 2; i++)
                    {
                        sbmsg.Append("        \r\n");
                    }
                    Byte[] byteInfo = new UTF8Encoding(true).GetBytes(sbmsg.ToString());
                    fs.Write(byteInfo, 0, byteInfo.Length);
                    fs.Close();
                }
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
    #endregion

    #region 日志清理
    private Thread cleanerThread;
    public void StartCleaner()
    {
        cleanerThread = new Thread(new ThreadStart(this.clean));
    }
    private void clean()
    {
        string strPath = Application.StartupPath + @"\Log";
        while (true)
        {
            if (!Directory.Exists(strPath))
            {
                Directory.CreateDirectory(strPath);
            }
            DirectoryInfo info = new DirectoryInfo(strPath);
            DirectoryInfo[] infos = info.GetDirectories();
            for (int i = 0; i < infos.Length; i++)
            {
                DateTime dt = DateTime.ParseExact(infos[i].Name, "yyyy-MM-dd", null);
                TimeSpan ts = DateTime.Now - dt;
                double minus = ts.TotalDays;
                if (minus >= 7)
                {
                    infos[i].Delete(true);
                }
            }
            Thread.Sleep(60 * 60 * 1000);
        }
    }
    public void StopCleaner()
    {
        cleanerThread.Abort();
        cleanerThread = null;
    }
    public Boolean IsCleanerWorking()
    {
        if (cleanerThread != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
}

 

 

 

posted @ 2013-03-26 11:45  苍云古齿  阅读(163)  评论(0)    收藏  举报