Wednesday, October 21, 2009

Switching Log-Files During Logging



private static readonly ILog alog = LogManager.GetLogger("AlgoLogger");

public void SwitchToNewUniqueAlgoLogFile(long scheduleID)
{
ILoggerRepository logRepos = LogManager.GetRepository();
IAppender[] logAppenders = logRepos.GetAppenders();

string targetAppenderName = "myAppender"

// first find the logger we are actually looking for

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

if (appender.Name == targetAppenderName )
{
// cast generic appender to our type
RollingFileAppender rfa = appender as RollingFileAppender;
string oldFName = rfa.File;
int indexOfLastSlash = oldFName.LastIndexOf(@"\");
string path = oldFName.Substring(0, indexOfLastSlash + 1);

// generate new log file name

string scheduleName = SQLHandler.GetScheduleName(scheduleID);

string newLogFileName = "newLog.txt"
string newFullFileName = path + newLogFileName ;

rfa.File = newFullFileName;
rfa.ActivateOptions();

break;
}
}
}

No comments:

Post a Comment

comment: