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();
            }
  • 相关阅读:
    程序员要善于在工作中找到偷懒的办法
    关于count(1) 和 count(*)
    前端设计+程序开发那点事
    关于MySQL Connector/C++那点事儿
    windows下编译php5.2.17这是闹哪样?
    easyui使用时出现这个Uncaught TypeError: Cannot read property 'combo' of undefined
    视频文件自动转rtsp流
    Jenkins Pipeline如何动态的并行任务
    Jenkins的Dockerfile中如何批量迁移原Jenkins安装的插件
    Groovy中json的一些操作
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/4278059.html
Copyright © 2011-2022 走看看