I tend to put mine in my most upper level class before the UI or the
EnterpriseService wrapper, then each class has a static reference to
the logger. I do this so that the logging is tied to my Object model
rather than my UI or wrapper and if the functional spec changes, the
logging still works. I had one project that had a UI but was then
integrated into another app just using the class library I had built. I
had another one turn into a service over night (last minute customer
request). Since the Logging was initialized in my libraries, the
switch over was flawless. The only problem is if the UI has an
exception before you initialize your object model, or during the
initialization process, you won't get that logged so you need to be
careful where you do things (don't do anything until your logger is
initialized).
Initialization:
public SystemModelController(string strApplicationPath)
{
log4net.Config.XmlConfigurator.Configure();
_strApplicationPath = strApplicationPath;
MyObj.ConfigFilePath = _strApplicationPath + "\\" +
System.Configuration.
ConfigurationSettings.AppSettings["system_config_name"];
MyObj.Initialize();
}
Then in all my classes:
private static ILog _l4nSystemLogger =
LogManager.GetLogger(System.Reflection.MethodBase.
GetCurrentMethod().DeclaringType);
I usually have 3 or 4 projects per solution and they all use the that
single init call in the main class.
Here is a config file section that creates a rolling file appender
based on date:
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHa ndler, log4net" />
</configSections>
<log4net debug="false">
<appender name="SystemLog" type="log4net.Appender.RollingFileAppender">
<param name="File" value=".\\SYSTEM\\" />
<param name="Threshold" value="ALL" />
<param name="AppendToFile" value="true" />
<param name="RollingStyle" value="Date" />
<param name="MaxSizeRollBackups" value="10" />
<param name="DatePattern"
value="yyyy\\MMMMMMMMM\\yyyyMMdd.\M\Y\F\I\L\E"/>
<param name="StaticLogFileName" value="False" />
<layout type="log4net.Layout.PatternLayout">
<header value="[BEGIN LOGGING AT %date ]%newline"
type="log4net.Util.PatternString" />
<footer value="[END LOGGING]%newline"
type="log4net.Util.PatternString" />
<param name="ConversionPattern" value="%d [%t] %-5p %c [%x] - %m%n"
/>
</layout>
</appender>
<root>
<appender-ref ref="SystemLog" />
</root>
</log4net>
(Courtesy of Nicko at Log4Net - Thanks again dude!)
Cheers
Russ
Lalit Bhatia wrote:
You have to call XmlConfigurator.Configure(...) to initialise only once in
the first project where you have your main() method.
--
Regards,
Lalit Bhatia
"Mr Flibble" <mr********@flibbyly.wobbly.n.et> wrote in message
news:e7***********@custnews.inweb.co.uk... Hi all,
I've decided to use log4net for my logging/tracing.
In the example on the site it shows using main() to setup the root
logger and then using the LogManager within your classes to create more
loggers (subloggers). This is nice since I can use just one
configuration file to control the behaviour of all the loggers within my
app. This is great if I'm using a project that contains all my classes
but what happens if my class is in another namespace in another project?
My namespaces span multiple projects...
Do I have to initialise a root logger within each project?
What do people do in this situation?