一段c#写的记录日志方法,分月分日记录
2012-10-31 15:43
393 查看
public class Log { private static readonly object obj = new object(); /// <summary> /// 操作日志 /// </summary> /// <param name="s">日志能容</param> public static void WriteLog(string title,string content) { WriteLogs(title, content, "操作日志"); } /// <summary> /// 错误日志 /// </summary> /// <param name="s">日志内容</param> public static void WriteError(string title,string content) { WriteLogs(title, content, "错误日志"); } public static void WriteLogs(string title, string content, string type) { lock (obj) { string path = AppDomain.CurrentDomain.BaseDirectory; if (!string.IsNullOrEmpty(path)) { path = AppDomain.CurrentDomain.BaseDirectory + "log"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = path + "\\" + DateTime.Now.ToString("yyMM"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = path + "\\" + DateTime.Now.ToString("dd") + ".txt"; if (!File.Exists(path)) { FileStream fs = File.Create(path); fs.Close(); } if (File.Exists(path)) { StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default); sw.WriteLine(DateTime.Now + " " + title); sw.WriteLine("日志类型:" + type); sw.WriteLine("详情:" + content); sw.WriteLine("----------------------------------------"); sw.Close(); } } } } }
相关文章推荐
- C#实现线程安全的简易日志记录方法
- c# 一个记录日志的通用方法
- C#方法 记录程序日志的方法
- C#中错误日志记录方法,信息比较详细易定位
- C# 使用Nlog记录日志到数据库 使用LogEventInfo类获取,命名空间名称、类名、方法名
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- C#记录消息到日志文件的方法
- [C#]记录程序耗时的方法
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- 一段我自己常用的c#读取页面源码的方法
- [C#]记录程序耗时的方法【转发】
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
- c#日志记录帮助类分享
- c#向Access数据库中批量插入大量记录的方法
- c# 简单日志记录 txt
- 日志记录的作用和方法
- C#WinCE 记录日志文件
- Linux中将执行过的命令记录到日志并发送到服务器的方法
- C#使用Log4Net记录日志
- Spring AOP进行日志记录,管理 (使用Spring的拦截器功能获取对action中每个方法的调用情况,在方法调用前和调用后记录相关日志。)