Discussion:
Problem logging in class library
Maybhate, Dipty
2006-08-16 16:13:44 UTC
Permalink
________________________________

From: Maybhate, Dipty
Sent: Wednesday, August 16, 2006 10:55 AM
To: 'log4net-***@logging.apache.org'
Subject: Problem logging in class library



Hi Everyone

I am new to log4net and I am facing problems with it. In my .Net
solution I have a class library project and a ASP.net project. I want to
create a Logger class which is like a wrapper in the class library and
have all the other Webservices or ASP.net projects in the solution
access that.

This is how I use log4net in the Class library



AssemblyInfo.cs

[assembly : log4net.Config.XmlConfigurator(Watch=true)]



App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="log4net"

type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />

</configSections>

<log4net>

<appender name="FileAppender"
type="log4net.Appender.FileAppender">

<param name="File" value="c:\\LogTest23.txt" />

<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="Header" value="[Header]\r\n" />

<param name="Footer" value="[Footer]\r\n" />

<param name="ConversionPattern" value="%d [%t] %-5p %c
%m%n" />

</layout>

</appender>

<root>

<level value="INFO" />

<appender-ref ref="FileAppender" />

</root>

</log4net>

</configuration>



Logger class

private static readonly ILog logger =

LogManager.GetLogger(typeof(Class1));

log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));

logger.Debug("Here is a debug log.");

logger.Info("... and an Info log.");

logger.Warn("... and a warning.");

logger.Error("... and an error.");

logger.Fatal("... and a fatal error.");



I also tried using the <ClassLibraryName>.exe.config as the name for the
app.config as I read it somewhere

Then my ASP.net application creates an object of this Class1 from the
class library application and see if the log file is created

Could you please help me figure out what I am doing wrong?

Thank you very much

Dipty
Steve Whitley
2006-08-16 17:41:28 UTC
Permalink
asp.net's config is called web.config. try looking for that.
------------------------------------------------------------------------
*From:* Maybhate, Dipty
*Sent:* Wednesday, August 16, 2006 10:55 AM
*Subject:* Problem logging in class library
Hi Everyone
I am new to log4net and I am facing problems with it. In my .Net
solution I have a class library project and a ASP.net project. I want
to create a Logger class which is like a wrapper in the class library
and have all the other Webservices or ASP.net projects in the solution
access that.
This is how I use log4net in the Class library
*_AssemblyInfo.cs_*
[assembly : log4net.Config.XmlConfigurator(Watch=true)]
*_App.config_*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<appender name="FileAppender"
type="log4net.Appender.FileAppender">
<param name="File" value="c:\\LogTest23.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c
%m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
*_Logger class_*
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
I also tried using the <ClassLibraryName>.exe.config as the name for
the app.config as I read it somewhere
Then my ASP.net application creates an object of this Class1 from the
class library application and see if the log file is created
Could you please help me figure out what I am doing wrong?
Thank you very much
Dipty
Steve Whitley
2006-08-16 18:20:44 UTC
Permalink
if its a seperate dll, you just need to add [assembly :
log4net.Config.XmlConfigurator(Watch=true)], but the application you use
needs to have the appropriate xml structure in their app config.

if you want it to work regardless of if the referencing application has
log4net configured, you can configure it to log by hand (and I think
thats documented, though I cant point you to where off hand).

