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 Jason Goldsmith <JG...@psmic.com> on 2005/08/16 21:37:11 UTC
Cannot switch between logging levels
Hi, I have an ASP.Net application and I store many Session variables, so
I've created a separate config file for log4net (log4net.config) because
if any change is made to the web.config file all my Session variables
are blown away. I have configured it with the following:
Global.asax.Application_Start:
MDC.Set("host", System.Net.Dns.GetHostName());
log4net.Repository.Hierarchy.Hierarchy hier =
log4net.LogManager.GetLoggerRepository() as
log4net.Repository.Hierarchy.Hierarchy;
if(hier != null)
{
// get ADONetAppender
log4net.Appender.ADONetAppender adoAppender =
(log4net.Appender.ADONetAppender)hier.GetLogger(
"AppName",
hier.LoggerFactory).GetAppender("ADONetAppender");
if(adoAppender != null)
{
adoAppender.ConnectionString =
System.Configuration.ConfigurationSettings.AppSettings["Logger.Connectio
nString"];
adoAppender.ActivateOptions();
}
}
log4net.Config.DOMConfigurator.Configure();
Web.config:
<add key="Logger.ConnectionString"
value="server=SqlServer1;database=APP_LOG;user id=CPM;password=CPM;
Application Name=Log"/>
AssemblyInfo.cs
[assembly: log4net.Config.DOMConfigurator(ConfigFile="log4net.config",
Watch=true)]
Log4net.config
<log4net>
<appender name="FileAppender"
type="log4net.Appender.FileAppender">
<param name="File" value=".//Logs//application.log" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="%d[%t]
%-5p %C{1}[%M] - %m%n" />
</layout>
</appender>
<appender name="ADONetAppender"
type="log4net.Appender.ADONetAppender">
<bufferSize value="1" />
<connectionType
value="System.Data.SqlClient.SqlConnection, System.Data,
Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<commandText value="INSERT INTO CPMLog
([Date],[Thread],[Level],[Logger], [Host],[Message],[Exception]) VALUES
(@log_date, @thread, @log_level, @logger, @host,
@message, @exception)" />
<parameter>
<parameterName value="@log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout"
/>
</parameter>
<parameter>
<parameterName value="@thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%t" />
</layout>
</parameter>
<parameter>
<parameterName value="@log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%p" />
</layout>
</parameter>
<parameter>
<parameterName value="@logger" />
<dbType value="String" />
<size value="512" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%c" />
</layout>
</parameter>
<parameter>
<parameterName value="@host" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%X{host}" />
</layout>
</parameter>
<parameter>
<parameterName value="@message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%m" />
</layout>
</parameter>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<!--Levels - OFF, ALL, WARN, FATAL, INFO, ERRROR, DEBUG -->
<logger name="CPMWeb">
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</logger>
<root>
<level value="DEBUG" />
<appender-ref ref="ADONetAppender" />
</root>
</log4net>
If I have my root level set to DEBUG, I can change it to OFF and log4net
stops logging (as expected), but if I decide to change back to DEBUG it
doesn't restart logging. Is there something that I have configured
incorrectly? I have tried add and set the Threshold in the log4net tag,
the ADONetAppender, as well as under the root tag, to No avail. I have
also tried to call ConfigureAndWatch in the Application_Start, but that
didn't work either. Am I missing something?
Thanks for any kind of assistance,
Jason Goldsmith
Re: Cannot switch between logging levels
Posted by Ron Grabowski <ro...@yahoo.com>.
If you enable debugging inside of log4net:
<appSettings>
<add key="log4net.Internal.Debug" value="true" />
</appSettings>
and use ConfigureAndWatch to configure log4net, you should see a
message similiar to this when you make a change to your log4net.config
file. The text below was generated when I changed the level of my root
logger from ALL to OFF:
log4net: ConfigureAndWatchHandler: Changed
[c:\inetpub\wwwroot\log4net.config]
log4net: ConfigureAndWatchHandler: Changed
[c:\inetpub\wwwroot\log4net.config]
log4net: XmlConfigurator: configuring repository
[log4net-default-repository] using file
[c:/inetpub/wwwroot/log4net.config]
log4net: XmlConfigurator: configuring repository
[log4net-default-repository] using stream
log4net: XmlConfigurator: loading XML configuration
log4net: XmlConfigurator: Configuring Repository
[log4net-default-repository]
log4net: XmlHierarchyConfigurator: Configuration update mode [Merge].
log4net: XmlHierarchyConfigurator: Logger [root] Level string is [OFF].
log4net: XmlHierarchyConfigurator: Logger [root] level set to
[name="OFF",value=2147483647].
...
It looks like you're configuring log4net twice. A better approach may
be to use your own AdoNetAppender:
public class MyAdoNetAppender : AdoNetAppender
{
protected string ConnectionString
{
get { return base.ConnectionString; }
set { base.ConnectionString =
ConfigurationSettings.AppSettings["Logger.ConnectionString"]; }
}
}
Your call to configure log4net will look something like this:
FileInfo log4netConfig = GetLog4netConfigFile();
log4net.Config.DOMConfigurator.ConfigureAndWatch(log4netConfig);
--- Jason Goldsmith <JG...@psmic.com> wrote:
> Hi, I have an ASP.Net application and I store many Session variables,
> so
> I've created a separate config file for log4net (log4net.config)
> because
> if any change is made to the web.config file all my Session variables
> are blown away. I have configured it with the following: