自己写的Log记录组件
常规的Debug组件的封装,然后加了一个文件log,分异步和同步(可跨平台使用)。
/// <summary>
/// 常用IO操作类
/// </summary>
public class HIO
{
/// <summary>
/// 控制台数据错误
/// </summary>
/// <param name="ex"></param>
public static void WriteConsole(Exception ex)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("Data:" + ex.Data + Environment.NewLine
+ " InnerException:" + ex.InnerException + Environment.NewLine
+ " Message:" + ex.Message + Environment.NewLine
+ " Source:" + ex.Source + Environment.NewLine
+ " StackTrace:" + ex.StackTrace + Environment.NewLine
+ " TargetSite:" + ex.TargetSite);
}
/// <summary>
/// 控制台数据错误
/// </summary>
/// <param name="ex"></param>
public static void WriteDebug(Exception ex)
{
Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
Debug.WriteLine("Data:" + ex.Data + Environment.NewLine
+ " InnerException:" + ex.InnerException + Environment.NewLine
+ " Message:" + ex.Message + Environment.NewLine
+ " Source:" + ex.Source + Environment.NewLine
+ " StackTrace:" + ex.StackTrace + Environment.NewLine
+ " TargetSite:" + ex.TargetSite);
}
/// <summary>
/// 应用根目录
/// </summary>
public static string AppRootPath = AppDomain.CurrentDomain.BaseDirectory;// Environment.CurrentDirectory
/// <summary>
/// 日志目录
/// </summary>
public static string logDir = "Log";
/// <summary>
/// 日志目录全路径
/// </summary>
public static string logDirPath = AppRootPath + Path.DirectorySeparatorChar + logDir;
/// <summary>
/// 错误记录
/// </summary>
/// <param name="ex"></param>
public static void WriteLog(Exception ex)
{
WriteLog("Data:" + ex.Data + Environment.NewLine
+ " InnerException:" + ex.InnerException + Environment.NewLine
+ " Message:" + ex.Message + Environment.NewLine
+ " Source:" + ex.Source + Environment.NewLine
+ " StackTrace:" + ex.StackTrace + Environment.NewLine
+ " TargetSite:" + ex.TargetSite);
}
/// <summary>
/// 写log
/// </summary>
/// <param name="InfoStr"></param>
public static void WriteLog(string info)
{
WriteLog(info, logDirPath);
}
/// <summary>
/// 写log(自动时间log)
/// </summary>
/// <param name="InfoStr">内容</param>
/// <param name="FilePath">目录地址</param>
public static void WriteLog(string info, string DirPath)
{
FileStream stream = null;
System.IO.StreamWriter writer = null;
try
{
if (Directory.Exists(DirPath) == false)
{
Directory.CreateDirectory(DirPath);
}
string dateDir = DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(DirPath + Path.DirectorySeparatorChar + dateDir)) { Directory.CreateDirectory(DirPath + Path.DirectorySeparatorChar + dateDir); }
string logFilePath = DirPath + Path.DirectorySeparatorChar + dateDir
+ Path.DirectorySeparatorChar + "SyncLog_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";
if (File.Exists(logFilePath) == false)
{
stream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
}
else
{
stream = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
}
writer = new System.IO.StreamWriter(stream);
writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
writer.WriteLine(info);
}
finally
{
if (writer != null)
{
writer.Close();
writer.Dispose();
}
if (stream != null)
{
stream.Close();
stream.Dispose();
}
}
}
/// <summary>
/// 异步写log
/// </summary>
/// <param name="info"></param>
public static void AsyncWriteLog(string info)
{
AsyncWriteLog(info, Encoding.UTF8, logDirPath);
}
/// <summary>
/// 异步写log
/// </summary>
/// <param name="ex"></param>
public static void AsyncWriteLog(Exception ex)
{
AsyncWriteLog("Data:" + ex.Data + Environment.NewLine
+ " InnerException:" + ex.InnerException + Environment.NewLine
+ " Message:" + ex.Message + Environment.NewLine
+ " Source:" + ex.Source + Environment.NewLine
+ " StackTrace:" + ex.StackTrace + Environment.NewLine
+ " TargetSite:" + ex.TargetSite, Encoding.UTF8, logDirPath);
}
/// <summary>
/// 异步写log
/// </summary>
/// <param name="info"></param>
/// <param name="encode"></param>
/// <param name="FilePath"></param>
public static void AsyncWriteLog(string info, Encoding encode, string FileDirPath)
{
AsyncWriteLog(encode.GetBytes(info), FileDirPath);
}
/// <summary>
/// 异步写log
/// </summary>
/// <param name="datagram">要写入当前流的数据的缓冲区</param>
/// <param name="FilePath"></param>
public static void AsyncWriteLog(byte[] datagram, string FileDirPath)
{
AsyncWriteLog(datagram, datagram.Length, FileDirPath, (obj) =>
{
AsyncResult ar = obj as AsyncResult;
FileStream stream = ar.AsyncState as FileStream;
if (stream != null)
{
stream.Close();
stream.Dispose();
}
});
}
/// <summary>
/// 异步写log
/// </summary>
/// <param name="datagram">要写入当前流的数据的缓冲区</param>
/// <param name="numBytes">最多写入的字节数</param>
/// <param name="FilePath"></param>
/// <param name="callback"></param>
public static void AsyncWriteLog(byte[] datagram, int numBytes, string FileDirPath, AsyncCallback callback)
{
if (datagram.Length == 0) { throw new Exception("数据为0"); }
if (numBytes == 0) { throw new Exception("写入数为0"); }
if (string.IsNullOrEmpty(FileDirPath)) { throw new Exception("文件地址为空"); }
if (!Directory.Exists(FileDirPath)) { Directory.CreateDirectory(FileDirPath); }
string dateDir = DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(FileDirPath + Path.DirectorySeparatorChar + dateDir)) { Directory.CreateDirectory(FileDirPath + Path.DirectorySeparatorChar + dateDir); }
string logFilePath = FileDirPath + Path.DirectorySeparatorChar + dateDir
+ Path.DirectorySeparatorChar + "AsyncLog_" + DateTime.Now.ToString("yyyyMMddHH") + ".txt";
FileStream stream = null;
try
{
if (File.Exists(logFilePath) == false)
{
stream = new FileStream(logFilePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
}
else
{
stream = new FileStream(logFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
} if (stream.CanWrite)
{
stream.BeginWrite(datagram, 0, numBytes, callback, stream);
}
else
{
throw new Exception("文件无法写入,文件或只读!");
}
}
catch (Exception ex)
{
WriteDebug(ex);
}
}
/// <summary>
/// 文本转义(方便讲文本转换成C#变量代码)
/// 例子 " 转化成 string str="\"";
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string ToEscape(string str, string m_var)
{
/*
" \"
\ \\
*/
str = str.Trim();
str = str.Replace("\\", "\\\\");
str = str.Replace("\"", "\\\"");
return "string " + m_var + "=\"" + str + "\";";
} /// <summary>
/// (递归)去掉所有首部出现的字符串
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="headStr">首部出现的字符串</param>
/// <returns></returns>
public static string RemoveHead(string str, string headStr)
{
if (str.StartsWith(headStr))
{
str = str.Remove(0, headStr.Length);
return RemoveHead(str, headStr);
}
else
{
return str;
}
}
/// <summary>
/// 路径解析转换,转化成符合当前系统的路径符号
/// </summary>
/// <param name="path">路径</param>
/// <param name="flag">(路径的类型)1:windows \ 2:linux /(linux和web网页的分隔相符)</param>
/// <param name="dHear">是否去掉首部的路径分隔符</param>
/// <returns></returns>
public static string PathParse(string path, int flag, bool dHear)
{
string win = @"\";
string linux = @"/";
string sys = Path.DirectorySeparatorChar.ToString();
if (flag == 1)
{
path = path.Replace(win, sys);
}
else if (flag == 2)
{
path = path.Replace(linux, sys);
}
if (dHear)
{
path = RemoveHead(path, sys);
}
return path;
}
/// <summary>
/// web路径地址转换为系统路径
/// </summary>
/// <param name="appPath">应用路径</param>
/// <param name="webPath">web路径</param>
/// <param name="dHear">是否去掉首部的路径分隔符</param>
/// <returns></returns>
public static string SysPathParse(string appPath, string webPath, bool dHear)
{
string sys = Path.DirectorySeparatorChar.ToString();
string web = @"/";//web的分隔符
webPath = webPath.Replace(web, sys);
if (dHear)
{
webPath = RemoveHead(webPath, sys);
}
string result = "";
if (appPath.EndsWith(sys))
{
if (webPath.StartsWith(sys))
{
result = appPath + webPath.Remove(0, 1);
}
else
{
result = appPath + webPath;
}
}
else
{
if (webPath.StartsWith(sys))
{
result = appPath + webPath;
}
else
{
result = appPath + sys + webPath;
}
}
return result;
}
public static string WebPathParse(string fullPath, string appPath, bool dHear)
{
string sys = Path.DirectorySeparatorChar.ToString();
string web = @"/";//web的分隔符
if (fullPath.StartsWith(appPath))
{
string webPath = fullPath.Remove(0, appPath.Length);
webPath = webPath.Replace(sys, web);
if (webPath.StartsWith(web) == false)
{
webPath = web + webPath;
}
if (dHear)
{
webPath = RemoveHead(webPath, web);
}
return webPath;
}
else
{
return "";
}
}
}
自己写的Log记录组件的更多相关文章
- 在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间
在.txt文件的首行写上.LOG后,后面每次对改文本文件进行编辑后,系统会自动在编辑内容后记录操作时间
- log4net--不可多得的开源日志记录组件
log4net--不可多得的开源日志记录组件 1 前奏 一直在用log4net日志工具,却没时间写个日志给大家分享一下这个工具,趁最近比较空些,好好分享一下这个工具. 2 说明 Log4net介绍就不 ...
- 日志记录组件[Log4net]详细介绍
转载:http://www.cnblogs.com/liwei6797/archive/2007/04/27/729679.html 因为工作中有要用到Log记录,找到一篇不错的文章,就转了过来. 一 ...
- 【干货】.NET开发通用组件发布(四) 日志记录组件
组件介绍和合作开发 http://www.cnblogs.com/MrHuo/p/MrHuoControls.html 日志记录组件功能介绍 通过基类Logger,实现了文本记录日志和数据库记录日志两 ...
- C# 程序A发送Log记录给程序B,程序B处理和分析Log记录
C# 程序A发送Log记录给程序B,程序B处理和分析Log记录 关键字:C# ;Log记录 ;在线Log记录;Socket:httplistener 一.常用场景 1. APP开发,在真机或者虚拟机上 ...
- HDFS namenode 写edit log原理以及源码分析
这篇分析一下namenode 写edit log的过程. 关于namenode日志,集群做了如下配置 <property> <name>dfs.nameservices< ...
- php 写内容到文件,把日志写到log文件
php 写内容到文件,把日志写到log文件 <?php header("Content-type: text/html; charset=utf-8"); /******** ...
- 快速php日志,写内容到文件,把日志写到log文件
php 写内容到文件,把日志写到log文件 //记录日志:要写入文件的文件名(可以是任意文件名),如果文件不存在,将会创建一个.log.txt位置在项目的根目录下. $file = 'log.txt' ...
- IIS 高并发导致log记录不完全
项目测试性能过程中,对于高并发测试过程中发现log记录缺失一部分,经过调查,找到了原因是因为IIS连接数的限制,经过修改连接数,成功完成.设置如下: “点击网站”->“右击切换到功能视图”-&g ...
随机推荐
- hadoop完全分布式文件系统集群搭建
一.准备工作: 1.找3台以上的主机(因为HDFS文件系统中保存的文件的blocak在datanode中至少要有3份或3份以上的备份,备份不能放于同一个机架上,更不能放于同一台主机上),我这里使用的是 ...
- php后台添加样式写法
和我们的光头后台讨论了样式的问题,总结一下 <span style="color:#6666cc;font-size: 12px" onclick="addwork ...
- mongo之map-reduce笔记
package com.sy.demo; import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; import ...
- UIAlertController UIAlertView用法
项目中很多地方会出现弹出框框,来做个判断 基本方法如下 UIAlertController *alertC = [UIAlertController alertControllerWithTitle: ...
- linux find -regex 使用正则表达式
find之强大毋庸置疑,此处只是带领大家一窥find门径,更详细的说明见man find和 info find.整篇文章循序渐进,从最常用的文件名测试项开始步步深入,到第六节基本讲完find处理文件 ...
- ThreadPoolExecutor之一:使用基本介绍
一.concurrent包中的线程池的简单介绍 线程池按照线程数量可以分为:一是固定线程数量的线程池:二是可变数量的线程池. 线程池按照执行时间可以分为:一是立即执行线程池:二是延时线程池. Thre ...
- SpringBoot之二:部署Spring Boot应用程序方式
衡量多种部署方式 Spring Boot应用程序有多种构建和运行方式,其中一些你已经使用过了. 在IDE中运行应用程序(涉及Spring ToolSuite或IntelliJ IDEA). 使用Mav ...
- springmvc----demo2---a->b--bai
1.jsp 2.jsp 3.jsp LianxiAction: package com.etc.controller; import javax.servlet.http.HttpSession; i ...
- django的settings.py设置session
############ # SESSIONS # ############ SESSION_CACHE_ALIAS = 'default' # Cache to store session data ...
- Celery-4.1 用户指南: Security (安全)
简介 虽然Celery 编写的时候考虑了安全,但是它仍然应该认为是一个不安全的组件. 依据安全策略,你可以采取几个步骤使得你的 Celery 安装更加安全. 关注的领域 消息中间件 保障消息中间件不受 ...