手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#写日志类实例
C#写日志类实例
摘要:本文实例讲述了C#写日志类,分享给大家供大家参考。具体实现方法如下:复制代码代码如下:usingSystem;usingSystem.Con...

本文实例讲述了C#写日志类,分享给大家供大家参考。

具体实现方法如下:

复制代码 代码如下:using System;

using System.Configuration;

using System.IO;

using System.Threading;

namespace FQDService.Utils

{

/// <summary>

/// 写日志类

/// </summary>

public class FileLogger

{

#region 字段

public static readonly object _lock = new object();

#endregion

#region 写文件

/// <summary>

/// 写文件

/// </summary>

public static void WriteFile(string log, string path)

{

Thread thread = new Thread(new ParameterizedThreadStart(delegate(object obj)

{

lock (_lock)

{

if (!File.Exists(path))

{

using (FileStream fs = new FileStream(path, FileMode.Create)) { }

}

using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))

{

using (StreamWriter sw = new StreamWriter(fs))

{

#region 日志内容

string value = string.Format(@"{0}

--------------------------------------------------------

{1}

", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), obj.ToString());

#endregion

sw.WriteLine(value);

sw.Flush();

}

}

}

}));

thread.Start(log);

}

#endregion

#region 写日志

/// <summary>

/// 写日志

/// </summary>

public static void WriteLog(string log)

{

string logPath = ConfigurationManager.AppSettings["LogPath"] + "FQDService_Log.txt";

WriteFile(log, logPath);

}

#endregion

#region 写错误日志

/// <summary>

/// 写错误日志

/// </summary>

public static void WriteErrorLog(string log)

{

string logPath = ConfigurationManager.AppSettings["LogPath"] + "FQDService_ErrorLog.txt";

WriteFile(log, logPath);

}

#endregion

}

}

希望本文所述对大家的C#程序设计有所帮助。

【C#写日志类实例】相关文章:

C#修改MAC地址类的实例

C# Linq读取XML文件的实例

C#简单获取时间差的小例子

C#Js时间格式化问题简单实例

C# 拓展方法的简单实例

C# 一个WCF简单实例

C#生成注册码的实例代码

c# 值类型实例构造器

C#中通过API实现的打印类 实例代码

C# 实现简单打印的实例代码

精品推荐
分类导航