logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cyz <zhoup...@micron.com>
Subject Re: Configure appenders/loggers at runtime
Date Tue, 01 Feb 2011 10:13:09 GMT

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.GetRepository();
> log4net.Repository.Hierarchy.Logger logger =
> (log4net.Repository.Hierarchy.Logger)repository.GetLogger("RuntimeAppender");
> 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