【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# 面向切面编程--监控日志记录方案
背景:现在公司整体在做监控平台,要求把各个部分的细节都记录下来,在前台页面上有所显示,所以现在需要做的就是一个监控日志的记录工作,今天讲的就是渲染监控日志的例子. 现状:当前的渲染程序没有为监控日志记 ...
随机推荐
- jdbc03 使用servlet实现
<%@page import="cn.bdqn.bean.News"%> <%@page import="cn.bdqn.service.impl.Ne ...
- python面对对象编程------4:类基本的特殊方法__str__,__repr__,__hash__,__new__,__bool__,6大比较方法
一:string相关:__str__(),__repr__(),__format__() str方法更面向人类阅读,print()使用的就是str repr方法更面对python,目标是希望生成一个放 ...
- python运行时间计算之timeit
timeit.timeit(stmt='pass', setup='pass', timer=<default timer>, number=1000000) stmt:statement ...
- 数据库WMI 0x80041010 如何解决?
在你打开 SQL Server Configuration Manager遇到以下错误的时候,请参考下面提出的解决办法 solution: 打开cmd 命令窗口执行mofcomp.exe " ...
- DOM基础之“寻找”子节点
今天学习了JS中的DOM的内容,虽然小菜的理解不深,但希望能够记录下来,慢慢一点一点的进步,有更深的理解了,再回来补充. 首先,关于DOM的概念: 1.概念:DOM = document(文档)Obj ...
- java的注释
最近在做java项目开始关注和注意一些java规范,目的只是为了让自己和别人更容易理解自己写的代码和复用. 一个重要的原则就是:问你自己,你如果从来没有见过这段代码,你要快速地知道这段代码是干什么的, ...
- Caused by: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "mil_id")
今天在使用mybatis处理数据库的时候,突然抛出了上述异常,让我感到很惊讶,因为在处理save的时候,在Mybatis的配置文件中,我根本就没有使用到ognl表达式,系统怎么会抛出上述异常.而且之前 ...
- keybd_event 转载
转自 http://apps.hi.baidu.com/share/detail/14468670 Option Explicit Private Declare Sub keybd_event Li ...
- centos6.2下安装星际译王stardict3.0
星际译王是一个Linux下很好的翻译软件. 我的系统是centos6.2 32位版.本来在http://code.google.com/p/stardict-3/downloads/list 上下的源 ...
- php urlencode()函数URL编码转换实例解析
URLEncode:是对网页url所包含中文字符的一种编码转化方式,URLEncode有两种常见方式,一种是基于GB2312的 Encode(Baidu.Yisou等搜索引擎使用),另一种是基于UTF ...