473,545 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Logging Class - Instance or Static

I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?
Jun 27 '07 #1
19 6056
JJ wrote:
I have a logging component that I will access in other assemblies. So it
was brought up to me that I should pass an instance around to these
components instead of just making the logging class static. If I make it
static I wouldn't need to
pass an instance around. What is your take on this?
Hi,

An important decision, and it depends on your needs. Does your class need
to retain state? How complicated is it? How memory-hungry is it?

--
Tom Spink
University of Edinburgh
Jun 27 '07 #2
Its a pretty simple class that will send emails, write to eventlog or to a
file. My thinking is if I pass an instance around and it gets passed down a
chain of about 3 classes then I will have multiple refs to my logging class
and I am wondering if that is the proper coding technique to be using here?
Does your class need
to retain state?
No my class doesn't need to retain state but if I make it static I can just
refer to directly from any where without having refs to it.

"Tom Spink" wrote:
JJ wrote:
I have a logging component that I will access in other assemblies. So it
was brought up to me that I should pass an instance around to these
components instead of just making the logging class static. If I make it
static I wouldn't need to
pass an instance around. What is your take on this?

Hi,

An important decision, and it depends on your needs. Does your class need
to retain state? How complicated is it? How memory-hungry is it?

--
Tom Spink
University of Edinburgh
Jun 27 '07 #3
On Jun 27, 10:30 am, JJ <J...@discussio ns.microsoft.co mwrote:
I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?
Have you seen the Microsoft Enterprise ApplicationBloc ks? There is a
logging block that should meet your needs. You might want to either
use this instead or model your component after it (the source code is
provided).

http://msdn2.microsoft.com/en-us/library/aa480464.aspx
Jun 27 '07 #4
I would recomend to use a Singleton class here.
This means a static GetInstance method which only instanciates the class
once and gives this instance back.

"JJ" wrote:
I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?
Jun 27 '07 #5
I would check out the Microsoft Enterprise Application Block first.

If it doesn't suit your needs, or you don't have time to study it,
then I would side with Martin: create a logging instance class, then
create a static method that returns a (singleton) instance of it. I
would also create a logging interface and have the static method
return that type.

The logic behind this is that you can then (if you want to) create
other logging classes that implement the same interface but log in
different ways, and swap them out with a minimum of fuss.

On Jun 27, 8:20 am, Martin# <Mart...@discus sions.microsoft .comwrote:
I would recomend to use a Singleton class here.
This means a static GetInstance method which only instanciates the class
once and gives this instance back.

"JJ" wrote:
I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?- Hide quoted text -

- Show quoted text -

Jun 27 '07 #6
So in using a singleton I am going to call it directly like so,
singletonClass. Instance().meth od;
and with using a singleton I don't have to pass it around from class to
class, I can just call it as above, correct?

"Martin#" wrote:
I would recomend to use a Singleton class here.
This means a static GetInstance method which only instanciates the class
once and gives this instance back.

"JJ" wrote:
I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?
Jun 27 '07 #7
Hello,

A singleton method looks normaly like this.

private static MyClass instance = null;
public static MyClass GetInstance()
{
if(instance==nu ll)
{
instance = new MyClass(); //Only done once
}
// You can do other stuff here
// For example calling some Init methods
return instance;
}

And will be called like this:
Your.Full.Names pace.MyClass myreferencetomy class =
Your.Full.Names pace.MyClass.Ge tInstance();

Hop it helps!

All the best,

Martin
"JJ" wrote:
So in using a singleton I am going to call it directly like so,
singletonClass. Instance().meth od;
and with using a singleton I don't have to pass it around from class to
class, I can just call it as above, correct?

"Martin#" wrote:
I would recomend to use a Singleton class here.
This means a static GetInstance method which only instanciates the class
once and gives this instance back.

"JJ" wrote:
I have a logging component that I will access in other assemblies. So it was
brought up to me that I should pass an instance around to these components
instead of just making the logging class static. If I make it static I
wouldn't need to
pass an instance around. What is your take on this?
Jun 28 '07 #8
Martin# <Ma****@discuss ions.microsoft. comwrote:
A singleton method looks normaly like this.

private static MyClass instance = null;
public static MyClass GetInstance()
{
if(instance==nu ll)
{
instance = new MyClass(); //Only done once
}
// You can do other stuff here
// For example calling some Init methods
return instance;
}
Except that's not thread-safe. It's simpler to use a static
initializer.

See http://pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 28 '07 #9
Good point for the concept!

But what I meant was alittle more custom specific.
For example if the sender is passed as a parameter, there would be the write
place to forward it to the accurate method.

All the best,

Martin

"Jon Skeet [C# MVP]" wrote:
Martin# <Ma****@discuss ions.microsoft. comwrote:
A singleton method looks normaly like this.

private static MyClass instance = null;
public static MyClass GetInstance()
{
if(instance==nu ll)
{
instance = new MyClass(); //Only done once
}
// You can do other stuff here
// For example calling some Init methods
return instance;
}

Except that's not thread-safe. It's simpler to use a static
initializer.

See http://pobox.com/~skeet/csharp/singleton.html

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 28 '07 #10

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

Similar topics

1
2119
by: Huzefa | last post by:
I am working on a amll project in Java that includes many classes. Each of the classes has a Logger object. I have associated a FileHandler with each of these Logger objects. The file is the same for each of these classes "log.xml" Now I want all the classes to log to the same file. However, this does not happen. Each class creates its own...
8
1979
by: Steve Erickson | last post by:
I have a logger class that uses the Python logging module. When I call it within a program using the unittest module, I get one line in the log file for the first test, two identical ones for the second, etc. I'm using local variables, which should go out of context with each test. Setting the 'propagate' property to False doesn't have any...
1
2310
by: Spur | last post by:
Hi all, I implemented a memory allocation/deallocation class that logs all new/delete calls (overloaded) and remembers for each allocated block where it was allocated from (using a macro that passes __FILE__ and __LINE__). On destruction, it reports all undeleted blocks (memory leaks). The class is implemented in a separate .h/.cpp...
6
7308
by: pmatos | last post by:
Hi all, I am trying to create a simple but efficient C++ logging class. I know there are lots of them out there but I want something simple and efficient. The number one requirement is the possibility of shutting logging down at compile time and suffer no performance penalty whatsoever for getting logging on whenever I wish. Of course that...
4
1662
by: Daniel Bass | last post by:
Hi, I've a situation where my application has to manage usernames and passwords, and after a certain amount of idle time, the application will log the current user out, (assuming they've left without logging out manually) and prompt for a new log in. Is there any way to do this, without having to call a reset method on the timer i've got...
2
1547
by: Chumma Dede | last post by:
Hi, I need to code a DLL in .NET which logs the response times for our asp.net multi-tier application. The problem is we need to log the timestamps at multiple stages in a process lifecycle roundtrip without too much overhead. We have two webservers which are load balanced and the back end tiers include Biztalk and several remoting...
3
2457
by: Chris Smith | last post by:
Hola, pythonisas: The documentation for the logging module is good, but a bit obscure. In particular, there seems to be a lot of action at a distance. The fact that getLogger() can actually be a call to Logger.__init__(), which is mentioned in para 6.29.1, also bears stressing on 6.29. I grasp _why_ you'd implement it that way, but I may not...
1
319
by: BobLaughland | last post by:
Hi All, I am development an ASP .NET C# web site. Just wondering the best way to implement logging on my web site. (I have a logging class to log database access, exceptions, etc, etc). I have about 10 pages in my web site (that is 10 seperate c# files with code), and 4 separate classes in the App_Code directory.
12
1907
by: Eric S. Johansson | last post by:
I need to to be able to conditionally log based on the method the log statement is in and one other factor like a log level. in order to do so, I need to be able to automatically find out the name of the method and its class but I haven't found out how to do that yet. for example, class catus(Felis): def Siamese_cat( yowl, purr,...
0
7468
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7401
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...
0
7656
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. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7757
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...
0
5972
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...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3450
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...
0
704
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...

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.