--steve
Thanks Steve
I want the log4net to initialize in Class libraries. In my
understanding I would need to create like a log4net.config or some
file like that and mention that in AssemblyInfo.cs
if it should be done in class libraries.
Basically I have created a Logger class in the class library project
and reference it in ASP.net project in the same solution by creating
its constructor in Page_Load of the home page
When the constructor is called it goes to the log4net code
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
but doesn't create a log file
I hope this makes some sense
I have wasted like 2 days and also didn't have much time to read
anything on this
Thanks again!
Dipty
------------------------------------------------------------------------
*Sent:* Wednesday, August 16, 2006 12:41 PM
*To:* Log4NET User
*Subject:* Re: FW: Problem logging in class library
asp.net's config is called web.config. try looking for that.
------------------------------------------------------------------------
*From:* Maybhate, Dipty
*Sent:* Wednesday, August 16, 2006 10:55 AM
*Subject:* Problem logging in class library
Hi Everyone
I am new to log4net and I am facing problems with it. In my .Net
solution I have a class library project and a ASP.net project. I want
to create a Logger class which is like a wrapper in the class library
and have all the other Webservices or ASP.net projects in the solution
access that.
This is how I use log4net in the Class library
*_AssemblyInfo.cs_*
[assembly : log4net.Config.XmlConfigurator(Watch=true)]
*_App.config_*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<appender name="FileAppender"
type="log4net.Appender.FileAppender">
<param name="File" value="c:\\LogTest23.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c
%m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
*_Logger class_*
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
I also tried using the <ClassLibraryName>.exe.config as the name for
the app.config as I read it somewhere
Then my ASP.net application creates an object of this Class1 from the
class library application and see if the log file is created
Could you please help me figure out what I am doing wrong?
Thank you very much
Dipty
Richard Collette
2006-08-16 20:54:22 UTC
Permalink
I thought you had to have a call to LogManager.getLogger in the application_startup event to trigger the loading of the configuration in addition to putting the configuration in the web.config?


Steve Whitley <***@nullfx.com> wrote:
if its a seperate dll, you just need to add [assembly : log4net.Config.XmlConfigurator(Watch=true)], but the application you use needs to have the appropriate xml structure in their app config.

if you want it to work regardless of if the referencing application has log4net configured, you can configure it to log by hand (and I think thats documented, though I cant point you to where off hand).

--steve

Maybhate, Dipty wrote:
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} Thanks Steve
I want the log4net to initialize in Class libraries. In my understanding I would need to create like a log4net.config or some file like that and mention that in AssemblyInfo.cs
if it should be done in class libraries.

Basically I have created a Logger class in the class library project and reference it in ASP.net project in the same solution by creating its constructor in Page_Load of the home page
When the constructor is called it goes to the log4net code
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");

but doesn’t create a log file
I hope this makes some sense
I have wasted like 2 days and also didn’t have much time to read anything on this
Thanks again!
Dipty


---------------------------------

From: Steve Whitley [mailto:***@nullfx.com]
Sent: Wednesday, August 16, 2006 12:41 PM
To: Log4NET User
Subject: Re: FW: Problem logging in class library


asp.net's config is called web.config. try looking for that.

Maybhate, Dipty wrote:





---------------------------------

From: Maybhate, Dipty
Sent: Wednesday, August 16, 2006 10:55 AM
To: 'log4net-***@logging.apache.org'
Subject: Problem logging in class library


Hi Everyone
I am new to log4net and I am facing problems with it. In my .Net solution I have a class library project and a ASP.net project. I want to create a Logger class which is like a wrapper in the class library and have all the other Webservices or ASP.net projects in the solution access that.
This is how I use log4net in the Class library

AssemblyInfo.cs
[assembly : log4net.Config.XmlConfigurator(Watch=true)]

App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="FileAppender" type="log4net.Appender.FileAppender">
<param name="File" value="c:\\LogTest23.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>

Logger class
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");

I also tried using the <ClassLibraryName>.exe.config as the name for the app.config as I read it somewhere
Then my ASP.net application creates an object of this Class1 from the class library application and see if the log file is created
Could you please help me figure out what I am doing wrong?
Thank you very much
Dipty










Richard P. Collette
Certified Lotus Notes Principal Developer
Steve Whitley
2006-08-16 20:43:44 UTC
Permalink
Adding [assembly : log4net.Config.XmlConfigurator(Watch=true)] to your
assemblyinfo.cs file will load up a class that looks of an application
configuration file. If the application has one, it will attempt to load
up the configuration details from the config file and build your logging
system. the data in the config file specifies the appenders that will
listen for logging events, and any parameters they need, as well as what
LoggingLevel they will subscribe to.

