winform 통합 log4net 프레임 워 크

3663 단어 C#log4net
설명: winform 통합 log4net 프레임 워 크 를 사용 하여 코드 방식 으로 log4net 엔진 을 불 러 옵 니 다.
사용자http://logging.apache.org/log4net/log4net 의 소스 코드 를 다운로드 합 니 다.압축 해제 패키지 후 압축 해제 src 디 렉 터 리 에서 log4net. sln 을 Visual Studio. NET 에 불 러 오고 컴 파일 후 log4net. dll 을 얻 을 수 있 습 니 다.사용 자 는 자신의 프로그램 에 로그 기능 을 추가 하려 면 log 4 net. dll 을 프로젝트 에 도입 하면 됩 니 다.
2 새 Project, 이름: MyLog4net, 프레임 선택. NET Framework 2.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
    }
}

좋은 웹페이지 즐겨찾기