zoukankan      html  css  js  c++  java
  • 写日志 log 到文件夹

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

    namespace DBUtility
    {
    public class FileHelper
    {
    public static void writeLog(string strSQL)
    {
    try
    {
    string strFileName = DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
    string strPath = "d:/log/";
    string strAllPath = strPath + strFileName;

    if (!Directory.Exists(strPath))
    {
    Directory.CreateDirectory(strPath);
    }

    if (File.Exists(strAllPath))
    {
    StreamWriter sw = new StreamWriter(strAllPath, true);
    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strSQL); // 写入Hello World
    sw.Close(); //关闭文件
    }
    else
    {
    FileStream fs = new FileStream(strAllPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
    StreamWriter sw = new StreamWriter(fs); // 创建写入流
    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + strSQL); // 写入Hello World
    sw.Close(); //关闭文件
    fs.Close();
    }
    }
    catch (Exception ex) { }
    finally { }
    }
    }
    }

    2---------------------------

    public static void SaveInfoToTxtFile(string info)
    {
    //如果不存在Log文件夹就创建文件夹
    if (Directory.Exists(@"e:iispublishLog") == false)
    {
    Directory.CreateDirectory(@"e:iispublishLog");
    }

    string fileName = @"e:iispublishLog" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";

    if (!File.Exists(fileName))
    {
    File.Create(fileName);
    }

    try
    {
    using (StreamWriter sWriter = File.AppendText(fileName))
    {
    sWriter.WriteLine("sdfsdf");
    sWriter.Flush();
    }
    }
    catch (Exception err)
    {
    Console.WriteLine(" 保存控制台显示的信息 出现异常!" + err.Message);
    }
    finally
    {
    //如果流不为空,关闭它

    }
    }

  • 相关阅读:
    ASP.NET 2.0的页面缓存功能介绍
    我对针对接口编程的浅解
    接口和抽象类的区别
    面向接口编程到底有什么好处
    泛型编程是什么
    方法的重写、重载及隐藏
    基于事件的编程有什么好处
    Socket Remoting WebService对比
    技术讲座:.NET委托、事件及应用兼谈软件项目开发
    ny589 糖果
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/5973455.html
Copyright © 2011-2022 走看看