if that data is not in the application configuration file then you will
have no appenders or repository set up to do your logging. If you plan
on users of your wrapper not having to do any configuring, then you need
to do a BasicConfigurator.Configure somewhere in the entrypoint to your
wrapper and setup log4net. there you can specify what repository you
want to use, and / or what appenders you want to use.
Steve I once again really appreciate your help
As you suggested my application where I intend to create the wrapper
class for logging is a separate dll. I have created an app.config and
also added the assembly instruction as you have suggested.
My another application which has reference to the wrapper class
project has no code or mention for log4net. It just class the wrapper
class constructor which must do the logging at initialization and will
eventually call the writeLog, writeDebug etc of this wrapper.
I am not quite clear how my referencing application which is the
ASP.net should be configured to log by hand as you have suggested.
Basically I am not sure what log by hand means.
Thanks again!
Dipty
------------------------------------------------------------------------
*Sent:* Wednesday, August 16, 2006 1:21 PM
*To:* Maybhate, Dipty; Log4NET User
*Subject:* Re: FW: Problem logging in class library
log4net.Config.XmlConfigurator(Watch=true)], but the application you
use needs to have the appropriate xml structure in their app config.
if you want it to work regardless of if the referencing application
has log4net configured, you can configure it to log by hand (and I
think thats documented, though I cant point you to where off hand).
--steve
Thanks Steve
I want the log4net to initialize in Class libraries. In my
understanding I would need to create like a log4net.config or some
file like that and mention that in AssemblyInfo.cs
if it should be done in class libraries.
Basically I have created a Logger class in the class library project
and reference it in ASP.net project in the same solution by creating
its constructor in Page_Load of the home page
When the constructor is called it goes to the log4net code
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
but doesn't create a log file
I hope this makes some sense
I have wasted like 2 days and also didn't have much time to read
anything on this
Thanks again!
Dipty
------------------------------------------------------------------------
*Sent:* Wednesday, August 16, 2006 12:41 PM
*To:* Log4NET User
*Subject:* Re: FW: Problem logging in class library
asp.net's config is called web.config. try looking for that.
------------------------------------------------------------------------
*From:* Maybhate, Dipty
*Sent:* Wednesday, August 16, 2006 10:55 AM
*Subject:* Problem logging in class library
Hi Everyone
I am new to log4net and I am facing problems with it. In my .Net
solution I have a class library project and a ASP.net project. I want
to create a Logger class which is like a wrapper in the class library
and have all the other Webservices or ASP.net projects in the solution
access that.
This is how I use log4net in the Class library
*_AssemblyInfo.cs_*
[assembly : log4net.Config.XmlConfigurator(Watch=true)]
*_App.config_*
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />
</configSections>
<log4net>
<appender name="FileAppender"
type="log4net.Appender.FileAppender">
<param name="File" value="c:\\LogTest23.txt" />
<param name="AppendToFile" value="true" />
<layout type="log4net.Layout.PatternLayout">
<param name="Header" value="[Header]\r\n" />
<param name="Footer" value="[Footer]\r\n" />
<param name="ConversionPattern" value="%d [%t] %-5p %c
%m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="FileAppender" />
</root>
</log4net>
</configuration>
*_Logger class_*
private static readonly ILog logger =
LogManager.GetLogger(typeof(Class1));
log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));
logger.Debug("Here is a debug log.");
logger.Info("... and an Info log.");
logger.Warn("... and a warning.");
logger.Error("... and an error.");
logger.Fatal("... and a fatal error.");
I also tried using the <ClassLibraryName>.exe.config as the name for
the app.config as I read it somewhere
Then my ASP.net application creates an object of this Class1 from the
class library application and see if the log file is created
Could you please help me figure out what I am doing wrong?
Thank you very much
Dipty
Maybhate, Dipty
2006-08-17 18:04:45 UTC
Permalink
Thank you very much Steve

I am trying to add appenders external instead of calling a config file
in my class library -- wrapper class. This seems to work fine in test
example and I hopefully will work well after integrating with our
project.

Thanks

Dipty





________________________________

From: Steve Whitley [mailto:***@nullfx.com]
Sent: Wednesday, August 16, 2006 3:44 PM
To: Maybhate, Dipty; log4net-***@logging.apache.org
Subject: Re: FW: Problem logging in class library



Adding [assembly : log4net.Config.XmlConfigurator(Watch=true)] to your
assemblyinfo.cs file will load up a class that looks of an application
configuration file. If the application has one, it will attempt to load
up the configuration details from the config file and build your logging
system. the data in the config file specifies the appenders that will
listen for logging events, and any parameters they need, as well as what
LoggingLevel they will subscribe to.

if that data is not in the application configuration file then you will
have no appenders or repository set up to do your logging. If you plan
on users of your wrapper not having to do any configuring, then you need
to do a BasicConfigurator.Configure somewhere in the entrypoint to your
wrapper and setup log4net. there you can specify what repository you
want to use, and / or what appenders you want to use.

