程序中编写log日志
public string logFile;
private int configLogLevel = ;
private Stream s = null;
StreamWriter sw = null; /// <summary>
/// 用log文件名初始化对象
/// </summary>
/// <param name="logFile"></param>
public WriteLog(string logFile)
{
this.logFile = logFile;
initFile();
} /// <summary>
/// 用log文件名和配置的log级别初始化对象
/// </summary>
/// <param name="logFile"></param>
/// <param name="configLevel"></param>
public WriteLog(string logFile,int configLevel)
{
this.logFile = logFile;
this.configLogLevel = configLevel;
initFile();
} ~WriteLog()
{
//析构函数,释放资源
//if (sw != null)
//{
// sw.Flush();
// sw.Close();
//}
//if (s != null)
//{
// s.Flush();
// s.Close();
//}
} /// <summary>
/// 初始化文件流
/// </summary>
private void initFile()
{
try
{
if (this.logFile == null || this.logFile == "") return; string path = "";
string fileName = "";
if (this.logFile.IndexOf("\\") >= )
{
path = this.logFile.Substring(,this.logFile.LastIndexOf("\\")+);
fileName = this.logFile.Substring(this.logFile.LastIndexOf("\\")+);
}
else if (this.logFile.IndexOf("/") >= )
{
path = this.logFile.Substring(, this.logFile.LastIndexOf("/")+);
fileName = this.logFile.Substring(this.logFile.LastIndexOf("/") + );
}
else
{
return;
}
string dateStr = DateTime.Now.ToString("yyyyMMdd");
path += dateStr;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
fileName = path + "\\"+ fileName; if (File.Exists(fileName))
{
s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
}
else
{
//CommonFunction.createDir(this.logFile.Substring(0, this.logFile.LastIndexOf(@"\")));
s = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write);
s.Flush();
s.Close();
s = new FileStream(fileName, FileMode.Append, FileAccess.Write);
} sw = new StreamWriter(s, Encoding.Default);//创建Stream流,指定编码方式
//sw.AutoFlush = true;
}
catch (Exception ex)
{
throw ex;
}
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="configLevel">配置设定的log级别</param>
/// <param name="logContent">log内容</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel,int configLevel,string logContent)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < configLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLevel + " ;" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
} }
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="logContent">log内容</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel, string logContent)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < this.configLogLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
} }
} /// <summary>
/// 如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
/// </summary>
/// <param name="logLevel">当前log的级别</param>
/// <param name="logContent">log内容</param>
/// <param name="method">log抛出调用的方法名</param>
//[MethodImpl(MethodImplOptions.Synchronized)]
public void log(int logLevel, string logContent,string methodName)
{
//如果当前log级别小于配置设定的log级别时才将log内容写到日志文件中
if (logLevel < this.configLogLevel && sw != null && s != null)
{
try
{
lock (sw)
{
sw.WriteLine(DateTime.Now.ToString() + "." + DateTime.Now.Millisecond.ToString() + ":logLevel=" + logLevel + " ;configLevel=" + configLogLevel + " ;method=" + methodName + ";content=" + logContent);
sw.Flush();
//s.Flush();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
} public void close()
{
try
{
if (sw != null)
{
sw.Flush();
sw.Close();
}
if (s != null)
{
s.Flush();
s.Close();
}
}
catch (Exception ex)
{
ex.ToString();
}
}
程序中编写log日志的更多相关文章
- 注释和取消注释 程序中的log日志
有点简单,但也是原创哦..亲测有效,期待指正. 更改了log多行的问题.. 例如//Log Util: 一.注释log import java.io.BufferedReader;import ...
- 在C#中winform程序中应用nlog日志工具
在C#中winform程序中应用nlog日志工具,配置文件简单应用. 文件名 nlog.config,请注意修改属性为"始终复制",发布时候容易遇到不存在文件的错误提示. 通过Nu ...
- java中关于log日志
博:http://zhw2527.iteye.com/blog/1006302 http://zhw2527.iteye.com/blog/1099658 在项目开发中,记录错误日志是一个很有必要功能 ...
- Android教程-02 在程序中输出Log
视频教程,建议采用超清模式观看 在Android中一般都用Log输出日志,常见的有5个不同的级别 Log.v() Log.d() Log.i() Log.w() Log.e() 当然很多程序员还比较习 ...
- 对于是否在一个python程序中编写函数的启发
那我们到底是应该直接使用这些模块级别的函数呢,还是先编译一个模式对象,再调用模式对象的方法呢?这其实取决于正则表达式的使用频率,如果说我们这个程序只是偶尔使用到正则表达式,那么全局函数是比较方便的:如 ...
- 如何查看MapReduce执行的程序中的输出日志
我们开发程序的时候,好多人都喜欢用sysout输出内容来查看运行情况.但是在MR程序里写了之后,却不知道去哪里查找,可以参考这篇文章. 第一种方法,我们可以在MapReduce任务查看页面找到这些日志 ...
- Android中对Log日志文件的分析[转]
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...
- Mybatis中在log日志或控制台打印执行的sql
最近在调试代码时,需要查看当前操作执行的sql,在日志里没查到,经过修改logback配置后成功,现记录如下:一.Mybatis版本是3.0.6(我正在用的版本)该版本只需要修改logback.xml ...
- ASP.NET Core 1.0 中使用 Log 日志配置
https://github.com/aspnet/Logging https://docs.asp.net/en/latest/fundamentals/logging.html ASP.NET C ...
随机推荐
- 什么是IOC
什么是IOC IOC:控制反转,控制权的转移,应用程序本身不负责对象的创建和维护,而是由外部容器负责创建和维护. DI(依赖注入)是其一种实现方式 目的:创 ...
- adformsctl.sh 与 adformsrvctl.sh, 10.1.2 及10.1.3
参考 http://blog.csdn.net/cai_xingyun/article/details/40393885 , adformsctl.sh 是开启forms oc4j , 根据之后的 ...
- saiku中文维度,补充说明
saiku在筛选中文维度 会出现浏览器白屏 停止响应的现象,经过跟踪源代码,分析原来在linux 操作系统中 数据库读取的中文和界面选取的编码是不一致的 解决方法, classes\saiku-dat ...
- 04_Nginx命令行参数,控制信号,Nginx启动、停止、重启命令
Nginx简单型,先关闭进程,修改你的配置后,重启进程. kill -QUIT `cat/usr/local/nginx/nginx.pid` ./nginx 2 重新加载配置文件,不重启进程, ...
- dos2unix(windows脚本文件放到unix下运行要注意)
在windows下编写的shell脚本文件,直接放到linux下运行,是不行的. infiniDB的倒库脚本文件load.sh,将tbl文件导入infiniDB,怎么运行不成功,不建job.运来,是w ...
- HOW TO LINK THE TRANSACTION_SOURCE_ID TO TRANSACTION_SOURCE_TYPE_ID
In this Document Goal Solution References APPLIES TO: Oracle Inventory Management - Version 11 ...
- Spring--FileSystemXmlApplicationContext
//从文件系统或者统一定位资源中获得上下文的定义 public class FileSystemXmlApplicationContext extends AbstractXmlApplication ...
- Hbase replication操作
1.修改hbase-site.xml文件 <property> <name>hbase.replication</name> <value>true ...
- os x下如何挂载iso镜像
在linux下可以使用 mount -o loop 在os x下mount好想没有loop选项,不过可以用系统自带的命令 hdiutil mount xxx.iso 即可,弹出可以用 hdiutil ...
- asp.net core上使用redis探索(1)
基于Ubuntu安装redis, 我找的一个很好的网站: https://www.digitalocean.com/community/tutorials/how-to-install-and-con ...