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

    方法一:以日期为日志文件名,输出txt文件

    public void WriteLog(string msg) 

        string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
        if (!Directory.Exists(filePath)) 
        { 
            Directory.CreateDirectory(filePath); 
        } 
        string logPath = AppDomain.CurrentDomain.BaseDirectory + "Log\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt"; 
        try 
        { 
            using (StreamWriter sw = File.AppendText(logPath)) 
            { 
                sw.WriteLine("消息:" + msg); 
                sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); 
                sw.WriteLine("**************************************************"); 
                sw.WriteLine(); 
                sw.Flush(); 
                sw.Close(); 
                sw.Dispose(); 
            } 
        } 
        catch (IOException e) 
        { 
            using (StreamWriter sw = File.AppendText(logPath)) 
            { 
                sw.WriteLine("异常:" + e.Message); 
                sw.WriteLine("时间:" + DateTime.Now.ToString("yyy-MM-dd HH:mm:ss")); 
                sw.WriteLine("**************************************************"); 
                sw.WriteLine(); 
                sw.Flush(); 
                sw.Close(); 
                sw.Dispose(); 
            } 
        } 

  • 相关阅读:
    HDU 6071
    HDU 6073
    HDU 2124 Repair the Wall(贪心)
    HDU 2037 今年暑假不AC(贪心)
    HDU 1257 最少拦截系统(贪心)
    HDU 1789 Doing Homework again(贪心)
    HDU 1009 FatMouse' Trade(贪心)
    HDU 2216 Game III(BFS)
    HDU 1509 Windows Message Queue(队列)
    HDU 1081 To The Max(动态规划)
  • 原文地址:https://www.cnblogs.com/zmbhfly/p/9914416.html
Copyright © 2011-2022 走看看