log4net 写日志

通过log4net实现日志的记录

1. 下载log4net dll

2. webconfig配置log4net

 <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="log4net" type="System.Configuration.IgnoreSectionHandler" />
  </configSections>
在configuration节点中添加 
<log4net> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file type="log4net.Util.PatternString" value="Logs/%date{yyyy-MM-dd}.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyy-MM-dd" /> <maxSizeToRollBackups value="10" /> <maximumFileSize value="3000kb" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout,log4net"> <param name="ConversionPattern" value="%d [%t] %-5p %c [%L] - %m%n" /> </layout> </appender> <root> <level value="All" /> <appender-ref ref="RollingLogFileAppender" /> </root> </log4net>

3.封装AppLog方法

/// <summary>
    /// 使用Log4net插件的log日志对象
    /// </summary>
    public static class AppLog
    {
        private static ILog log;

        static AppLog()
        {
            XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
            log = LogManager.GetLogger(typeof(AppLog));
        }

        public static void Debug(object message)
        {
            log.Debug(message);
        }

        public static void DebugFormatted(string format, params object[] args)
        {
            log.DebugFormat(format, args);
        }

        public static void Info(object message)
        {
            log.Info(message);
        }

        public static void InfoFormatted(string format, params object[] args)
        {
            log.InfoFormat(format, args);
        }

        public static void Warn(object message)
        {
            log.Warn(message);
        }

        public static void Warn(object message, Exception exception)
        {
            log.Warn(message, exception);
        }

        public static void WarnFormatted(string format, params object[] args)
        {
            log.WarnFormat(format, args);
        }

        public static void Error(object message)
        {
            log.Error(message);
        }

        public static void Error(object message, Exception exception)
        {
            log.Error(message, exception);
        }

        public static void ErrorFormatted(string format, params object[] args)
        {
            log.ErrorFormat(format, args);
        }

        public static void Fatal(object message)
        {
            log.Fatal(message);
        }

        public static void Fatal(object message, Exception exception)
        {
            log.Fatal(message, exception);
        }

        public static void FatalFormatted(string format, params object[] args)
        {
            log.FatalFormat(format, args);
        }
    }

完成。

上一篇:C# web程序,winform程序,控制台程序配置log4net,使用log4net


下一篇:log4net配置