zoukankan      html  css  js  c++  java
  • 日志记录到txt文件

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace WebApplication1
    {
    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    CreateErrorLog("test");
    }
    private static string m_fileName = System.Web.HttpContext.Current.Server.MapPath("~/Error/" + DateTime.Now.ToString("yyyyMMdd") + ".log");
    public static String FileName
    {
    get { return (m_fileName); }
    set { if (value != null || value != "") { m_fileName = value; } }
    }

    public static void CreateErrorLog(string message)
    {
    if (File.Exists(m_fileName))
    {
    ///如果日志文件已经存在,则直接写入日志文件
    StreamWriter sr = File.AppendText(FileName);
    sr.WriteLine(" ");
    sr.WriteLine(DateTime.Now.ToString() + message);
    sr.Close();
    }
    else
    {
    ///创建日志文件
    StreamWriter sr = File.CreateText(FileName);
    sr.WriteLine(" ");
    sr.WriteLine(DateTime.Now.ToString() + message);
    sr.Close();
    }
    }
    }
    }

    -------------------------------------------------------------------------

     1      string str;
     2             str = "sdds";
     3             string sPath = Application.StartupPath + "\" + System.DateTime.Now.ToString("yyyy-MM-dd");
     4             if (!Directory.Exists(sPath))
     5             {
     6                 Directory.CreateDirectory(sPath);
     7             }
     8 
     9 
    10    string exstr = ex.ToString();
    11                                         StreamWriter sw = new StreamWriter(crefod() + "\" + System.Guid.NewGuid().ToString() + ".txt", false);
    12                                         sw.WriteLine(exstr);
    13                                         sw.Close();//写入
    14 
    15 
    16 
    17 
    18             StreamWriter sw = new StreamWriter(sPath + "\txtwrsssiter.txt", false);
    19             sw.WriteLine(str);
    20             sw.Close();//写入
    21 
    22   string str = ex.ToString();
    23                 StreamWriter sw = new StreamWriter("D:\1111\1.txt", false);
    24                 sw.WriteLine(str);
    25                 sw.Close();//写入
    View Code
  • 相关阅读:
    Java第十次作业-房地产;学生成绩计算
    找回感觉之模拟房地产首付计算
    软件工程第四次作业—结对项目
    【作业】需要改进的用户体验二三则
    项目互测测试方式及报告
    [ASE]项目介绍及项目跟进——TANK BATTLE·INFINITE
    Hello World!
    [ASE]sprint3 总结 & sprint4计划
    [ASE][Daily Scrum]12.15
    [ASE][Daily Scrum]12.12
  • 原文地址:https://www.cnblogs.com/lyl6796910/p/3767626.html
Copyright © 2011-2022 走看看