public void FlushAPILogs() { IAppender[] appenders = log.Logger.Repository.GetAppenders(); for (int i = 0; i < appenders.Length; i++) { RollingFileAppender rollingFileAppender = appenders[i] as RollingFileAppender; if (rollingFileAppender != null) { string appenderName = rollingFileAppender.Name; string[] explodeString = appenderName.Split('_'); if (explodeString.Length > 0) { if (explodeString[0].Equals(LoggerNames.APILogs.ToString().ToLower(), StringComparison.InvariantCultureIgnoreCase)) { rollingFileAppender.Close(); try { var root = ((log4net.Repository.Hierarchy.Hierarchy)LogManager.GetRepository(Log4net.LogRepository.Name)).Root; root.RemoveAppender(rollingFileAppender); } catch (Exception ex) { } } } } } }
/// <summary> /// This method is registered on <see cref="CK.AppRecovery.ApplicationRecovery.ApplicationCrashed"/> event. /// </summary> /// <param name="source"></param> /// <param name="e"></param> static internal void OnApplicationCrash(object source, ApplicationCrashedEventArgs e) { if (_initialized) { _hierarchy.Root.Log(Level.Fatal, "Application crashes. Appending existing log files into Crash log. Over!", null); _hierarchy.Root.RemoveAppender(_roller); _roller.Close(); if (e.CrashLog.IsValid) { char[] buffer = new char[2048]; foreach (string f in Directory.GetFiles(_appLogDirectory)) { e.CrashLog.Writer.WriteLine("===== Log file: {0} =====", Path.GetFileName(f)); try { using (StreamReader r = File.OpenText(f)) { int nbRead; while ((nbRead = r.ReadBlock(buffer, 0, buffer.Length)) > 0) { e.CrashLog.Writer.Write(buffer, 0, nbRead); } } } finally { e.CrashLog.Writer.WriteLine("===== End of log file: {0} =====", Path.GetFileName(f)); } } } } else { e.CrashLog.Writer.WriteLine("CommonLogger has not been initialized."); } }
private void UnregisterLogAppender() { new_appender.Close(); }