473,734 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

log4net. Where to put BasicConfigurat or.Configure() when your codespans multiple projects

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?
Jun 19 '06 #1
6 8106
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********@fli bbyly.wobbly.n. et> wrote in message
news:e7******** ***@custnews.in web.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?

Jun 19 '06 #2
* Lalit Bhatia wrote:
You have to call XmlConfigurator .Configure(...) to initialise only once in
the first project where you have your main() method.


My problem is that I dont have a main() so I cant get my head around
where is the best place to place the configurator.

I have a website that creates an object x. x just returns some data
that the website presents. this object x uses objects y and z to get the
data. I want tracing in x , y , and z.. and of course the website
itself. and preferably just a single XML configuaration file.

Where do I configure log4net? Within the website itself?, individually
in the constructors of all of x, y & z? If I call
XmlConfigurator .Configure() from the website can I magically use
LogManger.GetLo gger from the components even though they are in seperate
bin files (physically rather than logically, they're all related by
their parent namespace).

I'm confused can some kind soul shed some light?
Jun 19 '06 #3
I tend to put mine in my most upper level class before the UI or the
EnterpriseServi ce 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 SystemModelCont roller(string strApplicationP ath)
{

log4net.Config. XmlConfigurator .Configure();

_strApplication Path = strApplicationP ath;
MyObj.ConfigFil ePath = _strApplication Path + "\\" +
System.Configur ation.
ConfigurationSe ttings.AppSetti ngs["system_config_ name"];

MyObj.Initializ e();

}

Then in all my classes:

private static ILog _l4nSystemLogge r =
LogManager.GetL ogger(System.Re flection.Method Base.
GetCurrentMetho d().DeclaringTy pe);

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.C onfig.Log4NetCo nfigurationSect ionHandler, log4net" />
</configSections>

<log4net debug="false">
<appender name="SystemLog " type="log4net.A ppender.Rolling FileAppender">
<param name="File" value=".\\SYSTE M\\" />
<param name="Threshold " value="ALL" />
<param name="AppendToF ile" value="true" />
<param name="RollingSt yle" value="Date" />
<param name="MaxSizeRo llBackups" value="10" />
<param name="DatePatte rn"
value="yyyy\\MM MMMMMMM\\yyyyMM dd.\M\Y\F\I\L\E "/>
<param name="StaticLog FileName" value="False" />
<layout type="log4net.L ayout.PatternLa yout">
<header value="[BEGIN LOGGING AT %date ]%newline"
type="log4net.U til.PatternStri ng" />
<footer value="[END LOGGING]%newline"
type="log4net.U til.PatternStri ng" />
<param name="Conversio nPattern" 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********@fli bbyly.wobbly.n. et> wrote in message
news:e7******** ***@custnews.in web.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?


Jun 19 '06 #4
* Dinsdale wrote:
I tend to put mine in my most upper level class before the UI or the
EnterpriseServi ce wrapper,


Ok, for me I dont have any top level class so the only place I could
conievabely configure the root logger is in the UI (which I dont want to
do in case different UI's are used, or the UI is infact removed
completely). Does it make sense for each class to initialise it's own
root logger or should there be just one BasicConfigurat or.Configure()
(or similar initiatlisation statement) within a solution? i.e. does
this hierarchial logging only work if Configure() is used in one place
only? Also what about calling GetLogger() within a class in a seperate
project, how does it get connected to the root logger in the calling
app/site/client if that is itself in a different project?

Sorry if my questions are confusing , it's because I myself am confused :-)
Jun 19 '06 #5
As I understand it, the logger is a static object. Once the Configure()
function is called, all you have to do is get the reference to the
logger. Therefore, so long as your objects are running in the same
AppDomain, the config is still valid. It doesn't matter about the
design time structure. If l4n is configured, any class that calls the
GetLogger() function gets the instance for the whole app (that is,
depending on how you set L4N up. You can create different loggers and
then get the reference to that specific logger as well). If it's not
configured, it doesn't seem to do anything (again, so far as I can
tell). I forgot to configure an app once and I didn't realize what was
wrong until much later because there was no exception. :)

Remember, each asp.net "site" runs as an application and all the pages
use the same AppDomain. It doesn't matter about the class structure
when the app is running - It seems to me you are confusing the idea of
referencing classes in a project with what is going on here. The static
instance of the Logger is shared across the entire application
regardless of the "physical" structure of the classes. If you configure
the L4N in your globals.asax page, then all pages called after that can
use the same instance because the GetLogger call I posted previously
simply says: "Get me the Logger that is being used by this class".
Since I don't have any class specific logger, it gets the one that is
specific to the application.

APPLICATION
- L4N Logger Instance 1

- Page1 instance 1 -> Uses l4n1
- Page1 instance 2 -> Still using l4n1
- page2 instance 1 -> you got it, l4n1
Two side notes that are entirely my opinion:

1) It sounds to me like this is the perfect opportunity for you to
design a hierachy and a class that takes care of configuration (which
is what I do in my top level class). How bout a static class that has
one function called Begin() that calls all config stuff?

