• 日志输出--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();
            }
  • 相关阅读:
    .JS replace方法替换所有字符
    .net framework 4.0,结果还是失败,出现HRESULT 0xc8000222错误代码
    用PowerDesigner15自动将数据库里的表生成ER图
    C#对JSON数据格式的处理
    Type of conditional expression cannot be determined because there is no implicit conversion between 'Common.EnumType.EnumGender' and '<null>'
    如何在string.Format方法中输出大括号({})
    网架构学习笔记
    c#实现javascript中函数escape解码
    Solon 开发,八、注入依赖与初始化
    Solon 开发,七、自定义注解开发汇总
  • 原文地址:https://www.cnblogs.com/kevinfuture/p/4278059.html
Copyright © 2020-2023  润新知