众所周知,日志是调试程序的有效途径,有一个好的日志代码,是一个程序小猿梦寐以求的。

以下是我结合网上资源自己总结的一小段代码,请笑纳:

转载请注明来源: http://www.cnblogs.com/benpao/p/3766644.html

using System.Text;
using System.IO;

public class Log
{
private static LogManager logManager;
static Log()
{
logManager = new LogManager();
} public static void WriteLog(LogFile logFile, string msg)
{
try
{
logManager.WriteLog(logFile, msg);
}
catch
{
}
} public static void WriteLog(string msg)
{
try
{
logManager.WriteLog(LogFile.Info, msg);
}
catch
{
}
} public static void WriteLog(string logFile, string msg)
{
try
{
logManager.WriteLog(logFile, msg);
}
catch
{ }
}
} public class LogManager
{
private string logFileName = string.Empty;
private string logPath = "Log";
private string logFileExtName = "log";
private bool writeLogTime = true;
private bool logFileNameEndWithDate = true;
private Encoding logFileEncoding = Encoding.UTF8;
private object obj = new object(); #region 构造函数
public LogManager()
{
this.LogPath = "Log";
this.LogFileExtName = "log";
this.WriteLogTime = true;
this.logFileNameEndWithDate = true;
this.logFileEncoding = Encoding.UTF8;
}
public LogManager(string logPath, string logFileExtName, bool writeLogTime)
{
this.LogPath = logPath;
this.LogFileExtName = logFileExtName;
this.WriteLogTime = writeLogTime;
this.logFileNameEndWithDate = true;
this.logFileEncoding = Encoding.UTF8;
} #endregion #region 属性
/// <summary>
/// Log 文件路径
/// </summary>
public string LogPath
{
get
{
if (this.logPath == null || this.logPath == string.Empty)
{
//Application.StartupPath
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
return this.logPath;
}
set
{
this.logPath = value;
if (this.logPath == null || this.logPath == string.Empty)
{
//Application.StartupPath
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
else
{
try
{
// 判断是否不是绝对路径(绝对路径里还有":")
if (this.logPath.IndexOf(Path.VolumeSeparatorChar) >= )
{ /* 绝对路径 */}
else
{
// 相对路径
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory + this.logPath, DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!Directory.Exists(this.logPath))
Directory.CreateDirectory(this.logPath);
}
catch
{
this.logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
}
if (!this.logPath.EndsWith(@"\"))
this.logPath += @"\";
}
}
} /// <summary>
/// Log 文件扩展名
/// </summary>
public string LogFileExtName
{
get { return this.logFileExtName; }
set { this.logFileExtName = value; }
} /// <summary>
/// 是否在每个Log行前面添加当前时间
/// </summary>
public bool WriteLogTime
{
get { return this.writeLogTime; }
set { this.writeLogTime = value; }
} /// <summary>
/// 日志文件名是否带日期
/// </summary>
public bool LogFileNameEndWithDate
{
get { return logFileNameEndWithDate; }
set { logFileNameEndWithDate = value; }
} /// <summary>
/// 日志文件的字符编码
/// </summary>
public Encoding LogFileEncoding
{
get { return logFileEncoding; }
set { logFileEncoding = value; }
}
#endregion #region 公有方法
public void WriteLog(string logFile, string msg)
{
lock (obj)
{
try
{
string dateString = string.Empty;
if (this.logFileNameEndWithDate || logFile.Length == )
{
dateString = DateTime.Now.ToString("yyyyMMdd");
} logFileName = string.Format("{0}{1}{2}.{3}",
this.LogPath,
logFile,
dateString,
this.logFileExtName);
using (StreamWriter sw = new StreamWriter(logFileName, true, logFileEncoding))
{
if (writeLogTime)
{
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
}
else
{
sw.WriteLine(msg);
}
}
}
catch
{ }
}
} public void WriteLog(LogFile logFile, string msg)
{
this.WriteLog(logFile.ToString(), msg);
} public void WriteLog(string msg)
{
this.WriteLog(string.Empty, msg);
} #endregion
} public enum LogFile
{
Trace,
Error,
SQL,
SQLError,
Login,
Info,
WeChat
}

码农都是有尊严的

转载请注明来源,谢谢

http://www.cnblogs.com/benpao/

C#中的一种按日期分文件夹的日志写法的更多相关文章

  1. 从Discuz!NT项目文件结构看如何给系统框架分层和类库分文件夹

    以下为Discuz!NT的文件夹根目录: 类库图: 从上面两个图可以看出: 1.dnt对于类库的分层是通过名称的层级来区分的,如Discuz.Plugn和Discuz.Plugin.Spread 2. ...

  2. vue-cli中自定义路径别名 assets和static文件夹的区别

    转自:vue-cli中自定义路径别名 assets和static文件夹的区别 静态资源处理: assets和static文件夹的区别 相信有很多人知道vue-cli有两个放置静态资源的地方,分别是sr ...

  3. AIR 中的 File 对象 所访问的文件夹位置

    AIR 中的 File 对象 所访问的文件夹位置 Link 关于File.cacheDirectory的一点说明 According to the Apple guidelines, data tha ...

  4. Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

    这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...

  5. [置顶] LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句

    LOAD语句:利用MSSQL中的xp_cmdshell功能,将指定文件夹下的指定文件,生成mysql的LOAD语句 declare @sql varchar(4000), @dirpath varch ...

  6. SecureCRT SSH Linux中不显示彩色 字体颜色、文件夹和文件显示的颜色区别开解决办法

    SecureCRT SSH Linux中不显示彩色 字体颜色.文件夹和文件显示的颜色区别开解决办法 实验环境: 刚开始我的情况是这样的:带颜色的显示不出来,然后还能看到,此处有内容,猜测是Secure ...

  7. linux中5种方法过滤出文件夹ls -F ls -p grep、find快速查找过滤目录

    1.ls -l , 根据颜色区分目录和文件2.ls -l, 以d开头的是目录 ls -l | grep "^d" 过滤以d开头的3.ls -l , 输入结果中第二列中大余1的是目录 ...

  8. webform工程中aspx页面为何不能调用appcode文件夹下的类(ASP.NET特殊文件夹的用法)

    App_code 只有website类型的工程才有效. App_Code 下创建的.cs文件仅仅是“内容”不是代码.你设置那个文件为“编译”就行了. 其他特殊文件夹 1. Bin文件夹 Bin文件夹包 ...

  9. OutputStream-InputStream-FileOutputStream-FileInputStream-BufferedOutputStream-BufferedInputStream-四种复制方式-单层文件夹复制

    字节流两套:         java.lang.Object--java.io.OutputStream--java.io.FileOutputStream         java.lang.Ob ...

随机推荐

  1. ASP.NET中在线用户统计

    统计在线用户的作用不言而喻,就是为了网站管理者可以知道当前用户的多少,然后根据用户数量来观察服务器或者程序的性能,从而可以直观的了解到网站的吸引力或者网站程序的效率.现在,我们就介绍一个简单明了的方法 ...

  2. (转)第一天 XHTML CSS基础知识 文章出处:标准之路(http://www.aa25.cn/div_css/902.shtml)

    欢迎大家学习<十天学会web标准>,也就是我们常说的DIV+CSS.不过这里的DIV+CSS是一种错误的叫法,建议大家还是称之为web标准. 学习本系列教程需有一定html和css基础,也 ...

  3. ORACLE SQL 组函数【weber出品必属精品】

    组函数:对一组数据进行加工,每组数据返回一个值 常用的组函数:count()  avg()  max()   min()  sum()   count()函数  1. count(*) :返回总共的行 ...

  4. hdu2488 dfs

    G - 深搜 基础 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bi ...

  5. jquery的uploadify上传jsp+servlet

    1.准备材料:下载jquery.uploadify上传js   注意:这个上传在firefox下会出现问题如果你在项目中加了拦截器,因为session会丢失,所以你可以传参的时候带上你所需要的条件,在 ...

  6. rman全库恢复到不同主机,不同实例名,不同目录下

    一.配置目标主机的ip.hostname及与源端主机的连通性 1.配置目标主机IP 使用图形界面配置IP: administration----network---修改IP(指定静态IP) deact ...

  7. 自定义VIew基础

    一.坐标 ①.通过View获取坐标,通过调用getLeft().getRight()...方法获取坐标. 1.获取到的是相对于View父控件的位置 2.指的是左上角和右下角的x,y值 3.View还提 ...

  8. apache用户认证、默认主机、301跳转

    我更正论坛一个同学帖子(今天坑我一下午):原文http://www.apelearn.com/bbs/foru ... 3%BB%A7%C8%CF%D6%A4 apache用户认证.默认主机.301跳 ...

  9. kafka文档翻译(一)

    原文来自(http://kafka.apache.org/documentation.html) 本文只做简单的翻译,水平有限,仅供学习交流使用 如有错误,欢迎点评指正 1  准备开始 1.1 介绍 ...

  10. 转载--http协议学习和总结

    http的了解一直停留在一知半解的程度,今天看到阿蜜果大大的博客,果断学习了,这里做个转载,希望阿蜜果大大不要怪罪~~ 3.1 Cookie和Session Cookie和Session都为了用来保存 ...