zoukankan      html  css  js  c++  java
  • 日志输出--C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    //添加引用,并导入命名空间
    using System.Management;
    using System.Net.NetworkInformation;
    using System.IO;
    //日志输出类
      public void SWriter(string ipname)
            {
                string sFilePath = "D:WebTest/LogFolder/";//指定文件夹路径
                string sFileName = "LogOutPut"+DateTime.Now.ToString("dd")+".log";//指定文件
                sFileName = sFilePath + "/" + sFileName;//指定路径下的文件
                ///判断是否存在文件夹,如果不存在则创建
                if (!Directory.Exists(sFilePath))
                {
                    Directory.CreateDirectory(sFilePath);
                }
                FileStream fs;//声明文件流
                StreamWriter sw;//声明写入流
                FileInfo fi=new FileInfo(sFileName);//初始化文件操作类
                ///判断是否存在文件,如果不存在则创建
                if (!fi.Exists)
                {
                    fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write);
                }
                else {
                    fs = new FileStream(sFileName,FileMode.Append,FileAccess.Write);
                }
                fs.Close();//凡是流(Stream)必须在最后调用.Flush();跟.Close();方法,或者图个省事用using来处理,也可以用try-catch
                using(sw = new StreamWriter(sFileName,true))
                {
                    Console.SetOut(sw);//打开要写入的文件,没有这个则不会写入
                    Console.WriteLine("“" + ipname + "”" + "的IP用户于" + DateTime.Now + "访问了该主页!!!");//写入内容
                }
                //StreamWriter sw = new StreamWriter(@"D:WebTestLogOutput.txt",true);
                //Console.SetOut(sw);
                //Console.WriteLine("“" + ipname + "”" + "的IP用户于" + DateTime.Now + "访问了该主页!!!");
                //sw.Flush();
                //sw.Close();
            }
  • 相关阅读:
    抽象函数
    函数的奇偶性习题
    高斯函数的那些事
    分段函数
    二次函数习题
    图是学习高中数学的生命线
    恒成立能成立恰成立习题
    http和https的作用与区别
    vue使用v-if v-show页面闪烁,div闪现的解决方法
    理解prototype、proto和constructor的三角关系
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/4278059.html
Copyright © 2011-2022 走看看