2) I would recommend signing up to the Log4Net mailing list. Those guys
are totally brilliant and are usually very quick at getting answers
back. The only time I ever had to wait, one of the guys was away or
something and he answered my 3 week old message anyways!

Cheers
Russ

Mr Flibble wrote:
* Dinsdale wrote:
I tend to put mine in my most upper level class before the UI or the
EnterpriseServi ce wrapper,


Ok, for me I dont have any top level class so the only place I could
conievabely configure the root logger is in the UI (which I dont want to
do in case different UI's are used, or the UI is infact removed
completely). Does it make sense for each class to initialise it's own
root logger or should there be just one BasicConfigurat or.Configure()
(or similar initiatlisation statement) within a solution? i.e. does
this hierarchial logging only work if Configure() is used in one place
only? Also what about calling GetLogger() within a class in a seperate
project, how does it get connected to the root logger in the calling
app/site/client if that is itself in a different project?

Sorry if my questions are confusing , it's because I myself am confused :-)


Jun 19 '06 #6
* Dinsdale wrote:
As I understand it, the logger is a static object. Once the Configure()
function is called, all you have to do is get the reference to the
logger. Therefore, so long as your objects are running in the same
AppDomain, the config is still valid.
Firstly thanks for your very imformative reply. Hopefully it will also
help other people who stumble upon it via google groups et al.

Now to some more questions I have :-) You're right I was wondering how
to get a reference back to the original instantiation of the previously
configured Logger object. Seems that you're saying that so long as they
are within the same AppDomain I dont need to care, where or how (or even
who by) it was configured. OK this is great, but sadly I can't see of a
good place to put the configuration..

I have a site Default.aspx that in it's Default.aspx.cs calls a method
on an instance a of data retrieval class. I also have a service that
that runs independantly to the website that is basically a data
gatherer. This data retriever just serves the data gathered by this.
Are these considered different AppDomains?

Assuming they are, or can be made to be, I guess the place that makes
the most sense for the configuration of l4net would be in the webpage
(globals.aspx), since as you can see I really dont have any top-level
object since I have webpage that creates an object and a service that
creates an object. No real class hierarchy or relation to one another.

This worries me since I dont want the UI to be webpage bound.
Two side notes that are entirely my opinion:

1) It sounds to me like this is the perfect opportunity for you to
design a hierachy and a class that takes care of configuration (which
is what I do in my top level class). How bout a static class that has
one function called Begin() that calls all config stuff?
I could create a utility function "ConfigureLoggi ng" and make sure the
UI calls it. This would make it UI independant but would mean that the
UI "would need to know" that it must do this. I was hoping of hiding
this from the UI.
2) I would recommend signing up to the Log4Net mailing list.


I shall. :-)
Jun 19 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
466
by: Joneleth | last post by:
Hi, anyone can help with log4net? i'm not sure i understand log4net object model properly... i'm trying to configure log4net programmatically, in order to assign different custom appenders to some custom loggers (run-time) and (i think) i've been able to add new custom loggers to the root logger of the repository. i've also added one appender for each custom logger. this is the code snippet i wrote:
3
2217
by: cwineman | last post by:
Hello, I posted this question in microsoft.public.dotnet.framework and didn't get any responses. Maybe somebody here has some input. If anyone knows of a better place to ask these questions, let me know. My group is considering using the log4net framework for logging. I'm hoping someone here can give me some feedback on some questions. Our application will need to log messages from both managed and unmanaged
2
6268
by: Alpha Zero | last post by:
how to configure log4net programmatically? I just want to modify some of the properties,which are read from configuration file, programatically. Regards,
1
2040
by: Ram | last post by:
Hey, I'm using the Log4Net opensource object to write to my logs. I'v written a wrapper ClassLibrary project for the Log4Net and all my projects reference only to it. I'v got a web application thats calls a com+ application (- another project of mine that is in the same solution) and I want both of this "layers" to use my Log4Net Wrapper, and - here comes the tricky part - write to the same Log file. When the Com+ application is set to...
4
2207
by: Gawel | last post by:
hajo, can someone send me an example how to use above pattern in asp.net applicaiton? I can not figure out how to do it. I get no exceptions, simply no logging occures. Thanks for any info Gawel
2
5153
by: Vikram | last post by:
I am using log4net in a vb.net console application. But its not working. I have added congif setting in app.config file also No error is generated but app. does not write to log file. app.config file: <?xml version="1.0" encoding="utf-8" ?>
1
2911
by: softwareakash | last post by:
Hi Anyone using log4net with vs2005. See my blog entry at http://akashdotnet.blogspot.com/ I would appreciate any feedback and comments Akash
1
5177
by: chidam.chidam | last post by:
Hi all, I would like to implement log4net for logging my application. First let me describe in short about the application. I have a solution with two exe file outputs and several dll assemblies. And my application has a different AppDomain as it is a customization of another COM based application written in VB.NET. I have tried with App.Config files in my startup exe applications. It works fine only till the parent application is...
2
20037
by: forest demon | last post by:
I need to be able to create an appender to direct output to a MessageBox. The API does not have a MessageBox appender like nLog does. What would be the easiest way to do this. The examples supplied with the source are anything but helpful. Thanks for any input. -fd
0
8776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.