程序中编写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 ...
随机推荐
- 【翻译】如何在Ext JS 6中使用Fashion美化应用程序
原文:How to Style Apps with Fashion in Ext JS 6 在Ext JS 6,一个最大的改变就是框架合并,使用一个单一的代码库,就可以为每一种设备开发各具有良好体验的 ...
- 分布式进阶(十二)Docker固定Container IP
使用pipework工具. 前提:每个Container所做的工作现在还很少,可以不用save.commit. 为了便于通信,自定义一个网桥(192.168.1.180/24),使之IP与宿主主机IP ...
- 【一天一道LeetCode】#69. Sqrt(x)
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Impleme ...
- 浅谈OC内存管理
一.基本原理 (一)为什么要进行内存管理. 由于移动设备的内存极其有限,所以每个APP所占的内存也是有限制的,当app所占用的内存较多时,系统就会发出内存警告,这时需要回收一些不需要再继续使用的内存空 ...
- sed命令 linux
sed 实用工具是一个"编辑器",但它与其它大多数编辑器不同.除了不面向屏幕之外,它还是非交互式的.这意味着您必须将要对数据执行的命令插入到命令行或要处 理的脚本中.当显示它时,请 ...
- 集群通信组件tribes之集群的平行通信
前面的集群成员维护服务为我们提供了集群内所有成员的地址端口等信息,可以通过MembershipService可以轻易从节点本地的成员列表获取集群所有的成员信息,有了这些成员信息后就可以使用可靠的TCP ...
- 深入理解Android IPC机制之Binder机制
Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥有的进程间通信IPC手段包括(Internet Process Connection): 管道(Pipe).信号(Sign ...
- wing带你玩转自定义view系列(3)模仿微信下拉眼睛
发现了爱神的自定义view系列,我只想说一个字:凸(艹皿艹 ) !!相见恨晚啊,早看到就不会走这么多弯路了 另外相比之下我这完全是小儿科..所以不说了,这篇是本系列完结篇....我要从零开始跟随爱哥脚 ...
- (二十四)监听键盘的通知和键盘弹出隐藏的View移动
让控制器监听键盘的通知,注意谁监听,谁的dealloc方法中就要remove,如果非ARC还要调用父类的dealloc方法. //监听键盘的操作: [[NSNotificationCenter def ...
- RAC 10g administration
10g RAC administration See OCFS Oracle Cluster Filesystem, ASM, TNSnames configuration, Oracle Datab ...