Maybhate, Dipty wrote:



Steve I once again really appreciate your help

As you suggested my application where I intend to create the wrapper
class for logging is a separate dll. I have created an app.config and
also added the assembly instruction as you have suggested.

My another application which has reference to the wrapper class project
has no code or mention for log4net. It just class the wrapper class
constructor which must do the logging at initialization and will
eventually call the writeLog, writeDebug etc of this wrapper.

I am not quite clear how my referencing application which is the ASP.net
should be configured to log by hand as you have suggested. Basically I
am not sure what log by hand means.

Thanks again!

Dipty



________________________________

From: Steve Whitley [mailto:***@nullfx.com]
Sent: Wednesday, August 16, 2006 1:21 PM
To: Maybhate, Dipty; Log4NET User
Subject: Re: FW: Problem logging in class library



if its a seperate dll, you just need to add [assembly :
log4net.Config.XmlConfigurator(Watch=true)], but the application you use
needs to have the appropriate xml structure in their app config.

if you want it to work regardless of if the referencing application has
log4net configured, you can configure it to log by hand (and I think
thats documented, though I cant point you to where off hand).

--steve

Maybhate, Dipty wrote:




Thanks Steve

I want the log4net to initialize in Class libraries. In my understanding
I would need to create like a log4net.config or some file like that and
mention that in AssemblyInfo.cs

if it should be done in class libraries.



Basically I have created a Logger class in the class library project and
reference it in ASP.net project in the same solution by creating its
constructor in Page_Load of the home page

When the constructor is called it goes to the log4net code

private static readonly ILog logger =

LogManager.GetLogger(typeof(Class1));

log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));

logger.Debug("Here is a debug log.");

logger.Info("... and an Info log.");

logger.Warn("... and a warning.");

logger.Error("... and an error.");

logger.Fatal("... and a fatal error.");



but doesn't create a log file

I hope this makes some sense

I have wasted like 2 days and also didn't have much time to read
anything on this

Thanks again!

Dipty



________________________________

From: Steve Whitley [mailto:***@nullfx.com]
Sent: Wednesday, August 16, 2006 12:41 PM
To: Log4NET User
Subject: Re: FW: Problem logging in class library



asp.net's config is called web.config. try looking for that.

Maybhate, Dipty wrote:









________________________________

From: Maybhate, Dipty
Sent: Wednesday, August 16, 2006 10:55 AM
To: 'log4net-***@logging.apache.org'
Subject: Problem logging in class library



Hi Everyone

I am new to log4net and I am facing problems with it. In my .Net
solution I have a class library project and a ASP.net project. I want to
create a Logger class which is like a wrapper in the class library and
have all the other Webservices or ASP.net projects in the solution
access that.

This is how I use log4net in the Class library



AssemblyInfo.cs

[assembly : log4net.Config.XmlConfigurator(Watch=true)]



App.config

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="log4net"

type="log4net.Config.Log4NetConfigurationSectionHandler,
log4net" />

</configSections>

<log4net>

<appender name="FileAppender"
type="log4net.Appender.FileAppender">

<param name="File" value="c:\\LogTest23.txt" />

<param name="AppendToFile" value="true" />

<layout type="log4net.Layout.PatternLayout">

<param name="Header" value="[Header]\r\n" />

<param name="Footer" value="[Footer]\r\n" />

<param name="ConversionPattern" value="%d [%t] %-5p %c
%m%n" />

</layout>

</appender>

<root>

<level value="INFO" />

<appender-ref ref="FileAppender" />

</root>

</log4net>

</configuration>



Logger class

private static readonly ILog logger =

LogManager.GetLogger(typeof(Class1));

log4net.Config.XmlConfigurator.ConfigureAndWatch(new
System.IO.FileInfo("App.config"));

logger.Debug("Here is a debug log.");

logger.Info("... and an Info log.");

logger.Warn("... and a warning.");

logger.Error("... and an error.");

logger.Fatal("... and a fatal error.");



I also tried using the <ClassLibraryName>.exe.config as the name for the
app.config as I read it somewhere

Then my ASP.net application creates an object of this Class1 from the
class library application and see if the log file is created

Could you please help me figure out what I am doing wrong?

Thank you very much

Dipty

Loading...