代码改变世界

c# 写入文件日志

2010-03-24 08:45  zhangbaoyu  阅读(420)  评论(0编辑  收藏  举报

public static void WriteLog(Model.ModelLog ModelLog)
        {
            DbAcess dba = new DbAcess();

 

            try
            {
                //string logFileName =Path.GetFullPath("Log/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString().ToString() + DateTime.Now.Day.ToString() + ".log");
                Random random = new Random();
                DataTable dt = GetCodeInfo(dba, "37", "0");
                string logFolder = ModelLog.RootPath + dt.Rows[0]["CODE_NAME2"].ToString() + "\\" + FormatDateTime(DateTime.Now, Constant.DATETIME_FORMAT_0005) + "\\" + ModelLog.FormName + "\\" + ModelLog.AppCode + "\\";
                string logFileName = logFolder + FormatDateTime(DateTime.Now, Constant.DATETIME_FORMAT_0007) + Convert.ToString(random.Next(100000, 999999)) + ".log";

                if (!Directory.Exists(logFolder))
                {
                    Directory.CreateDirectory(logFolder);
                }

                FileInfo fileinfo = new FileInfo(logFileName);

                string logTime = DateTime.Now.Year.ToString() + "/" + PadddingLeft(DateTime.Now.Month.ToString(), "0", 2) + "/" + PadddingLeft(DateTime.Now.Day.ToString(), "0", 2) + " " + PadddingLeft(DateTime.Now.Hour.ToString(), "0", 2) + PadddingLeft(DateTime.Now.Minute.ToString(), "0", 2) + PadddingLeft(DateTime.Now.Second.ToString(), "0", 2);
                string levelName = "";

                switch (ModelLog.LogLevel)
                {
                    case Constant.LogLevel_INFO:
                        levelName = "INFO";
                        break;
                    case Constant.LogLevel_WARNING:
                        levelName = "WARNING";
                        break;
                    case Constant.LogLevel_ERROR:
                        levelName = "ERROR";
                        break;
                    case Constant.LogLevel_CRITICAL:
                        levelName = "CRITICAL";
                        break;

                }

                if (!fileinfo.Exists)
                {
                    //fileinfo.Create();
                    FileStream fs = fileinfo.OpenWrite();
                    StreamWriter sw = new StreamWriter(fs);

                    try
                    {

                        sw.BaseStream.Seek(0, SeekOrigin.End);
                        sw.WriteLine(logTime + " " + ModelLog.LoginId + " " + ModelLog.UserName + " " + levelName + " " + ModelLog.DepartName + " " + ModelLog.LogContent);
                        sw.Flush();
                        sw.Close();
                    }
                    catch (Exception ex)
                    {
                        sw.Flush();
                        sw.Close();
                        throw ex;
                    }
                }
                else
                {
                    StreamWriter sw = fileinfo.AppendText();

                    try
                    {
                        sw.BaseStream.Seek(0, SeekOrigin.End);
                        sw.WriteLine(logTime + " " + ModelLog.LoginId + " " + ModelLog.UserName + " " + levelName + " " + ModelLog.DepartName + " " + ModelLog.LogContent);
                        sw.Flush();
                        sw.Close();
                    }
                    catch (Exception ex)
                    {
                        sw.Flush();
                        sw.Close();
                        throw ex;
                    }
                }

            }
            catch (Exception ex)
            {

                throw ex;
            }
            finally
            {
                dba.Dispose();
            }
        }