1.文件流形式
public class LogManager
{
private static string m_RootPath =string.Empty;
public static string RootPath
{
get
{
try
{
if (string.IsNullOrEmpty(m_RootPath))
{
string strDLLPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
strDLLPath = strDLLPath.Substring(0, strDLLPath.LastIndexOf("\\") + 1);
m_RootPath = strDLLPath + "Logs\\";
if (!Directory.Exists(m_RootPath))
{
Directory.CreateDirectory(m_RootPath);
}
}
return m_RootPath;
}
catch
{
throw new DirectoryNotFoundException("取得程序集路径出错!");
}
}
}
public static void WriteLog(string message)
{
try
{
string logFile = RootPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
string newMsg = "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] " + message + "\r\n";
using (var fs = new FileStream(logFile, FileMode.Append))
{
using (var sw = new StreamWriter(fs))
{
sw.Write(newMsg);
}
}
}
catch
{
throw new FileNotFoundException("操作日志文件出错!");
}
}
}