zoukankan      html  css  js  c++  java
  • 生成Log文件的写法

    System.IO.StreamWriter w = new System.IO.StreamWriter(Server.MapPath("~/App_data/xxx.log"), true);
    w.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + " IP:" + clientIp + " UserName:" + this.Login1.UserName);
    w.Close();

     1 using System.IO;
     2 public static void WriteLog(string strLog)
     3 {
     4   string sFilePath="d:\"+DateTime.Now.ToString("yyyyMM");
     5   string sFileName = "rizhi" + DateTime.Now.ToString("dd") + ".log";
     6   sFileName = sFilePath+ "\"+sFileName; //文件的绝对路径
     7   if (!Directory.Exists(sFilePath))//验证路径是否存在
     8   {
     9     Directory.CreateDirectory(sFilePath);
    10     //不存在则创建
    11   }
    12   FileStream fs;      
    13   StreamWriter sw;
    14   if (File.Exists(sFileName))
    15   //验证文件是否存在,有则追加,无则创建
    16   {
    17     fs = new FileStream(sFileName, FileMode.Append, FileAccess.Write);
    18   }
    19   else
    20   {
    21     fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
    22   }
    23   sw = new StreamWriter(fs);
    24   sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "   ---   " + strLog);
    25   sw.Close();
    26   fs.Close();    
    27 }
    View Code
  • 相关阅读:
    properties 插件安装
    FineReport 安装教程
    Red/Black Tree 演示
    java 日期转化
    Tomcat使用Log4j按天生成日志 亲测可行
    服务器初始化
    ubuntu-rc.local
    django-views
    https tcp ssl
    svn
  • 原文地址:https://www.cnblogs.com/huangjing/p/7452423.html
Copyright © 2011-2022 走看看