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

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

转载请注明来源: 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 HttpApplication 事件汇总

    Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看 ...

  2. 《第一行代码》学习笔记9-活动Activity(7)

    1.发现Android中的活动是可以层叠的,每启动一个新的活动,就会覆盖在原活动之上, 然后点击Back键会销毁最上面的活动,下面的一个活动就会重新显示出来. 2.Android是使用任务来管理活动的 ...

  3. shell中的type命令

    type [-aftpP] name [name,...] -a 打印name的所有可能情况,比如type -a ls,会打印ls is aliased to 'ls --color=auto'和ls ...

  4. Linux下运行C++程序出现"段错误(核心已转储)"的原因

    今天写程序出现了“段错误(核心已转储)"的问题,查了一下资料,加上自己的实践,总结了以下几个方面的原因. 1.内存访问出错  这类问题的典型代表就是数组越界. 2.非法内存访问 出现这类问题 ...

  5. linux修改密码

    情景:Linux 服务器上用户的密码被服务器管理员发现太过简单,需要重置密码.处理时为了方便记忆,就直接使用普通用户登录,修改密码时,在原密码的基础上增加一串特定的数字,结果提示不通过.例如出现错误提 ...

  6. a标签加绝对定位在图片上面,a的链接和块状属性block失效,而且是所有IE版本都失效的

    谷歌和火狐浏览器下测试是正常的,IE下鼠标移过logo是没有超链接的提示的 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitio ...

  7. open_basedir restriction in effect. File() is not within the allowed path(s)

    目前发现eaccelerator安装之后如果php.ini中设置open_basedir将导致open_basedir的一些报错(open_basedir restriction in effect. ...

  8. 外部主机连接mysql服务器延时严重问题

    1.原因:由于mysql对连接的客户端进行DNS反向解析 2.禁用dns解析,在 /etc/my.cnf 中加入skip-name-resolve 3.反向解析说明: 所谓反向解析是这样的:mysql ...

  9. C 中va_list,va_arg,va_start,va_end usage

    1.在学习c语言,难免会遇到多参函数,刚好c中也提供了一些机制:宏函数 #ifdef _M_ALPHA typedef struct { char *a0; /* pointer to first h ...

  10. SIEM

    http://en.wikipedia.org/wiki/Security_information_and_event_management http://en.wikipedia.org/wiki/ ...