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

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

转载请注明来源: 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. Lanucherr 默认显示第几屏

    Launcher.java static final int SCREEN_COUNT = 5;static final int DEFAULT_SCREEN = 2;//第一页是从0开始计数,这里是 ...

  2. C#创建Windows服务的几个注意事项

    1.服务安装后的自动启动:服务的StartType即使配置成Automatic,在首次安装成功之后还是要在服务列表中找到并手工启动.此外,可以通过在ProjectInstaller中添加AfterIn ...

  3. webform 验证控件

    验证: 一.非空验证  RequiredFieldValidator ErrorMessage - 验证出错后的提示信息 ControlToValidate - 要验证的控件的ID Display - ...

  4. linux install zh_CN(ubuntu)

    cd /usr/share/locales sudo ./install-language-pack zh_CN

  5. VC中窗口ID,句柄,指针三者相互转换函数【转】

    ID--HANDLE--HWND三者之间的互相转换id->句柄        hWnd = ::GetDlgItem(hParentWnd,id);id->指针        CWnd:: ...

  6. 512M内存机器如何用好Mysql

    购买阿里云512M内存ECS后,mysql有时候会自动关闭,停止运行 解决办法: a,优化mysql配置,因为自己安装的是mysql 5.6,而从5.6开始,mysql安装包中不再包含my-small ...

  7. Oracle11g R2学习系列 之五回闪

    Oracle里面有一个回闪的操作,这个貌似sql server是没有的.要使用这个功能,需要用到两个时间内部函数 TIMESTAMP和TO_TIMESTAMP.其中,函数TO_TIMESTAMP的语法 ...

  8. WordPress插件制作教程(二): 编写一个简单的插件

    上一篇说到了如何创建一个插件,我想大家看了之后一定会有所收获,这一篇简单给大家写一个插件样例,让大家有一个基本的印象.这个插件的样例就是当你激活这个插件后会在你的每篇文章中插入一段自己定义好的内容,比 ...

  9. 数组对象-new Array

    声明空数组    var arr = new Array(); 声明指定长度的数组          var arr = new Array(5) 声明初始值的数组             var a ...

  10. c++第三天

    今天完成的事情: [主线] 1.复习了一下昨天的内容 while(std::cin >> value) 扫描[标准输入] 2.在网上下载Sales_item.h 代码如下 #ifndef ...