1、新建一个类库解决方案 CommnoLog
2、新建两个文件夹
2.1FileUtil.cs 代码如下
public static class FileUtil { /// <summary> /// 追加内容到指定文件中 /// </summary> /// <param name="filePath"></param> /// <param name="content"></param> public static void WriteAppend(string filePath, string content) { WriteAppend(filePath, new string[] { content }); }
public static void WriteAppend(string filePath, string[] contents) { //System.IO.StreamWriter sr = new System.IO.StreamWriter(filePath, true); //foreach (string c in contents) //{ // sr.WriteLine(c); //} //sr.Flush(); //sr.Close();
using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { fs.Seek(fs.Length, SeekOrigin.Current);
string content = String.Join(Environment.NewLine, contents) + Environment.NewLine;
byte[] data = System.Text.Encoding.UTF8.GetBytes(content);
fs.Write(data, 0, data.Length);
fs.Close(); } } }
文件夹2、
2.1 Ilog 接口
public interface ILog { bool IsDebugEnabled { get; }
bool IsErrorEnabled { get; }
bool IsFatalEnabled { get; }
bool IsInfoEnabled { get; }
bool IsWarnEnabled { get; }
void Debug(bool isWriteFile, object message);
void Debug(bool isWriteFile, object message, Exception exception);
void DebugFormat(bool isWriteFile, string format, object arg0);
void DebugFormat(bool isWriteFile, string format, params object[] args);
void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
void DebugFormat(bool isWriteFile, string format, object arg0, object arg1);
void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
void Error(bool isWriteFile, object message);
void Error(bool isWriteFile, object message, Exception exception);
void ErrorFormat(bool isWriteFile, string format, object arg0);
void ErrorFormat(bool isWriteFile, string format, params object[] args);
void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1);
void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
void Fatal(bool isWriteFile, object message);
void Fatal(bool isWriteFile, object message, Exception exception);
void FatalFormat(bool isWriteFile, string format, object arg0);
void FatalFormat(bool isWriteFile, string format, params object[] args);
void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
void FatalFormat(bool isWriteFile, string format, object arg0, object arg1);
void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
void Info(bool isWriteFile, object message);
void Info(bool isWriteFile, object message, Exception exception);
void InfoFormat(bool isWriteFile, string format, object arg0);
void InfoFormat(bool isWriteFile, string format, params object[] args);
void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
void InfoFormat(bool isWriteFile, string format, object arg0, object arg1);
void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2);
void Warn(bool isWriteFile, object message);
void Warn(bool isWriteFile, object message, Exception exception);
void WarnFormat(bool isWriteFile, string format, object arg0);
void WarnFormat(bool isWriteFile, string format, params object[] args);
void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args);
void WarnFormat(bool isWriteFile, string format, object arg0, object arg1);
void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2); }
//新建工厂代理类接口
public interface ILogFactory { ILog GetLog(string name); }
、、实现 ILog接口
public class Log : ILog {
private string m_Name;
private const string m_MessageTemplate = "{0}-{1}: {2}";
private const string m_Debug = "DEBUG";
private const string m_Error = "ERROR";
private const string m_Fatal = "FATAL";
private const string m_Info = "INFO";
private const string m_Warn = "WARN";
public bool isWriteFile = true; /// <summary> /// Initializes a new instance of the <see cref="Log"/> class. /// </summary> /// <param name="name">The name.</param> public Log(string name) { m_Name = name; }
/// <summary> /// Gets a value indicating whether this instance is debug enabled. /// </summary> /// <value> /// <c>true</c> if this instance is debug enabled; otherwise, <c>false</c>. /// </value> public bool IsDebugEnabled { get { return true; } } /// <summary> /// Gets a value indicating whether this instance is error enabled. /// </summary> /// <value> /// <c>true</c> if this instance is error enabled; otherwise, <c>false</c>. /// </value> public bool IsErrorEnabled { get { return true; } }
/// <summary> /// Gets a value indicating whether this instance is fatal enabled. /// </summary> /// <value> /// <c>true</c> if this instance is fatal enabled; otherwise, <c>false</c>. /// </value> public bool IsFatalEnabled { get { return true; } }
/// <summary> /// Gets a value indicating whether this instance is info enabled. /// </summary> /// <value> /// <c>true</c> if this instance is info enabled; otherwise, <c>false</c>. /// </value> public bool IsInfoEnabled { get { return true; } }
/// <summary> /// Gets a value indicating whether this instance is warn enabled. /// </summary> /// <value> /// <c>true</c> if this instance is warn enabled; otherwise, <c>false</c>. /// </value> public bool IsWarnEnabled { get { return true; } }
public string GetDataTimeLog(string log) { return String.Format("[{0}] {1}", DateTime.Now.ToString("yy-MM-dd HH:mm:ss"), log); }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> public void Debug(bool isWriteFile, object message) { string log = GetDataTimeLog(message.ToString()); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> public void Debug(bool isWriteFile, object message, Exception exception) { string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> public void DebugFormat(bool isWriteFile, string format, object arg0) { string log = GetDataTimeLog(string.Format(format, arg0)); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void DebugFormat(bool isWriteFile, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(format, args)); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="provider">The provider.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void DebugFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, string.Format(provider, format, args)); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1) { string log = GetDataTimeLog(string.Format(format, arg0, arg1)); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the debug message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> /// <param name="arg2">The arg2.</param> public void DebugFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) { string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); Console.WriteLine(m_MessageTemplate, m_Name, m_Debug, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Debug, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> public void Error(bool isWriteFile, object message) { string log = GetDataTimeLog(message.ToString()); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> public void Error(bool isWriteFile, object message, Exception exception) { string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> public void ErrorFormat(bool isWriteFile, string format, object arg0) { string log = GetDataTimeLog(string.Format(format, arg0)); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void ErrorFormat(bool isWriteFile, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="provider">The provider.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void ErrorFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) { string log = GetDataTimeLog(string.Format(provider, format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1) { string log = GetDataTimeLog(string.Format(format, arg0, arg1)); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> /// <param name="arg2">The arg2.</param> public void ErrorFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) { string log = GetDataTimeLog(string.Format(format, arg0, arg2)); Console.WriteLine(m_MessageTemplate, m_Name, m_Error, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Error, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> public void Fatal(bool isWriteFile, object message) { string log = GetDataTimeLog(message.ToString()); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> public void Fatal(bool isWriteFile, object message, Exception exception) { string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> public void FatalFormat(bool isWriteFile, string format, object arg0) { string log = GetDataTimeLog(string.Format(format, arg0)); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void FatalFormat(bool isWriteFile, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="provider">The provider.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void FatalFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) { string log = GetDataTimeLog(string.Format(provider, format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1) { string log = GetDataTimeLog(string.Format(format, arg0, arg1)); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the fatal error message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> /// <param name="arg2">The arg2.</param> public void FatalFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) { string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); Console.WriteLine(m_MessageTemplate, m_Name, m_Fatal, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Fatal, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> public void Info(bool isWriteFile, object message) { string log = GetDataTimeLog(message.ToString()); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> public void Info(bool isWriteFile, object message, Exception exception) { string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> public void InfoFormat(bool isWriteFile, string format, object arg0) { string log = GetDataTimeLog(string.Format(format, arg0)); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void InfoFormat(bool isWriteFile, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="provider">The provider.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void InfoFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) { string log = GetDataTimeLog(string.Format(provider, format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1) { string log = GetDataTimeLog(string.Format(format, arg0, arg1)); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the info message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> /// <param name="arg2">The arg2.</param> public void InfoFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) { string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); Console.WriteLine(m_MessageTemplate, m_Name, m_Info, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Info, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> public void Warn(bool isWriteFile, object message) { string log = GetDataTimeLog(message.ToString()); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="message">The message.</param> /// <param name="exception">The exception.</param> public void Warn(bool isWriteFile, object message, Exception exception) { string log = GetDataTimeLog(message + Environment.NewLine + exception.Message + exception.StackTrace); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> public void WarnFormat(bool isWriteFile, string format, object arg0) { string log = GetDataTimeLog(string.Format(format, arg0)); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void WarnFormat(bool isWriteFile, string format, params object[] args) { string log = GetDataTimeLog(string.Format(format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="provider">The provider.</param> /// <param name="format">The format.</param> /// <param name="args">The args.</param> public void WarnFormat(bool isWriteFile, IFormatProvider provider, string format, params object[] args) { string log = GetDataTimeLog(string.Format(provider, format, args)); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1) { string log = GetDataTimeLog(string.Format(format, arg0, arg1)); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } }
/// <summary> /// Logs the warning message. /// </summary> /// <param name="isWriteFile"></param> /// <param name="format">The format.</param> /// <param name="arg0">The arg0.</param> /// <param name="arg1">The arg1.</param> /// <param name="arg2">The arg2.</param> public void WarnFormat(bool isWriteFile, string format, object arg0, object arg1, object arg2) { string log = GetDataTimeLog(string.Format(format, arg0, arg1, arg2)); Console.WriteLine(m_MessageTemplate, m_Name, m_Warn, log); if (isWriteFile) { LogUtil.WriteLogFile(m_Name, m_Warn, log); } } }
、、实现 ILogFactory工厂接口
public class LogFactory : ILogFactory { /// <summary> /// 创建日志实例 /// </summary> /// <param name="name"></param> /// <returns></returns> public ILog GetLog(string name) { return new Log(name); } }
新建LogUtil类 写入日志文件类
public class LogUtil { /// <summary> /// 格式式化Log信息 /// </summary> /// <param name="format"></param> /// <param name="name"></param> /// <param name="logType"></param> /// <param name="log"></param> /// <returns></returns> private static string GetLogString(string name, string logType, string log) { return String.Format("[{0}]{1}-{2}: {3}", DateTime.Now.ToString("HH:mm:ss"), name, logType, log); }
/// <summary> /// 获得日志要保存的路径 /// </summary> /// <param name="name"></param> /// <param name="logType"></param> /// <returns></returns> private static string GetLogPath(string name, string logType) { string path = AppDomain.CurrentDomain.BaseDirectory + "Log"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); }
return System.IO.Path.Combine(path, String.Format("{0}_{1}_{2}.log", DateTime.Now.ToString("yyyy-MM-dd"), name, logType)); }
public static void WriteLogFile(string name, string logType, string log) { string logPath = GetLogPath(name, logType);
FileUtil.WriteAppend(logPath, log); } }
|
请发表评论