C#日志记录设计与实现

日志记录:

日志记录在程序设计开发过程中,是非常重要的,可以供调试和记录数据,虽然说有开源的强大日志管理系统,比如apache的Log4Net,功能可谓强悍,但是有时候,不需要这么大的日志,只需要显示和文件记录就可以了,没必要用这么重的日志系统,那么就需要自己来写,如下就是一个简单的日志记录和显示模块的设计和实现,如有不足,还望见谅!废话不多,直入主题。

笨小孩日志:BenXHLog

类文件设计:

文件结构简单,类图就不画了,细心的已经发现了,这就是一个简单工厂模式,

程序代码:

Ilog接口

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXH.Log.Log
{
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);
}
}

ILogFactory工厂接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXH.Log.Log
{
public interface ILogFactory
{
ILog GetLog(string name);
}
}

日志类Log 这个代码实现多一点,合并了,点开看吧

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXH.Log.Log
{
/// <summary>
/// Console Log
/// </summary>
internal 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"; /// <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);
}
}
}
}

LogFactory 日志工厂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXH.Log.Log
{
public class LogFactory:ILogFactory
{
/// <summary>
/// 创建日志实例
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public ILog GetLog(string name)
{
return new Log(name);
}
}
}

LogUtil日志格式化

 using BenXH.Log.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace BenXH.Log.Log
{
internal 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);
}
}
}

最后就是一个日志信息的显示和文件的存储   FileUtil

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; namespace BenXH.Log.Common
{
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, , data.Length); fs.Close();
}
}
}
}

测试代码:

Test.cs

  class Test
{
static void Main(string[] args)
{
//日志BenXH的Debug信息
new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "Hello");
new BenXH.Log.Log.LogFactory().GetLog("BenXH").Debug(true, "World"); //日志BenXH的info信息
new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "Hello");
new BenXH.Log.Log.LogFactory().GetLog("BenXH").Info(true, "BenXH");
Console.Read();
}
}

运行结果:

文件:程序运行根目录下Log文件夹

Log文件夹下新创建俩个文件DEBUG和INFO

文件:2017-07-29_BenXH_DEBUG.log

文件内容

[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>World

文件:2017-07-29_BenXH_INFO.log

文件内容:

[17-07-29 16:01:26]>>Hello
[17-07-29 16:01:26]>>BenXH

工程源文件下载

C#日志记录设计与实现(BenXHLog)的更多相关文章

  1. TP5数据库数据变动日志记录设计

    根据网友的设计进行了部分调整: 用户分为管理员admin表和用户user表 记录操作表数据 增删改: insert/delete/update <?php /** * OperateLog.ph ...

  2. TinyFrame升级之六:全局日志的设计及实现

    日志记录显然是框架设计中不可或缺的元素,在本框架中,我们将使用log4net作为日志记录的主体.下面来具体说明如何让框架继承log4net,并通过Autofac进行IOC注入. 首先,定义好我们的Lo ...

  3. Java日志记录的事儿

    一.java日志组件 1.common-logging common-logging是apache提供的一个通用的日志接口.用户可以自由选择第三方的日志组件作为具体实现,像log4j,或者jdk自带的 ...

  4. IT第二十天 - 面向对象编程思想、抽象类、异常处理、程序操作日志记录、本周总结 ★★★

    IT第二十天 上午 面向对象编程思想 1.组装电脑的设计: (1)电脑的组成:显示器+机箱 (2)机箱的组成:电源+主板+硬盘 (3)主板所包含的部件:cpu+内存+PCI接口+usb接口 (4)PC ...

  5. 扔掉log4j、log4j2,自己动手实现一个多功能日志记录框架,包含文件,数据库日志写入,实测5W+/秒日志文件写入,2W+/秒数据库日志写入,虽然它现在还没有logback那么强大

    讲到log4j,现在国外基本是没有开发者用这个框架了,原因大致有几点,1.功能太少:2.效率低下:3.线程锁bug等等等各种莫名其妙的bug一直都没解决. 其实最重要的是log4j的作者自己也放弃了l ...

  6. 来一手 AOP 注解方式进行日志记录

    系统日志对于定位/排查问题的重要性不言而喻,相信许多开发和运维都深有体会. 通过日志追踪代码运行状况,模拟系统执行情况,并迅速定位代码/部署环境问题. 系统日志同样也是数据统计/建模的重要依据,通过分 ...

  7. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十 || AOP面向切面编程浅解析:简单日志记录 + 服务切面缓存

    代码已上传Github+Gitee,文末有地址 上回<从壹开始前后端分离[ .NET Core2.0 Api + Vue 2.0 + AOP + 分布式]框架之九 || 依赖注入IoC学习 + ...

  8. 从壹开始前后端分离 [.netCore 不定期更新 ] 三十五║ 完美实现全局异常日志记录

    缘起 哈喽我是不定期更新的日常,昨天群里小伙伴问到了记录日志,当然,以前我也挖过这个坑,后来一直没有来得及填上,也想着 swagger 一直又有错误信息展示的功能,就迟迟没有添加这个功能,不过昨天夜里 ...

  9. Python开发之日志记录模块:logging

    1 引言 最近在开发一个应用软件,为方便调试和后期维护,在代码中添加了日志,用的是Python内置的logging模块,看了许多博主的博文,颇有所得.不得不说,有许多博主大牛总结得确实很好.似乎我再写 ...

随机推荐

  1. android: LayoutInflater使用

    1. 题外话 相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个I ...

  2. iOS 转换异步block为同步方式运行

    使用dispatch_semaphore_t 实现 dispatch_semaphore_t sema = dispatch_semaphore_create(0); //创建信号量 __block ...

  3. 错误<BEA-101165> <Could not load user defined filter in web.xml

    <2017-7-4 上午10时13分33秒 CST> <Error> <HTTP> <BEA-101165> <Could not load us ...

  4. Comparison method violates its general contract 解决

    java.lang.IllegalArgumentException: Comparison method violates its general contract! 原因 JDK7中的Collec ...

  5. 【OpenCV】OpenCV中GPU模块使用 (转)

    CUDA基本使用方法 在介绍OpenCV中GPU模块使用之前,先回顾下CUDA的一般使用方法,其基本步骤如下: 1.主机代码执行:2.传输数据到GPU:3.确定grid,block大小: 4.调用内核 ...

  6. sublime text修改package安装路径

    删除C:\Users\Administrator\AppData\Roaming\Sublime Text 3下面的packages目录 在\Sublime Text 3安装路径下新建Data文件夹 ...

  7. !HDU 1574 RP-dp-(重点在状态确定)

    题意:有n件事.每件事若发生有两种情况.添加RP为a,可是收益会降低c:降低R为a,收益会添加c. 每件事可以发生的前提是小于等于或者大于等于门限值b.求最大收益. 分析:这题我没找到状态,所以就不会 ...

  8. 12C -- 配置Application Continuity

     

  9. 11g新特性 -- Virtual Private Catalogs

    在oracle 11g之前版本中,如果用户只是需要访问recovery catalog 中的某个数据库,但是我们却只能将整个recovery catalog授权给他,无法做到严格限制. 现在,virt ...

  10. FAL_CLIENT和FAL_SERVER参数详解

    FAL_CLIENT和FAL_SERVER参数详解 转载:http://openwares.net/database/fal_client_fal_server.html FAL_CLIENT和FAL ...