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
    {
    //如果流不为空,关闭它

    }
    }

  • 相关阅读:
    关于表格单元格溢出情况的处理(单行文本溢出或多行文本溢出)
    下拉树的公共插件(手写插件的方法)
    ztree树样式的设计
    Android学习笔记之Intent(2)
    Android学习笔记之Intent(1)
    Ajax
    Android学习笔记之ContentProvider
    Android学习笔记之Broadcast Receiver
    Android学习笔记之Service
    Android学习笔记之Intent
  • 原文地址:https://www.cnblogs.com/zhang-wenbin/p/5973455.html
Copyright © 2011-2022 走看看