zoukankan      html  css  js  c++  java
  • .net后台记录输出日志到文本,向文本文件追加字符数据

    有些时候不方便F5调试。这样输出到文本。可以当调试,又可以当错误日志,或操作日志

    上代码 

    using System;
    using System.Web;
    using System.IO;
    
    /// <summary>
    /// 输出文本日志到文件~/log目录
    /// </summary>
    /// <param name="title">方便查询的关键字</param>
    /// <param name="logStr">日志内容</param>
    public static void CreateWebLog(string title,string logStr)
    {
    	try
    	{
    		string dir = System.Web.HttpContext.Current.Server.MapPath("~/log");
    		if (Directory.Exists(dir) == false)
    		{
    			Directory.CreateDirectory(dir);
    		}
    		string strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/log/log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
    		FileInfo logFile = new FileInfo(strFilePath);
    		System.IO.FileStream fs;
    		if (logFile.Exists)
    		{
    			fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
    		}
    		else
    		{
    			fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create);
    		}
    		System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
    		sw.WriteLine("---------------------------------------------------------------------------------------");
    		sw.WriteLine("-----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "---------------------------------------");
    		sw.WriteLine("-----------------------------" + title + "----------------------------------------------------------");
    		sw.WriteLine("---------------------------------------------------------------------------------------");
    		sw.WriteLine(logStr);
    		sw.Close();
    		fs.Close();
    	}
    	catch (Exception)
    	{
    
    	}
    
    }
  • 相关阅读:
    Python实现以不同分辨率分类视频
    JPA 的增删改查方法
    JPA 中 find() 和 getReference() 的区别
    (三)JPA工具类
    (二)JPA实体类主键生成策略
    (一)配置JPA的开发环境
    自定义视图和自定义视图解析器
    view-controller
    RedirectAttributes 的使用
    SpringMVC视图解析中的 forward: 与 redirect: 前缀
  • 原文地址:https://www.cnblogs.com/wybshyy/p/13783627.html
Copyright © 2011-2022 走看看