to write log files in two different files
- by Sun
my application run on customized client framework,the client framework used log4net to log their own log files. we are(our application) has to use the same log4net to log our log files in our own path(say our customized path). currently the our log files are created but log are not writing in that file.it is writting in the client framework log file.
searched lot of sites the link
http://stackoverflow.com/questions/308436/log4net-programmatcially-specify-multiple-loggers-with-multiple-file-appenders
helped me to configure the log4net config programatically,
still im log statemets are not written in my log file.the code used as below
public class TraceLog
{
private string message = string.Empty;
private static ILog ILogger = null;
private static TraceLog instance = new TraceLog();
private TraceLog()
{
SetLevel("Log4net.MainForm", "ALL");
AddAppender("Log4net.MainForm", CreateFileAppender("FileAppender", "C:\\mylog.log"));
}
public static TraceLog Instance
{
get
{
return instance;
}
}
public void Debug(string logMessage)
{
message = PrepareLog(logMessage);
ILogger.Debug(message);
}
protected string PrepareLog(string logMessage)
{
string message = GetFileMethodLineNumberInfo();
message += logMessage;
return message;
}
protected string GetFileMethodLineNumberInfo()
{
StackTrace stackTrace = new StackTrace(true);
// The position 3 is relative to the index of the specified method
StackFrame stackFrame = stackTrace.GetFrame(3);
return (stackFrame.GetMethod().DeclaringType.Name
+ "/" + stackFrame.GetMethod().Name
+ "/" + stackFrame.GetFileLineNumber()
+ ":");
}
private static void SetLevel(string loggerName, string levelName)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.Level = l.Hierarchy.LevelMap[levelName];
}
private static void AddAppender(string loggerName, IAppender appender)
{
ILogger = LogManager.GetLogger(loggerName);
log4net.Repository.Hierarchy.Logger l = (log4net.Repository.Hierarchy.Logger)ILogger.Logger;
l.AddAppender(appender);
}
private static IAppender CreateFileAppender(string name, string fileName)
{
FileAppender appender = new FileAppender();
appender.Name = name;
appender.File = fileName;
appender.AppendToFile = true;
//PatternLayout layout = new PatternLayout();
//layout.ConversionPattern = "%d [%t] %-5p %c [%x] - %m%n";
//layout.ActivateOptions();
//appender.Layout = layout;
appender.ActivateOptions();
return appender;
}
}
}
anyone pls help how to solve this