【C#通用类】日志记录类
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace KTCommon.LOG
{
public class TraceLog
{
/// <summary>
/// 全局日志对象
/// </summary>
public static TraceLog m_Trace = new TraceLog((HttpRuntime.AppDomainAppId == null) ?
"d://LOG" : HttpRuntime.AppDomainAppPath + "//", "KTGJ"); public string m_LogFilePath = "";//当前日志文件路径 private string m_xmlPath = ""; //Log的目录 private string m_FileNamePrefix = "";
StreamWriter SW; public TraceLog(string filePath, string fileNamePrefix)
{
m_xmlPath = filePath;
m_FileNamePrefix = fileNamePrefix;
} #region //将显示的提示信息写到Log文件
public void Trace(string tipMsg)
{
string nodeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
Trace(nodeTime, tipMsg);
tipMsg = null;
}
public void Trace(string nodeTime, string tipMsg)
{
try
{
//一小时写一个文件
string strNowY = DateTime.Now.Year.ToString();
string strNowM = DateTime.Now.Month.ToString();
string strNowD = DateTime.Now.Day.ToString();
string strNowH = DateTime.Now.Hour.ToString();
string fileName = m_FileNamePrefix + "_" + strNowH + "0000.log";
string filePath = m_xmlPath + "\\LOG\\" + strNowY + "\\" + strNowM + "\\" + strNowD + "\\";
if (nodeTime != "")
{
nodeTime = "[" + nodeTime + "] ";
} //LOG目录不存在,则创建 if (Directory.Exists(filePath) == false)
{
Directory.CreateDirectory(filePath);
} m_LogFilePath = filePath + fileName;
//日志文件不存在,则创建 if (File.Exists(filePath + fileName) == false)
{
if (SW != null)
{
SW.Flush();
SW.Close();
}
File.Create(filePath + fileName).Close();
SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8);
}
//创建实例
if (SW == null)
{
SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8);
}
//将内容写到log文件中 SW.WriteLine(nodeTime + tipMsg);
//刷新,实时保存 SW.Flush();
}
catch (Exception ex)
{
System.Diagnostics.Debug.Print("TraceLog Error:" + ex.Message.ToString());
}
}
#endregion //将消息写到Log文件
}
}
【C#通用类】日志记录类的更多相关文章
- 【个人使用.Net类库】(2)Log日志记录类
开发接口程序时,要保证程序稳定运行就要时刻监控接口程序发送和接收的数据,这就需要一个日志记录的类将需要的信息记录在日志文件中,便于自己维护接口程序.(Web系统也是如此,只是对应的日志实现比这个要复杂 ...
- php 简单通用的日志记录方法
使用file_put_contents 方法来实现简单便捷的日志记录功能 方法1: // use \r\n for new line on windows, just \n on linux func ...
- 日志记录类LogHelper
开源日志log4net使用起来很方便,但是项目中不让用,所以自己重写了一个类,用来记录日志,比较简单. 1.首先是可以把日志分成多个类型,分别记录到不同的文件中 /// <summary> ...
- C#日志记录类
public class WriteLog { /// <summary> /// 将错误写入文件中 /// </summary> /// <param name=&qu ...
- Java 基于log4j的日志工具类
对log4j日志类进行了简单封装,使用该封装类的优势在于以下两点: 1.不必在每个类中去创建对象,直接类名 + 方法即可 2.可以很方便的打印出堆栈信息 package com.tradeplatfo ...
- Log 日志工具类 保存到文件 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- yii学习笔记(6),连接数据库,创建活动记录类
创建数据库用于测试 配置数据库连接 打开yii的配置文件目录下的数据库配置文件config/db.php <?php return [ 'class' => 'yii\db\Connect ...
- 利用AOP与ToStringBuilder简化日志记录
刚学spring的时候书上就强调spring的核心就是ioc和aop blablabla...... IOC到处都能看到...AOP么刚开始接触的时候使用在声明式事务上面..当时书上还提到一个用到ao ...
- C# 面向切面编程--监控日志记录方案
背景:现在公司整体在做监控平台,要求把各个部分的细节都记录下来,在前台页面上有所显示,所以现在需要做的就是一个监控日志的记录工作,今天讲的就是渲染监控日志的例子. 现状:当前的渲染程序没有为监控日志记 ...
随机推荐
- webform 复杂点的服务器控件
1 , dropdownlist: 下拉框 属性items 列表集合, 里面的每一个元素是一个 listitem . 联动的时候注意要 设置属性 .Autopostback 为ture: 注注 ...
- Socket服务端口长连接最多能支持多少?
答案是无限的.视服务端的资源而不同. 以前一直认为服务端在Accept客户端连接后,会开启一个新的端口与客户端建立链路,但这是错误的.事实上,一个连路是由Server IP+server Port + ...
- 数据类型转换中的一些特殊情况(JY06-JavaScript)
1.字符串的不可变性 字符串定义了后,会一直占据内存空间,企鹅该处内存空间(栈)不可被重新赋值. 2.短路运算 ||.&& 二元运算符,返回参与运算的操作数的原值(原数据类型和原数据) ...
- How to Build CyanogenMod for One X (codename: endeavoru)
来源:http://wiki.cyanogenmod.org/w/Build_for_endeavoru#What_you.E2.80.99ll_need How to Build CyanogenM ...
- 认识html标签
让我们通过一个网页的学习,来对html标签有一个初步理解. 平常大家说的上网就是浏览各种各式各样的网页,这些网页都是由html标签组成的. 下面就是一个简单的网页.效果图如下: 我们来分析一下,这个网 ...
- "float: left;" div 不换行显示
<div id='p'> <div id='c1'> </div> <div id='c2'> </div> <div id='c3' ...
- sql查询每门课程成绩最高的学生
给出数据库(sco)如下图: 查出每门课程成绩最高的学生 select b.id,b.kemu,b.name,b.chengji from (select kemu,max(chengji) maxc ...
- js中的|| 与 &&
a && b : 将a, b转换为Boolean类型, 再执行逻辑与, true返回b, false返回aa || b : 将a, b转换为Boolean类型, 再执行逻辑或, tru ...
- Thinkphp 控制器
控 制 器: 1.命名方法:新建一个主页面的控制器 controller文件夹下新建一个文件,命名为:MainController.class.php首字母都大写,称为驼峰命名法 eg: <?p ...
- 1105ecos笔记
1 dbschema <?php$db['item']=array ( 'columns' => array ( 'item_id' => array ( 'type' => ...