You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by dh <dh...@cube.org> on 2005/12/30 16:04:38 UTC

Multiple logs?

Hi all,

  Until now, I used the following code for my log:

Namespace Test
{
  Class Class1
  {  	
	Private static string appPath = "";

	private static ILog log = LogManager.GetLogger(
System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
	private static log4net.Appender.RollingFileAppender
rollingFileAppender;

	void Initialize()
	{
		appPath = Assembly.GetExecutingAssembly().Location; 
		appPath = appPath.Substring(0, appPath.LastIndexOf(@"\"));

		rollingFileAppender			= new
log4net.Appender.RollingFileAppender();
		rollingFileAppender.File			=
String.Format(@"{0}\log\test", appPath);
		rollingFileAppender.AppendToFile	= true;
		rollingFileAppender.DatePattern		= "-yyMMdd'.log'";
		rollingFileAppender.RollingStyle		=
log4net.Appender.RollingFileAppender.RollingMode.Date;
		rollingFileAppender.StaticLogFileName	= false;
		rollingFileAppender.Layout		= new
log4net.Layout.PatternLayout("%d{HH:mm:ss} %5p - %m%n");
		rollingFileAppender.Encoding		=
System.Text.Encoding.UTF8;
		rollingFileAppender.Threshold		=
log4net.Core.Level.Info;
		rollingFileAppender.ActivateOptions();

		log4net.Config.BasicConfigurator.Configure(
rollingFileAppender );
	}
 
	void DebugLog( ref ILog log, string s )
	{
		if( log != null && s.Length > 0 ) log.Debug( s );
	}
  
	/// <summary>
	/// The main entry point for the application.
	/// </summary>
	[STAThread]
	static void Main(string[] args)
	{
		Initialize();
		DebugLog( ref log, "Starting the application..." );
	}
  }
}

It works well with one log file, but I need to log into two different log
files.

For example:

	DebugLog( ref log1, "a message into the log file 1." );
	DebugLog( ref log2, "a message into the log file 2." );

Log1 -> log1.log
Log2 -> log2.log


How could I do that with log4net?


I tried with two appenders, but each message is written into the two log
files which I don't want. I want the message1 (resp. message2) to be written
into the log file 1 (resp. log file 2).



Thanks,

Damien



RE: Multiple logs?

Posted by dh <dh...@cube.org>.
Hi,

  Class1 is just an example, but in the real source code, this is the main
class of my windows forms application, then it is just a class which has a
logger.

  I'm going to search in the documentation for a configuration file like you
said.

Thanks.


-----Message d'origine-----
De : Matthew Brown [mailto:octavius@gmail.com] 
Envoyé : vendredi 30 décembre 2005 16:27
À : Log4NET User
Objet : Re: Multiple logs?

Is Class1 a logger or a class that happens to have a logger?

Either way, you're going to want to create different appenders for the
different log files.

Have you looked into using a config file to handle configuration? Much
easier.

On 12/30/05, dh <dh...@cube.org> wrote:
> Hi all,
>
>   Until now, I used the following code for my log:
>
> Namespace Test
> {
>   Class Class1
>   {
>         Private static string appPath = "";
>
>         private static ILog log = LogManager.GetLogger(
> System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
>         private static log4net.Appender.RollingFileAppender
> rollingFileAppender;
>
>         void Initialize()
>         {
>                 appPath = Assembly.GetExecutingAssembly().Location;
>                 appPath = appPath.Substring(0, appPath.LastIndexOf(@"\"));
>
>                 rollingFileAppender                     = new
> log4net.Appender.RollingFileAppender();
>                 rollingFileAppender.File                        =
> String.Format(@"{0}\log\test", appPath);
>                 rollingFileAppender.AppendToFile        = true;
>                 rollingFileAppender.DatePattern         = "-yyMMdd'.log'";
>                 rollingFileAppender.RollingStyle                =
> log4net.Appender.RollingFileAppender.RollingMode.Date;
>                 rollingFileAppender.StaticLogFileName   = false;
>                 rollingFileAppender.Layout              = new
> log4net.Layout.PatternLayout("%d{HH:mm:ss} %5p - %m%n");
>                 rollingFileAppender.Encoding            =
> System.Text.Encoding.UTF8;
>                 rollingFileAppender.Threshold           =
> log4net.Core.Level.Info;
>                 rollingFileAppender.ActivateOptions();
>
>                 log4net.Config.BasicConfigurator.Configure(
> rollingFileAppender );
>         }
>
>         void DebugLog( ref ILog log, string s )
>         {
>                 if( log != null && s.Length > 0 ) log.Debug( s );
>         }
>
>         /// <summary>
>         /// The main entry point for the application.
>         /// </summary>
>         [STAThread]
>         static void Main(string[] args)
>         {
>                 Initialize();
>                 DebugLog( ref log, "Starting the application..." );
>         }
>   }
> }
>
> It works well with one log file, but I need to log into two different log
> files.
>
> For example:
>
>         DebugLog( ref log1, "a message into the log file 1." );
>         DebugLog( ref log2, "a message into the log file 2." );
>
> Log1 -> log1.log
> Log2 -> log2.log
>
>
> How could I do that with log4net?
>
>
> I tried with two appenders, but each message is written into the two log
> files which I don't want. I want the message1 (resp. message2) to be
written
> into the log file 1 (resp. log file 2).
>
>
>
> Thanks,
>
> Damien
>
>
>


Re: Multiple logs?

Posted by Matthew Brown <oc...@gmail.com>.
Is Class1 a logger or a class that happens to have a logger?

Either way, you're going to want to create different appenders for the
different log files.

Have you looked into using a config file to handle configuration? Much easier.

On 12/30/05, dh <dh...@cube.org> wrote:
> Hi all,
>
>   Until now, I used the following code for my log:
>
> Namespace Test
> {
>   Class Class1
>   {
>         Private static string appPath = "";
>
>         private static ILog log = LogManager.GetLogger(
> System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
>         private static log4net.Appender.RollingFileAppender
> rollingFileAppender;
>
>         void Initialize()
>         {
>                 appPath = Assembly.GetExecutingAssembly().Location;
>                 appPath = appPath.Substring(0, appPath.LastIndexOf(@"\"));
>
>                 rollingFileAppender                     = new
> log4net.Appender.RollingFileAppender();
>                 rollingFileAppender.File                        =
> String.Format(@"{0}\log\test", appPath);
>                 rollingFileAppender.AppendToFile        = true;
>                 rollingFileAppender.DatePattern         = "-yyMMdd'.log'";
>                 rollingFileAppender.RollingStyle                =
> log4net.Appender.RollingFileAppender.RollingMode.Date;
>                 rollingFileAppender.StaticLogFileName   = false;
>                 rollingFileAppender.Layout              = new
> log4net.Layout.PatternLayout("%d{HH:mm:ss} %5p - %m%n");
>                 rollingFileAppender.Encoding            =
> System.Text.Encoding.UTF8;
>                 rollingFileAppender.Threshold           =
> log4net.Core.Level.Info;
>                 rollingFileAppender.ActivateOptions();
>
>                 log4net.Config.BasicConfigurator.Configure(
> rollingFileAppender );
>         }
>
>         void DebugLog( ref ILog log, string s )
>         {
>                 if( log != null && s.Length > 0 ) log.Debug( s );
>         }
>
>         /// <summary>
>         /// The main entry point for the application.
>         /// </summary>
>         [STAThread]
>         static void Main(string[] args)
>         {
>                 Initialize();
>                 DebugLog( ref log, "Starting the application..." );
>         }
>   }
> }
>
> It works well with one log file, but I need to log into two different log
> files.
>
> For example:
>
>         DebugLog( ref log1, "a message into the log file 1." );
>         DebugLog( ref log2, "a message into the log file 2." );
>
> Log1 -> log1.log
> Log2 -> log2.log
>
>
> How could I do that with log4net?
>
>
> I tried with two appenders, but each message is written into the two log
> files which I don't want. I want the message1 (resp. message2) to be written
> into the log file 1 (resp. log file 2).
>
>
>
> Thanks,
>
> Damien
>
>
>