Example #1
0
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="fileAppenderConfig">配置</param>
        /// <param name="pathManager">路由管理器</param>
        /// <param name="createFilePathTime">创建时间</param>
        /// <param name="logMessage">日志信息项</param>
        protected override void WriteLog(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager, DateTime createFilePathTime, LogMessageItem logMessage)
        {
            DateTime currentTime = DateTime.Now;

            if (this._sw != null &&
                !pathManager.IsFixPath &&
                (fileAppenderConfig.MaxFileLength > 0 &&
                 this._sw.BaseStream.Length >= fileAppenderConfig.MaxFileLength ||
                 currentTime.Year != createFilePathTime.Year ||
                 currentTime.Month != createFilePathTime.Month ||
                 currentTime.Day != createFilePathTime.Day))
            {
                this._sw.Close();
                this._sw = null;
            }

            if (this._sw == null)
            {
                string logFilePath = base.GetLogFilePath();
                if (string.IsNullOrWhiteSpace(logFilePath))
                {
                    return;
                }

                this._sw = File.AppendText(logFilePath);
            }

            base.WriteLogToFile(logMessage, this._sw);
        }
Example #2
0
        public FileAppenderPathBuilderBase(FileAppenderConfig config, string[] paths, char[] pathSplitChars)
        {
            this._config         = config;
            this._pathSplitChars = pathSplitChars;
            int rootDirPathCount = paths.Length - 1;

            for (int i = 0; i < paths.Length - 1; i++)
            {
                if (paths[i].Contains(LogConstant.PATTERN_FALG_CHAR))
                {
                    rootDirPathCount = i;
                    break;
                }
            }

            string[] rootPaths = paths.Take(rootDirPathCount).ToArray();
            this._rootDir = Path.Combine(rootPaths);
            if (Environment.OSVersion.Platform != PlatformID.Win32NT)
            {
                this._rootDir = "/" + this._rootDir;
            }

            string[] relativePaths = paths.Skip(rootDirPathCount).ToArray();
            this._searchPattern = string.Format("*{0}", Path.GetExtension(relativePaths.Last()));
            this._pathItems     = new FileAppenderPathItem[relativePaths.Length];
            for (int i = 0; i < relativePaths.Length; i++)
            {
                this._pathItems[i] = new FileAppenderPathItem(relativePaths[i]);
            }
        }
Example #3
0
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            this._fileLogWriter = null;
            var fileAppenderConfig = (FileAppenderConfig)base._config;

            this._fileAppenderConfig = fileAppenderConfig;
            var pathManager = new FileAppenderPathManager(this._fileAppenderConfig);

            switch (this._fileAppenderConfig.LockingModel)
            {
            case LockingModel.Exclusive:
                this._fileLogWriter = new ExclusiveFileLogWriter(fileAppenderConfig, pathManager);
                break;

            case LockingModel.Minimal:
                this._fileLogWriter = new MinimalFileLogWriter(fileAppenderConfig, pathManager);
                break;

            case LockingModel.InterProcess:
                this._fileLogWriter = new InterProcessFileLogWriter(fileAppenderConfig, pathManager);
                break;

            default:
                LogSysInnerLog.OnRaiseLog(this, new NotSupportedException(string.Format("不支持的锁模型:{0}", this._fileAppenderConfig.LockingModel.ToString())));
                break;
            }
        }
Example #4
0
        public FileAppenderFixFileNameBuilder(FileAppenderConfig config, string[] paths, char[] pathSplitChars) :
            base(config, paths, pathSplitChars)
        {
            var fileName = paths.Last();

            this._fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName) + "_";
            this._extension = Path.GetExtension(fileName);
            this.RecoverIndex();
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="fileAppenderConfig">配置</param>
        /// <param name="pathManager">路由管理器</param>
        /// <param name="createFilePathTime">创建时间</param>
        /// <param name="logMessage">日志信息项</param>
        protected override void WriteLog(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager, DateTime createFilePathTime, LogMessageItem logMessage)
        {
            string logFilePath = base.GetLogFilePath();

            if (string.IsNullOrWhiteSpace(logFilePath))
            {
                return;
            }

            using (var sw = File.AppendText(logFilePath))
            {
                base.WriteLogToFile(logMessage, sw);
            }
        }
Example #6
0
 public FileLogWriterBase(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager)
 {
     this._fileAppenderConfig = fileAppenderConfig;
     this._pathManager        = pathManager;
     try
     {
         Type type = LogUtil.GetType(fileAppenderConfig.SecurityPolicy);
         if (type != null && type.GetInterface(typeof(ILogSecurityPolicy).FullName) != null)
         {
             this._securityPolicy = Activator.CreateInstance(type) as ILogSecurityPolicy;
         }
     }
     catch (Exception ex)
     {
         LogSysInnerLog.OnRaiseLog(this, ex);
     }
 }
Example #7
0
        public FileAppenderPathManager(FileAppenderConfig config)
        {
            string filePath = config.FilePath;

            if (string.IsNullOrWhiteSpace(filePath))
            {
                throw new ArgumentNullException("文件路径不能为空");
            }

            this.IsFixPath = !filePath.Contains(LogConstant.PATTERN_FALG_CHAR);
            var fullPath = this.ConvertToFullPath(filePath);

            string[] paths = fullPath.Split(_pathSplitChars, StringSplitOptions.RemoveEmptyEntries);
            paths[0] = paths[0] + Path.DirectorySeparatorChar;
            if (paths[paths.Length - 1].Contains(LogConstant.PATTERN_FALG_CHAR))
            {
                this._pathBuilder = new FileAppenderVariateFileNameBuilder(config, paths, _pathSplitChars);
            }
            else
            {
                this._pathBuilder = new FileAppenderFixFileNameBuilder(config, paths, _pathSplitChars);
            }
        }
        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="fileAppenderConfig">配置</param>
        /// <param name="pathManager">路由管理器</param>
        /// <param name="createFilePathTime">创建时间</param>
        /// <param name="logMessage">日志信息项</param>
        protected override void WriteLog(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager, DateTime createFilePathTime, LogMessageItem logMessage)
        {
            Mutex mutex = null;

            try
            {
                string logFilePath = base.GetLogFilePath();
                if (string.IsNullOrWhiteSpace(logFilePath))
                {
                    return;
                }

                mutex = this.GetMutex(logFilePath);
                using (var sw = File.AppendText(logFilePath))
                {
                    //日志处理
                    base.WriteLogToFile(logMessage, sw);
                }
            }
            finally
            {
                this.ReleaseMutex(mutex);
            }
        }
Example #9
0
 public ExclusiveFileLogWriter(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager) :
     base(fileAppenderConfig, pathManager)
 {
 }
Example #10
0
 public FileAppenderVariateFileNameBuilder(FileAppenderConfig config, string[] paths, char[] pathSplitChars) :
     base(config, paths, pathSplitChars)
 {
     this._lastPathItem = this._pathItems.Last();
 }
 public MinimalFileLogWriter(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager) :
     base(fileAppenderConfig, pathManager)
 {
 }
 public InterProcessFileLogWriter(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager) :
     base(fileAppenderConfig, pathManager)
 {
 }
Example #13
0
 /// <summary>
 /// 写日志
 /// </summary>
 /// <param name="fileAppenderConfig">配置</param>
 /// <param name="pathManager">路由管理器</param>
 /// <param name="createFilePathTime">创建时间</param>
 /// <param name="logMessage">日志信息项</param>
 protected abstract void WriteLog(FileAppenderConfig fileAppenderConfig, FileAppenderPathManager pathManager, DateTime createFilePathTime, LogMessageItem logMessage);