1.写入文本txt日志
创建一个StreamWriter,将文件写入进去
public static void WriteLog(string msg) { System.IO.StreamWriter sw = null; if (sw != null) { try { //同一天同一类日志以追加形式保存,如果要一个小时新增一个日志文件,只需要改成yyyyMMddHH即可 sw = System.IO.File.AppendText( LogPath + LogFielPrefix + "_" + DateTime.Now.ToString("yyyyMMdd") + ".Log" ); sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg); } catch { } finally { sw.Close(); } }
2.利用Log4net实现日志,需要引用Log4net.dll
1.首先创建一个ILog接口
using System; namespace WQB.Logging { /// <summary> /// The ILog interface is used by the client to log messages. /// </summary> /// <remarks>Use the <see cref="T:WQB.Logging.LogManager" /> class to programmatically assign logger implementations.</remarks> public interface ILog { bool IsDebugEnabled { get; } bool IsInfoEnabled { get; } bool IsWarnEnabled { get; } bool IsErrorEnabled { get; } bool IsFatalEnabled { get; } void Debug(object message); void Debug(object message, Exception exception); void DebugFormat(string format, object arg0); void DebugFormat(string format, object arg0, object arg1); void DebugFormat(string format, object arg0, object arg1, object arg2); void DebugFormat(string format, params object[] args); void DebugFormat(IFormatProvider provider, string format, params object[] args); void Info(object message); void Info(object message, Exception exception); void InfoFormat(string format, object arg0); void InfoFormat(string format, object arg0, object arg1); void InfoFormat(string format, object arg0, object arg1, object arg2); void InfoFormat(string format, params object[] args); void InfoFormat(IFormatProvider provider, string format, params object[] args); void Warn(object message); void Warn(object message, Exception exception); void WarnFormat(string format, object arg0); void WarnFormat(string format, object arg0, object arg1); void WarnFormat(string format, object arg0, object arg1, object arg2); void WarnFormat(string format, params object[] args); void WarnFormat(IFormatProvider provider, string format, params object[] args); void Error(object message); void Error(object message, Exception exception); void ErrorFormat(string format, object arg0); void ErrorFormat(string format, object arg0, object arg1); void ErrorFormat(string format, object arg0, object arg1, object arg2); void ErrorFormat(string format, params object[] args); void ErrorFormat(IFormatProvider provider, string format, params object[] args); void Fatal(object message); void Fatal(object message, Exception exception); void FatalFormat(string format, object arg0); void FatalFormat(string format, object arg0, object arg1); void FatalFormat(string format, object arg0, object arg1, object arg2); void FatalFormat(string format, params object[] args); void FatalFormat(IFormatProvider provider, string format, params object[] args); } }
2.创建一个ILogFactory日志工厂接口
using System; namespace WQB.Logging { /// <summary> /// Implement this interface to instantiate your custom ILog implementation /// </summary> public interface ILogFactory { ILog GetLogger(string name); ILog GetLogger(Type type); } }
3.创建一个Log4net的工厂Log4NetFactory实现日志工厂接口ILogFactory
using System; namespace WQB.Logging { /// <summary> /// log4net log factory /// </summary> public class Log4NetFactory : ILogFactory { ILog ILogFactory.GetLogger(string name) { return new Log4NetWrapper(log4net.LogManager.GetLogger(name)); } ILog ILogFactory.GetLogger(Type type) { return new Log4NetWrapper(log4net.LogManager.GetLogger(type)); } } }
4.创建一个Wrapper实现ILog
using System; namespace WQB.Logging { internal class Log4NetWrapper : ILog { private log4net.ILog log; public Log4NetWrapper(log4net.ILog log) { this.log = log; } #region [ ILog ] bool ILog.IsDebugEnabled { get { return this.log.IsDebugEnabled; } } bool ILog.IsInfoEnabled { get { return this.log.IsInfoEnabled; } } bool ILog.IsWarnEnabled { get { return this.log.IsWarnEnabled; } } bool ILog.IsErrorEnabled { get { return this.log.IsErrorEnabled; } } bool ILog.IsFatalEnabled { get { return this.log.IsFatalEnabled; } } void ILog.Debug(object message) { this.log.Debug(message); } void ILog.Debug(object message, Exception exception) { this.log.Debug(message, exception); } void ILog.DebugFormat(string format, object arg0) { this.log.DebugFormat(format, arg0); } void ILog.DebugFormat(string format, object arg0, object arg1) { this.log.DebugFormat(format, arg0, arg1); } void ILog.DebugFormat(string format, object arg0, object arg1, object arg2) { this.log.DebugFormat(format, arg0, arg1, arg2); } void ILog.DebugFormat(string format, params object[] args) { this.log.DebugFormat(format, args); } void ILog.DebugFormat(IFormatProvider provider, string format, params object[] args) { this.log.DebugFormat(provider, format, args); } void ILog.Info(object message) { this.log.Info(message); } void ILog.Info(object message, Exception exception) { this.log.Info(message, exception); } void ILog.InfoFormat(string format, object arg0) { this.log.InfoFormat(format, arg0); } void ILog.InfoFormat(string format, object arg0, object arg1) { this.log.InfoFormat(format, arg0, arg1); } void ILog.InfoFormat(string format, object arg0, object arg1, object arg2) { this.log.InfoFormat(format, arg0, arg1, arg2); } void ILog.InfoFormat(string format, params object[] args) { this.log.InfoFormat(format, args); } void ILog.InfoFormat(IFormatProvider provider, string format, params object[] args) { this.log.InfoFormat(provider, format, args); } void ILog.Warn(object message) { this.log.Warn(message); } void ILog.Warn(object message, Exception exception) { this.log.Warn(message, exception); } void ILog.WarnFormat(string format, object arg0) { this.log.WarnFormat(format, arg0); } void ILog.WarnFormat(string format, object arg0, object arg1) { this.log.WarnFormat(format, arg0, arg1); } void ILog.WarnFormat(string format, object arg0, object arg1, object arg2) { this.log.WarnFormat(format, arg0, arg1, arg2); } void ILog.WarnFormat(string format, params object[] args) { this.log.WarnFormat(format, args); } void ILog.WarnFormat(IFormatProvider provider, string format, params object[] args) { this.log.WarnFormat(provider, format, args); } void ILog.Error(object message) { this.log.Error(message); } void ILog.Error(object message, Exception exception) { this.log.Error(message, exception); } void ILog.ErrorFormat(string format, object arg0) { this.log.ErrorFormat(format, arg0); } void ILog.ErrorFormat(string format, object arg0, object arg1) { this.log.ErrorFormat(format, arg0, arg1); } void ILog.ErrorFormat(string format, object arg0, object arg1, object arg2) { this.log.ErrorFormat(format, arg0, arg1, arg2); } void ILog.ErrorFormat(string format, params object[] args) { this.log.ErrorFormat(format, args); } void ILog.ErrorFormat(IFormatProvider provider, string format, params object[] args) { this.log.ErrorFormat(provider, format, args); } void ILog.Fatal(object message) { this.log.Fatal(message); } void ILog.Fatal(object message, Exception exception) { this.log.Fatal(message, exception); } void ILog.FatalFormat(string format, object arg0) { this.log.FatalFormat(format, arg0); } void ILog.FatalFormat(string format, object arg0, object arg1) { this.log.FatalFormat(format, arg0, arg1); } void ILog.FatalFormat(string format, object arg0, object arg1, object arg2) { this.log.FatalFormat(format, arg0, arg1, arg2); } void ILog.FatalFormat(string format, params object[] args) { this.log.FatalFormat(format, args); } void ILog.FatalFormat(IFormatProvider provider, string format, params object[] args) { this.log.FatalFormat(provider, format, args); } #endregion } }
5.创建一个日志对象LogManager
using System; using System.Configuration; using System.IO; namespace WQB.Logging { /// <summary> /// Creates loggers based on the current configuration. /// </summary> /// <example> /// /// /// Code: /// /// private static readonly WQB.Logging.ILog log = WQB.Logging.LogManager.GetLogger(typeof(DefaultServerPool)); /// log.Debug("hello world."); /// </example> public static class LogManager { private static ILogFactory factory; static LogManager() { // use the log4net logger LogManager.factory = (ILogFactory)new Log4NetFactory(); } /// <summary> /// Assigns a new logger factory programmatically. /// </summary> /// <param name="factory"></param> public static void AssignFactory(ILogFactory factory) { if (factory == null) throw new ArgumentNullException("factory"); LogManager.factory = factory; } /// <summary> /// Returns a new logger for the specified Type. /// </summary> /// <param name="type"></param> /// <returns></returns> public static ILog GetLogger(Type type) { return factory.GetLogger(type); } /// <summary> /// Returns a logger with the specified name. /// </summary> /// <param name="name"></param> /// <returns></returns> public static ILog GetLogger(string name) { return factory.GetLogger(name); } } }
创建日志对象,添加日志如下操作即可:
private static readonly ILog logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
logger.Error(msg);
6.在web.config中配置log4net
在configSections节点内部添加节点log4net的节点即可,如下:
<section name="log4net" type="System.Configuration.IgnoreSectionHandler"/>