zoukankan      html  css  js  c++  java
  • c# 写入文件日志

    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();
                }
            }

  • 相关阅读:
    携程 去呼呼 Odoo SSO 单点登录
    Spring Boot 2.2 正式发布,大幅性能提升 + Java 13 支持 & Spring Data Moore M2
    全球顶尖AI技术 微软CRM X 销售易CRM Gartner魔力象限
    职场的真相——七句话
    东软 UniEAP SaCa DataViz & Report
    UML建模——活动图(Activity Diagram)
    领域驱动设计(DDD)部分核心概念
    阿里云RPA4.0背后的黑科技
    Open Source In-Memory Computing Platform
    Mac OS X ifconfig命令解释
  • 原文地址:https://www.cnblogs.com/tatsuya/p/1693406.html
Copyright © 2011-2022 走看看