`
jie_bosshr
  • 浏览: 139901 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

winform集成log4net框架

    博客分类:
  • .net
阅读更多
说明:使用winform集成log4net框架操作采用代码方式加载log4net引擎。
1 用户可以从http://logging.apache.org/log4net/下载log4net的源代码。解压软件包后,在解压的src目录下将log4net.sln载入Visual Studio .NET,编译后可以得到log4net.dll。用户要在自己的程序里加入日志功能,只需将log4net.dll引入工程即可。
2 新建Project,名称为:MyLog4net,框架选择.NET Framework2.0
3 新建类Logger如下:
using System;
using System.Collections.Generic;
using System.Text;
using log4net;

namespace MyLog4net
{
    public class Logger
    {
        #region
        public static ILog init()
        {
            DateTime now = DateTime.Now;
            int year = now.Year;
            int month = now.Month;
            int date = now.Day;
            int hour = now.Hour;
            int minute = now.Minute;
            int second = now.Second;
            int millisecond = now.Millisecond;
            string format = year + "/" + month + "/" + date + " " + hour + ":" + minute + ":" + second + ":" + millisecond;
            //string pattern = "["+format+"]%n MESSAGE:%message 日志级别:%-5level%n";
            string pattern = "[" + format + "]%n %-5level:[%message]%n%n";
            log4net.Layout.PatternLayout pl = new log4net.Layout.PatternLayout(pattern);
            log4net.Appender.FileAppender file = new log4net.Appender.FileAppender(pl, "D:/log.txt");
            log4net.Config.BasicConfigurator.Configure(file);
            return log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        }

        public static void Debug(object message, Exception exception)
        {
            ILog log = init();
            log.Debug(message, exception);
        }

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

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

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

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

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

        public static void Info(object message, Exception exception)
        {
            ILog log = init();
            log.Info(message, exception);
        }

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

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

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

        #endregion
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics