コード例 #1
0
ファイル: Log4net.cs プロジェクト: usamabintariq/NCache
        /// <summary>
        /// Creates Buffer Appender, Responsible for storing all logging events in memory buffer
        /// and writing them down when by passing the logging events on to the file appender it holds
        /// when its buffer becomes full
        /// Can also be made a lossy logger which writes only writes down to a file when a specific crieteria/condition is met
        /// </summary>
        /// <param name="cacheName">CacheName used to name the Buffer Appender</param>
        /// <param name="fileName">File name to log into</param>
        /// <returns>Returns the created Appender</returns>
        private static log4net.Appender.IAppender CreateBufferAppender(string cacheName, string fileName)
        {
            log4net.Appender.BufferingForwardingAppender appender = new BufferingForwardingAppender();
            appender.Name = "BufferingForwardingAppender" + cacheName;
            //Pick from config
            int bufferSize = NCacheLog.bufferDefaultSize;

            NCacheLog.ReadConfig(out bufferSize);
            if (bufferSize == NCacheLog.bufferDefaultSize)
            {
                NCacheLog.ReadClientConfig(out bufferSize);
            }

            if (bufferSize < 1)
            {
                bufferSize = NCacheLog.bufferDefaultSize;
            }

            appender.BufferSize = bufferSize;

            //Threshold is maintained by the logger rather than the appenders
            appender.Threshold = log4net.Core.Level.All;

            //Adds the appender to which it will pass on all the logging levels upon filling up the buffer
            appender.AddAppender(CreateRollingFileAppender(cacheName, fileName));

            //necessary to apply the appender property changes
            appender.ActivateOptions();

            return(appender);
        }
コード例 #2
0
        public static bool IsDebugEnabled(string cacheName)
        {
            #region isEnabledLogic
            if (cacheName == null || cacheName == string.Empty)
            {
                NCacheLog.LogLoggingError("Cache Name is null");
                return(false);
            }
            string loggerName = LoggingInformation.GetLoggerName(cacheName);
            if (loggerName != null && loggerName.Length > 0)
            {
                log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
                return(log.IsInfoEnabled);
            }
            else
            {
                NCacheLog.LogLoggingError("loggerName != null && loggerName.Length > 0");
            }

            return(false);

            #endregion

            return(true);
        }
コード例 #3
0
ファイル: NCacheLog.cs プロジェクト: wangchengqun/NCache
        public static bool[] ReadClientConfig(out int bufferAppender)
        {
            try
            {
                string EnableLogs         = System.Configuration.ConfigurationManager.AppSettings["EnableNCWebLogs"];
                string EnableDetailedLogs = System.Configuration.ConfigurationManager.AppSettings["EnableDetailedNCWebLogs"];
                string BufferSize         = System.Configuration.ConfigurationManager.AppSettings["BufferSize"];


                try
                {
                    if (BufferSize != null)
                    {
                        bufferAppender = Convert.ToInt32(BufferSize);
                    }
                    else
                    {
                        bufferAppender = bufferDefaultSize;
                    }
                }
                catch (Exception)
                {
                    bufferAppender = bufferDefaultSize;
                }

                if (EnableDetailedLogs == null && EnableLogs == null)
                {
                    return(new bool[2] {
                        false, false
                    });
                }
                else if (EnableDetailedLogs != null && EnableLogs == null)
                {
                    return(new bool[2] {
                        false, Convert.ToBoolean(EnableDetailedLogs)
                    });
                }
                else if (EnableDetailedLogs == null && EnableLogs != null)
                {
                    return(new bool[2] {
                        Convert.ToBoolean(EnableLogs), false
                    });
                }
                else
                {
                    return(new bool[2] {
                        Convert.ToBoolean(EnableLogs), Convert.ToBoolean(EnableDetailedLogs)
                    });
                }
            }
            catch (Exception ex)
            {
                bufferAppender = bufferDefaultSize;
                return(new bool[2] {
                    false, false
                });

                NCacheLog.LogLoggingError(ex.Message);
            }
        }
コード例 #4
0
ファイル: NCacheLog.cs プロジェクト: wangchengqun/NCache
        public static void OnClientChange(object _o, FileSystemEventArgs _a)
        {
            bool[] logLevel   = new bool[2];
            int    bufferSize = bufferDefaultSize;

            lock (syncLock)
            {
                if (_timeFired != null)
                {
                    if (DateTime.Now.Subtract(_timeFired).TotalMilliseconds < 50)
                    {
                        return;
                    }
                }
                _timeFired = DateTime.Now;


                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                logLevel = ReadClientConfig(out bufferSize);
            }

            foreach (string loggerName in LoggingInformation.cacheLogger.Values)
            {
                NCacheLog.SetLevel(loggerName, logLevel[1] == true ? NCacheLog.Level.ALL : logLevel[0] == true ? NCacheLog.Level.INFO : NCacheLog.Level.OFF);
            }

            if (bufferSize < 1)
            {
                bufferSize = bufferDefaultSize;
            }

            NCacheLog.SetBufferSize(bufferSize);
        }
コード例 #5
0
ファイル: LoggingInformation.cs プロジェクト: yaobos/NCache
 internal static string GetStaticLoggerName(string name)
 {
     if (name == null || name == string.Empty)
     {
         NCacheLog.LogLoggingError("CacheName Empty");
         return(null);
     }
     else
     {
         return((string)staticCacheLogger[name]);
     }
 }
コード例 #6
0
ファイル: LoggingInformation.cs プロジェクト: yaobos/NCache
 /// <summary>
 /// Returns the logger Name w.r.t the cache Name
 /// </summary>
 /// <param name="cacheName">CacheName to which the loggername is associated</param>
 /// <returns>returns the logger Name, if not found null is returned</returns>
 internal static string GetLoggerName(string cacheName)
 {
     if (cacheName == null || cacheName == string.Empty)
     {
         NCacheLog.LogLoggingError("CacheName Empty");
         return(null);
     }
     else
     {
         return((string)cacheLogger[cacheName]);
     }
 }
コード例 #7
0
        public static void OnChange()
        {
            bool[] logLevel   = new bool[2];
            int    bufferSize = bufferDefaultSize;

            lock (syncLock)
            {
                if (_timeFired != null)
                {
                    if (DateTime.Now.Subtract(_timeFired).TotalMilliseconds < 50)
                    {
                        return;
                    }
                }
                _timeFired = DateTime.Now;


                System.Configuration.ConfigurationManager.RefreshSection("appSettings");
                logLevel = ReadConfig(out bufferSize);
            }

            string loggerName = LoggingInformation.GetStaticLoggerName(LoggerNames.SocketServerLogs.ToString());

            if (loggerName != null)
            {
                log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
                log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;

                BufferingAppenderSkeleton buffered = (BufferingAppenderSkeleton)l.GetAppender("BufferingForwardingAppender" + loggerName);
                if (buffered is BufferingForwardingAppender)
                {
                    ((BufferingForwardingAppender)buffered).Flush();
                }
            }

            //if not already initialized
            Log4net.Initialize(LoggerNames.SocketServerLogs);

            NCacheLog.SetLevel(LoggerNames.SocketServerLogs.ToString(), logLevel[1] == true ? NCacheLog.Level.ALL : logLevel[0] == true ? NCacheLog.Level.INFO : NCacheLog.Level.OFF);

            if (bufferSize < 1)
            {
                bufferSize = bufferDefaultSize;
            }

            NCacheLog.SetBufferSize(bufferSize);
        }
コード例 #8
0
        public static bool IsDebugEnabled(NCacheLog.LoggerNames loggerEnum)
        {
            #region isEnabledLogic
            string temp = LoggingInformation.GetStaticLoggerName(loggerEnum.ToString());
            if (temp != null && temp.Length > 0)
            {
                string loggerName = LoggingInformation.GetLoggerName(temp);
                if (loggerName != null && loggerName.Length > 0)
                {
                    log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
                    return(log.IsDebugEnabled);
                }
                else
                {
                    NCacheLog.LogLoggingError("loggerName != null && loggerName.Length > 0");
                }
            }
            return(false);

            #endregion
        }
コード例 #9
0
        public static void Warn(string cacheName, string message)
        {
            if (cacheName == null || cacheName == string.Empty)
            {
                NCacheLog.LogLoggingError("Cache Name is null");
                return;
            }
            string loggerName = LoggingInformation.GetLoggerName(cacheName);

            if (loggerName != null && loggerName.Length > 0)
            {
                log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
                if (message.Contains(Environment.NewLine))
                {
                    message = message + "\r\n";
                }
                log.Warn(message);
            }
            else
            {
                NCacheLog.LogLoggingError("loggerName != null && loggerName.Length > 0");
            }
        }
コード例 #10
0
ファイル: NCacheLog.cs プロジェクト: javithalion/NCache
 public static void Warn(NCacheLog.LoggerNames loggerName, String module, String message)
 {
     string name = loggerName.ToString(); ;
     if (loggerName != LoggerNames.ClientLogs)
         name = LoggingInformation.GetStaticLoggerName(loggerName.ToString());
     Warn(name, module, message);
 }
コード例 #11
0
ファイル: NCacheLog.cs プロジェクト: javithalion/NCache
 public static void CriticalInfo(NCacheLog.LoggerNames loggerName, string message)
 {
     string name = loggerName.ToString(); ;
     if (loggerName != LoggerNames.ClientLogs)
         name = LoggingInformation.GetStaticLoggerName(loggerName.ToString());
     CriticalInfo(name, message);
 }
コード例 #12
0
ファイル: NCacheLog.cs プロジェクト: javithalion/NCache
 public static bool IsFatalEnabled(NCacheLog.LoggerNames loggerEnum)
 {
     return true;
 }
コード例 #13
0
ファイル: NCacheLog.cs プロジェクト: javithalion/NCache
 public static bool IsDebugEnabled(NCacheLog.LoggerNames loggerEnum)
 {
     #region isEnabledLogic
     string temp = LoggingInformation.GetStaticLoggerName(loggerEnum.ToString());
     if (temp != null && temp.Length > 0)
     {
         string loggerName = LoggingInformation.GetLoggerName(temp);
         if (loggerName != null && loggerName.Length > 0)
         {
             log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
             return log.IsDebugEnabled;
         }
         else
         {
             NCacheLog.LogLoggingError("loggerName != null && loggerName.Length > 0");
         }
     }
     return false;
     #endregion
 }
コード例 #14
0
        public static void SetLevel(LoggerNames loggerEnum, Level level)
        {
            string loggerName = LoggingInformation.GetLoggerName(LoggingInformation.GetStaticLoggerName(loggerEnum.ToString()));

            if (loggerName != null && loggerName.Length > 0)
            {
                lock (NCacheLog.syncLock)
                {
                    log4net.Core.Level lvl;
                    switch (level.ToString().ToLower())
                    {
                    case "all":
                        lvl = log4net.Core.Level.All;
                        break;

                    case "error":
                        lvl = log4net.Core.Level.Error;
                        break;

                    case "fatal":
                        lvl = log4net.Core.Level.Fatal;
                        break;

                    case "info":
                        lvl = log4net.Core.Level.Info;
                        break;

                    case "debug":
                        lvl = log4net.Core.Level.Debug;
                        break;

                    case "warn":
                        lvl = log4net.Core.Level.Warn;
                        break;

                    case "off":
                        lvl = log4net.Core.Level.Off;
                        break;

                    default:
                        lvl = log4net.Core.Level.All;
                        break;
                    }

                    //If the logger doesnot exist it will create one else fetches one
                    log4net.ILog log = log4net.LogManager.GetLogger(loggerName);
                    //adds the logger as a seperate hierchy, not dependant on any other logger
                    log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)log.Logger;

                    //Applies the logger threshold level
                    l.Level = l.Hierarchy.LevelMap[level.ToString()];

                    IAppender[] appenderCol = log.Logger.Repository.GetAppenders();

                    for (int i = 0; i < appenderCol.Length; i++)
                    {
                        IAppender appender = appenderCol[i];

                        if (appender != null)
                        {
                            if (appender is BufferingForwardingAppender)
                            {
                                ((BufferingForwardingAppender)appender).Threshold = lvl;
                            }

                            if (appender is RollingFileAppender)
                            {
                                ((RollingFileAppender)appender).Threshold = lvl;
                            }
                        }
                    }
                }
            }
            else
            {
                NCacheLog.LogLoggingError("loggerName != null && loggerName.Length > 0");
            }
        }
コード例 #15
0
ファイル: Log4net.cs プロジェクト: javithalion/NCache
        /// <summary>
        /// intitializes Known name based log files
        /// </summary>
        /// <param name="loggerName">Enum of Known loggerNames</param>
        /// <param name="cacheName">cacheName  use the other override</param>
        public static void Initialize(NCacheLog.LoggerNames loggerNameEnum, string cacheName)
        {
            lock (lockObj)
            {

                MemoryStream logStream = new MemoryStream(log4netXML);
                log4net.Config.XmlConfigurator.Configure(logStream);

                string logName = loggerNameEnum.ToString();

                string filename = logName;
                if (loggerNameEnum == NCacheLog.LoggerNames.ClientLogs && (cacheName != null && cacheName.Length > 0))
                {
                    filename = filename + "." + cacheName + "." + System.Diagnostics.Process.GetCurrentProcess().Id;

                    // changing the name here will invalidate static log checks automatically since LoggerName == ClientLogs
                    logName = cacheName + System.Diagnostics.Process.GetCurrentProcess().Id;
                }
                else
                {
                    if (cacheName != null && cacheName.Length > 0)
                        filename = cacheName;
                }

                //If Logger is already present, can either be a cache or Client
                if (LoggingInformation.GetLoggerName(logName) != null)
                {
                    if (loggerNameEnum == NCacheLog.LoggerNames.ClientLogs)
                        return; // clientLogs alread initiated
                    else
                    {
                        if (LoggingInformation.GetStaticLoggerName(logName) != null)
                            return; // Log already initiated
                        else
                        {
                            logName = logName + DateTime.Now;
                        }
                    }
                }
                else
                {
                    if (loggerNameEnum != NCacheLog.LoggerNames.ClientLogs)
                    {
                        if (LoggingInformation.GetStaticLoggerName(logName) != null)
                            return; // Log already initiated
                        else
                        {
                            logName = logName + DateTime.Now;
                        }
                    }
                }

                filename = filename + "." +
                            Environment.MachineName.ToLower() + "." +
                            DateTime.Now.ToString("dd-MM-yy HH-mm-ss") + @".logs.txt";

                string filepath = "";

                if (!DirectoryUtil.SearchGlobalDirectory("log-files", false, out filepath))
                {
                    try
                    {
                        DirectoryUtil.SearchLocalDirectory("log-files", true, out filepath);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Unable to initialize the log file", ex);
                    }
                }

                try
                {
                    filepath = Path.Combine(filepath, loggerNameEnum.ToString());
                    if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);

                    filepath = Path.Combine(filepath, filename);

                    LoggingInformation.cacheLogger.Add(logName, logName);
                    if (loggerNameEnum != NCacheLog.LoggerNames.ClientLogs)
                    {
                        LoggingInformation.staticCacheLogger.Add(loggerNameEnum.ToString(), logName);
                    }

                    SetLevel(logName, NCacheLog.Level.OFF.ToString());
                    AddAppender(logName, CreateBufferAppender(logName, filepath));

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
コード例 #16
0
ファイル: Log4net.cs プロジェクト: javithalion/NCache
 /// <summary>
 /// intitializes Known name based log files (will not log License Logs at service Startup
 /// </summary>
 /// <param name="loggerName">Enum of Known loggerNames</param>
 public static void Initialize(NCacheLog.LoggerNames loggerName)
 {
     if (loggerName != NCacheLog.LoggerNames.Licence)
     {
         Initialize(loggerName, null);
     }
 }