Example #1
0
 /// <summary>
 /// 检查记录器是否为null,如果为null添加默认记录器
 /// </summary>
 private void CheckWriter()
 {
     if (_writers == null || _writers.Count == 0)
     {
         TextWriter textWriter = new TextWriter();
         textWriter.FileName = "runlog.txt";
         textWriter.LogPath = @"C:\xiben\temp";
         _writers.Add(textWriter);
     }
 }
        public static LogConfigSetting[] GetSection()
        {
            Configuration config = UtilSystem.getCurrentConfig();
            ConfigurationSectionGroup oSectionGroup = config.SectionGroups[defaultSectionName];
            
            if ((oSectionGroup != null) && (oSectionGroup.Sections.Count > 0))
            {
                LogConfigSetting[] logConfigSettings = new LogConfigSetting[oSectionGroup.Sections.Count];
                LogConfigSetting logConfigSetting = null;
                for (int i = 0; i < oSectionGroup.Sections.Count; i++)
                {
                    logConfigSetting = (LogConfigSetting)config.GetSection(defaultSectionName + "/" + oSectionGroup.Sections.Keys.Get(i));
                    try
                    {
                        List<IWriter> writers = new List<IWriter>();
                        TextWriter textWriter = new TextWriter();
                        textWriter.FileName = logConfigSetting.fileName;
                        textWriter.LogPath = ConfigurationSectionHandler.LogPathInfo + logConfigSetting.logPath;                        
                        writers.Add(textWriter);
                        logConfigSetting.writers = writers;
                    }
                    catch
                    {
                        //出现异常,填充默认的日志
                        logConfigSetting.loggerName = "";
                        logConfigSetting.logLevel = LogLevel.All;
                        List<IWriter> writers = new List<IWriter>();
                        TextWriter textWriter = new TextWriter();
                        textWriter.FileName = "xiben.log";
                        textWriter.LogPath = @"C:\xiben\Temp";
                        writers.Add(textWriter);

                        logConfigSetting.writers = writers;
                    }
                    logConfigSettings[i] = logConfigSetting;
                }
                return logConfigSettings;
            }
            return null;
        }