C#-Log4net 패키지 log 클래스 및 사용자 정의 log 저장 경로

10665 단어 C#
인용 dll:log4net.dll
 
인터페이스 클래스: Ilogger.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HCCD.Base.Comm.NewLogger
{
    /// 
    ///     
    /// 
    public interface ILogger
    {
        /// 
        ///       
        /// 
        ///     
        void Debug(string msg);

        /// 
        ///       
        /// 
        ///     
        ///     
        void Debug(string msg, Exception ex);

        /// 
        ///       
        /// 
        ///     
        void Info(string msg);

        /// 
        ///       
        /// 
        ///     
        ///     
        void Info(string msg, Exception ex);

        /// 
        ///       
        /// 
        ///     
        void Warn(string msg);

        /// 
        ///       
        /// 
        ///     
        ///     
        void Warn(string msg, Exception ex);

        /// 
        ///       
        /// 
        ///     
        void Error(string msg);

        /// 
        ///       
        /// 
        ///     
        ///     
        void Error(string msg, Exception ex);

        /// 
        ///       
        /// 
        ///     
        void Fatal(string msg);

        /// 
        ///       
        /// 
        ///     
        ///     
        void Fatal(string msg, Exception ex);
    }
}

인터페이스 구현 클래스: LogHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using log4net.Appender;
using System.IO;
using log4net.Config;

namespace HCCD.Base.Comm.NewLogger
{
    /*
     * Author: Long
     * Date: 2019-09
     * Detail: Log4net   
     */

    /// 
    /// Log4net   
    /// 
    public class LogHelper : ILogger
    {
        private Dictionary LogDic = new Dictionary();
        private object _islock = new object();
        private string fileName = string.Empty;

        /// 
        ///        
        /// 
        ///         [     ,        Logger   ;]
        ///      [      ,      :Default]
        public LogHelper(string fileSavePath, string fileName,string logSuffix = ".log")
        {
            try
            {
                Init();
                if (string.IsNullOrEmpty(fileSavePath))
                    fileSavePath = "Logger";
                if (string.IsNullOrEmpty(fileName))
                    fileName = "Default";

                this.fileName = fileName;
                var repository = LogManager.GetRepository();
                var appenders = repository.GetAppenders();

                if (appenders.Length == 0) return;
                var targetApder = appenders.First(p => p.Name == "FileInfoAppender") as RollingFileAppender;
                targetApder.File = Path.Combine(fileSavePath, this.fileName + logSuffix);
                targetApder.ActivateOptions();
            }
            catch (Exception ex) { }
        }

        /// 
        ///       
        /// 
        /// 
        /// 
        private ILog GetLog(string name)
        {
            try
            {
                if (LogDic == null)
                {
                    LogDic = new Dictionary();
                }
                lock (_islock)
                {
                    if (!LogDic.ContainsKey(name))
                    {
                        LogDic.Add(name, LogManager.GetLogger(name));
                    }
                }
                return LogDic[name];
            }
            catch
            {
                return LogManager.GetLogger("Default");
            }
        }

        /// 
        ///        
        /// 
        private void Init()
        {
            var file = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log4net.config"));
            XmlConfigurator.Configure(file);
        }

        /// 
        ///       
        /// 
        ///     
        public void Debug(string msg)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Debug(msg);
        }

        /// 
        ///       
        /// 
        ///     
        ///     
        public void Debug(string msg, Exception ex)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Debug(msg, ex);
        }


        /// 
        ///       
        /// 
        ///     
        public void Info(string msg)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Info(msg);
        }

        /// 
        ///       
        /// 
        ///     
        ///     
        public void Info(string msg, Exception ex)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Info(msg, ex);
        }

        /// 
        ///       
        /// 
        ///     
        public void Warn(string msg)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Warn(msg);
        }

        /// 
        ///       
        /// 
        ///     
        ///     
        public void Warn(string msg, Exception ex)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Warn(msg, ex);
        }

        /// 
        ///       
        /// 
        ///     
        public void Error(string msg)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Error(msg);
        }

        /// 
        ///       
        /// 
        ///     
        ///     
        public void Error(string msg, Exception ex)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Error(msg, ex);
        }

        /// 
        ///       
        /// 
        ///     
        public void Fatal(string msg)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Fatal(msg);
        }

        /// 
        ///       
        /// 
        ///     
        ///     
        public void Fatal(string msg, Exception ex)
        {
            var log = GetLog(this.fileName);
            if (log == null)
            {
                return;
            }
            log.Fatal(msg, ex);
        }
    }
}

구성 클래스: Log4net.config


  
    

 

 

调用方式

引用:log4net.dll 

           HCC.Base.Comm.NewLogger.dll

实现:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCC.Base.Comm.NewLogger;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            LogHelper log = new LogHelper(@"D:\Logger", "stydy");

            log.Info("                   ");
            log.Info("                   ", new Exception("       !          。"));

            log.Fatal("gdsgdsgdsgdsgdsgds");
            log.Fatal("gdsgdsgdsgdsgdsgds", new Exception("       !          。"));

            log.Warn("             ");
            log.Warn("gdsgdsgdsgdsgdsgds", new Exception("       !          。"));

            log.Debug("cbvcbvfgsfgdsgdsgdsgdsgdsgds");
            log.Debug("gdsgdsgdsgdsgdsgds", new Exception("       !          。"));

            log.Error("    ");
            log.Error("    ",new Exception("       !          。"));
        }
    }
}

좋은 웹페이지 즐겨찾기