Exemplo n.º 1
0
        private void AddDefaultLogs()
        {
            // 预设一些日志。
            var infoLogCfg = new LogEntryConfig()
            {
                FilePathTemplate = Path.Combine(BaseLogPath, InfoLogPath), OwnerClassName = OwnerClassName
            };
            var warningLogCfg = new LogEntryConfig()
            {
                FilePathTemplate = Path.Combine(BaseLogPath, WarningLogPath), OwnerClassName = OwnerClassName
            };
            var errorLogCfg = new LogEntryConfig()
            {
                FilePathTemplate = Path.Combine(BaseLogPath, ErrorLogPath), OwnerClassName = OwnerClassName
            };
            var debugLogCfg = new LogEntryConfig()
            {
                FilePathTemplate = Path.Combine(BaseLogPath, DebugLogPath), OwnerClassName = OwnerClassName
            };
            var expLogCfg = new LogEntryConfig()
            {
                FilePathTemplate = Path.Combine(BaseLogPath, ExceptionLogPath), CacheSize = 0, OwnerClassName = OwnerClassName
            };

            CreateLog(infoLogCfg);
            CreateLog(warningLogCfg);
            CreateLog(errorLogCfg);
            CreateLog(debugLogCfg);
            CreateLog(expLogCfg);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 创建日志
        /// </summary>
        /// <param name="cfg">日志配置</param>
        public void CreateLog(LogEntryConfig cfg)
        {
            if (cfg == null)
            {
                throw new ArgumentNullException(nameof(cfg));
            }
            if (cfg.FilePathTemplate == null)
            {
                throw new ArgumentNullException(nameof(cfg), "必须指定log路径或路径模板,字段:FilePathTemplate");
            }
            if (string.IsNullOrEmpty(cfg.OwnerClassName))
            {
                cfg.OwnerClassName = OwnerClassName;
            }

            var loginf = new LogEntry()
            {
                LogConfig = cfg,

                // LogName = cfg.FilePathTemplate,

                // Logfile = filePath,
            };

            lock (this)
            {
                try
                {
                    if (_logEntryMap.ContainsKey(cfg.FilePathTemplate))
                    {
#if DEBUG
                        throw new Exception("The log is alreay exist.");
#else
                        return;
#endif
                    }
                    else
                    {
                        _logEntryMap.Add(cfg.FilePathTemplate, loginf);
                    }
                }
                finally
                {
                }
            }
        }