logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Radovan Raszka" <ras...@hasam.cz>
Subject RE: Configure appenders/loggers at runtime
Date Tue, 01 Feb 2011 10:21:08 GMT
Sorry I forgot to attach the link:

You can check also this thread for more information
http://mail-archives.apache.org/mod_mbox/logging-log4net-user/200805.mbox/browser
It is enough to use BasicConfigurator.Configure(appender) if you are using single appender.
Radovan

-----Původní zpráva-----
Od: cyz [mailto:zhouping@micron.com] 
Odesláno: 1. února 2011 11:13
Komu: log4net-user@logging.apache.org
Předmět: Re: Configure appenders/loggers at runtime


Found the problem. Need to add activate the layout options.

layout.ActivateOptions();



cyz wrote:
> 
> Hi, I tried to add appender/loggers at runtime. But result came out is 
> not what I expected.
> 
> //--------------------------------------code
> --------------------------------------
> log4net.Appender.RollingFileAppender fileAppender = new 
> log4net.Appender.RollingFileAppender();
> fileAppender.Name = "RuntimeAppender"; fileAppender.File = 
> @"E:\runtime.log"; fileAppender.AppendToFile = true; 
> fileAppender.RollingStyle = 
> log4net.Appender.RollingFileAppender.RollingMode.Size;
> fileAppender.MaxSizeRollBackups = -1;
> fileAppender.MaximumFileSize = "100KB"; 
> fileAppender.PreserveLogFileNameExtension = true; 
> fileAppender.StaticLogFileName = false;
> fileAppender.CountDirection = 0;       
> fileAppender.Threshold = log4net.Core.Level.All;                        
> log4net.Layout.PatternLayout layout = new 
> log4net.Layout.PatternLayout(); layout.ConversionPattern = 
> "%n%d{yyMMdd_hhmmss.fff};%-5level;%m";
> layout.Header = "Time;Level;Description;";
> fileAppender.Layout = layout;                        
> fileAppender.ActivateOptions();
> 
> log4net.Repository.Hierarchy.Hierarchy repository = 
> (log4net.Repository.Hierarchy.Hierarchy)log4net.LogManager.GetReposito
> ry(); log4net.Repository.Hierarchy.Logger logger = 
> (log4net.Repository.Hierarchy.Logger)repository.GetLogger("RuntimeAppe
> nder");
> logger.AddAppender(fileAppender);
> 
> 
> log4net.ILog myLogger = 
> log4net.LogManager.GetLogger("RuntimeAppender");
> myLogger.Info("testing");
> //--------------------------------------------------------------------
> -------- the resulted file content is something like below:
> Time;Level;Description;testing
> 
> I'm looking for something like below:
> Time;Level;Description;
> 110201_050751.829;INFO ;testing
> 
> which configuration/code below can produce this result nicely.
> 
> //--------------------------------------------------------------------
> -----
> <appender name="ConfigAppender"
> type="log4net.Appender.RollingFileAppender">
>       <!--file value="e:\log%property{RuntimeName}.log" /-->
>       <file value="e:\configappender.log" />
>       <appendToFile value="true" />
>       <rollingStyle value="Size" />
>       <maxSizeRollBackups value="-1" />
>       <maximumFileSize value="1MB" />            
>       <staticLogFileName value="false" />
>       <countDirection value="0" />      
>       <layout type="log4net.Layout.PatternLayout">
>         <header value="Time;Level;Description;" />
>         <conversionPattern value="%n%d{yyMMdd_hhmmss.fff};%-5level;%m" />
>       </layout>
> </appender>
> <logger name="RollingLogAppender">
>       <level value="ALL" />
>       <appender-ref ref="ConfigAppender" /> </logger>
> 
> log4net.ILog log2 = 
> log4net.LogManager.GetLogger("RollingLogAppender");
> log2.Info("testing");
> 
> 
> Can someone help? Thanks a lot.
> 
> 
> 

--
View this message in context: http://old.nabble.com/Configure-appenders-loggers-at-runtime-tp30814581p30814923.html
Sent from the Log4net - Users mailing list archive at Nabble.com.


Mime
View raw message