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

    一:Windows服务、控制台日志方法

    private void WriteLog(string text)
    {

    string path = AppDomain.CurrentDomain.BaseDirectory;
    path = System.IO.Path.Combine(path
    , "Logs\");

    if (!System.IO.Directory.Exists(path))
    {
    System.IO.Directory.CreateDirectory(path);
    }
    string fileFullName = System.IO.Path.Combine(path
    , string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMdd-HHmm")));


    using (StreamWriter output = System.IO.File.AppendText(fileFullName))
    {
    output.WriteLine(text);

    output.Close();
    }
    }

    二:Web写日志方法

    public class WriteLog
    {
    public static void Write(string title, string content)
    {
    string sFilePath = HttpContext.Current.Server.MapPath("\ErrorLog");
    if (Directory.Exists(sFilePath) == false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
    {
    Directory.CreateDirectory(sFilePath); //建立目录 Directory为目录对象
    }

    //string sDirPath = Path.GetDirectoryName(sFilePath);
    //sDirPath += @"ErrorLog";

    string sFname = sFilePath + @"" + title + "-" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

    using (FileStream stream = new FileStream(sFname, FileMode.Append))
    using (StreamWriter writer = new StreamWriter(stream))
    {
    writer.WriteLine("{0}-{1}", DateTime.Now.ToString(), content);
    }
    }

    #region 创建错误日志
    ///-----------------------------------------------------------------------------
    /// <summary>创建错误日志 在c:ErrorLog</summary>
    /// <param name="strFunctionName">strFunctionName,调用方法名</param>
    /// <param name="strErrorNum">strErrorNum,错误号</param>
    /// <param name="strErrorDescription">strErrorDescription,错误内容</param>
    /// <returns></returns>
    public static void ErrorLog(string strFunctionName, string strErrorNum, string strErrorDescription, string content)
    {
    string strPath; //错误文件的路径
    DateTime dt = DateTime.Now;
    try
    {
    strPath = HttpContext.Current.Server.MapPath("\ErrorLog"); //返回网站根目录下查找errolog文件夹地址
    //strPath = "c:" + "\ErrorLog";//暂时放在c:下

    if (Directory.Exists(strPath) == false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
    {
    Directory.CreateDirectory(strPath); //建立目录 Directory为目录对象
    }
    strPath = strPath + "\" + dt.ToString("yyyyMMdd");

    if (Directory.Exists(strPath) == false) //目录是否存在 '工程目录下 Log月 目录 yyyymm
    {
    Directory.CreateDirectory(strPath); //建立目录//日志文件,以 日 命名
    }
    strPath = strPath + "\" + dt.ToString("yyyyMMdd") + ".txt";

    StreamWriter FileWriter = new StreamWriter(strPath, true); //创建日志文件

    FileWriter.WriteLine("---------------------------------------日志开始-----------------------------------------------------------");
    FileWriter.WriteLine("时间: " + dt.ToString("HH:mm:ss"));
    FileWriter.WriteLine("方法名: " + strFunctionName);
    FileWriter.WriteLine("错误代码: " + strErrorNum);
    FileWriter.WriteLine("错误内容: " + strErrorDescription.Replace(" ", ""));
    FileWriter.WriteLine("执行内容: " + content.Replace(" ", ""));
    //FileWriter.WriteLine("时间: " + dt.ToString("HH:mm:ss") + " 日志内容: " + strMatter);
    FileWriter.WriteLine("---------------------------------------日志结束-----------------------------------------------------------");
    FileWriter.Close(); //关闭StreamWriter对象
    }
    catch (Exception ex)
    {
    //("写错误日志时出现问题,请与管理员联系! 原错误:" + strMatter + "写日志错误:" + ex.Message.ToString());
    string str = ex.Message.ToString();
    }
    }

    #endregion
    }
    }

  • 相关阅读:
    C#删除程序自身【总结】
    X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?
    【转】关于C#接口和抽象类的一些说明
    C# 的可空合并运算符(??)到底是怎样的宝宝?
    第三章 “我要点爆”微信小程序云开发之点爆方式页面和爆炸之音页面制作
    微信小程序云开发之云函数的创建与环境配置
    第五章 “我要点爆”微信小程序云开发实例之从云端获取数据制作首页
    第一章 “我要点爆”微信小程序云开发之项目建立与我的页面功能实现
    第四章 “我要点爆”微信小程序云开发之疯狂点击与糖果点爆页面制作
    Git的使用方法与GitHub项目托管方法
  • 原文地址:https://www.cnblogs.com/zhan-shuai/p/5291165.html
Copyright © 2011-2022 走看看