LogHelper.cs 1.06 KB
using log4net;
using log4net.Config;
using log4net.Repository;
using System.IO;

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]
namespace Infrastructure
{
    public class LogHelper
    {
        private static ILog logger = null;

        public static void LogError(string logstr)
        {
            if (InitLog4net())
                logger.Error(logstr);
        }

        private static object objlock = new object();//初始化log用的锁
        private static bool InitLog4net()
        {
            if (logger != null)
                return true;
            lock (objlock)
            {
                if (logger == null)
                {
                    ILoggerRepository repository = LogManager.CreateRepository("NETCoreRepository");
                    XmlConfigurator.Configure(repository, new FileInfo("log4net.config"));
                    logger = LogManager.GetLogger(repository.Name, "NETCorelog4net");
                    return true;
                }
            }
            return false;
        }
    }
}