Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Logging Class - Instance or Static

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JJ - 27 Jun 2007 15:30 GMT
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?
Tom Spink - 27 Jun 2007 15:34 GMT
> 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?

Signature

Tom Spink
University of Edinburgh

JJ - 27 Jun 2007 16:08 GMT
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.

> > 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
[quoted text clipped - 6 lines]
> 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?
Ignacio Machin ( .NET/ C# MVP ) - 29 Jun 2007 16:14 GMT
Hi,

> 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
[quoted text clipped - 3 lines]
> and I am wondering if that is the proper coding technique to be using
> here?

I will definetely will not pass an instance around. A logging class makes
sense to have it accesible from all over the app, I would select either a
static class or a Singleton
wfairl@gmail.com - 27 Jun 2007 16:14 GMT
> 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 ApplicationBlocks? 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
Martin# - 27 Jun 2007 16:20 GMT
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.

> 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?
Bruce Wood - 27 Jun 2007 16:52 GMT
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.

> I would recomend to use a Singleton class here.
> This means a static GetInstance method which only instanciates the class
[quoted text clipped - 7 lines]
>
> - Show quoted text -
JJ - 27 Jun 2007 21:18 GMT
So in using a singleton I am going to call it directly like so,
singletonClass.Instance().method;
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?

> I would recomend to use a Singleton class here.
> This means a static GetInstance method which only instanciates the class
[quoted text clipped - 5 lines]
> > wouldn't need to
> > pass an instance around. What is your take on this?
Martin# - 28 Jun 2007 07:02 GMT
Hello,

A singleton method looks normaly like this.

private static MyClass instance = null;
public static MyClass GetInstance()
{
   if(instance==null)
   {
       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.Namespace.MyClass myreferencetomyclass =
Your.Full.Namespace.MyClass.GetInstance();

Hop it helps!

All the best,

Martin

> So in using a singleton I am going to call it directly like so,
>  singletonClass.Instance().method;
[quoted text clipped - 10 lines]
> > > wouldn't need to
> > > pass an instance around. What is your take on this?
Jon Skeet [C# MVP] - 28 Jun 2007 07:18 GMT
> A singleton method looks normaly like this.
>
[quoted text clipped - 9 lines]
>     return instance;
> }

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

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Martin# - 28 Jun 2007 07:30 GMT
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

> > A singleton method looks normaly like this.
> >
[quoted text clipped - 15 lines]
> See http://pobox.com/~skeet/csharp/singleton.html
>  
JJ - 28 Jun 2007 14:22 GMT
You wouldn't necessarily create one ref to singleton and pass it around to
the different classes. I figure I would get a ref to getInstance whenever I
would need it and destroy it after I am done using it in each class. Is that
how you guys would use it?

> Hello,
>
[quoted text clipped - 36 lines]
> > > > wouldn't need to
> > > > pass an instance around. What is your take on this?
Martin# - 28 Jun 2007 14:32 GMT
Hello,

Yes, you don't have to pass around it, cause every class can call the
GetInstance().
"destroy" as you sayed, is only a "null" set .

All the best,

Martin

> You wouldn't necessarily create one ref to singleton and pass it around to
> the different classes. I figure I would get a ref to getInstance whenever I
[quoted text clipped - 41 lines]
> > > > > wouldn't need to
> > > > > pass an instance around. What is your take on this?
JJ - 28 Jun 2007 15:28 GMT
Thank you all!

> Hello,
>
[quoted text clipped - 51 lines]
> > > > > > wouldn't need to
> > > > > > pass an instance around. What is your take on this?
Bruce Wood - 28 Jun 2007 18:04 GMT
Oh, and GetInstance() might be better implemented in C# as an Instance
property. I think it reads better:

Logger.Instance.Log(...);

versus

Logger.GetInstance().Log(...);

> Hello,
>
[quoted text clipped - 53 lines]
>
> - Show quoted text -
Fernando Botelho - 29 Jun 2007 11:41 GMT
> Oh, and GetInstance() might be better implemented in C# as an Instance
> property. I think it reads better:
[quoted text clipped - 64 lines]
>
> - Show quoted text -

Why don't you use a simple static method in a static class (if you are
using .NET Framework 2.0)?
I think the singleton instance, in this case, is completely useless.

Regards,
Fernando
Ignacio Machin ( .NET/ C# MVP ) - 29 Jun 2007 16:22 GMT
Hi,

>> Oh, and GetInstance() might be better implemented in C# as an Instance
>> property. I think it reads better:
[quoted text clipped - 77 lines]
> using .NET Framework 2.0)?
> I think the singleton instance, in this case, is completely useless.

Not really, if the logging facility change dynamically (file, eventlog,
email, SMS, etc) you will want a Factory that build the concrete class
according some parameters:

class Log
{
   ILog current;
static    public ILog GetLogger{ get{return current;}}
static Log()
{
 switch ( flag)
{
  case Email:  current = new EmailLogger();break;
  case Eventlog:  current = new EL_Logger();break;
  case textl:  current = new FileLogger();break;
}
}
}
interface ILog{ void Log(......);}
class EmailLogger:ILog{}
....
Fernando Botelho - 29 Jun 2007 19:17 GMT
On Jun 29, 12:22 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
[quoted text clipped - 103 lines]
>
> - Show quoted text -

Hi Ignacio,

.NET 2.0 have some facilities to work with logging, based on
configuration files (*.config's). You don't need to switch which
logging class use based on a flag passed from client classes.
Instead, .NET Framework 2.0 have classes (namespace
System.Diagnostics) that abstract this job for us. For more
information, take a look at http://msdn2.microsoft.com/en-us/library/system.diagnostics.aspx

What I want to say, is that your (static) class can abstract the BLC
methods to do this kind of job, instead of create instance class (the
unique advantage from this is that you can decide, based on an commom
interface, which class to use) and verify other things about log
source etc.

ps.: sorry by my poor english. my natural language is another one.
Ignacio Machin ( .NET/ C# MVP ) - 29 Jun 2007 16:09 GMT
Hi,
>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.

Why singleton over a static class?

The only reason I can think of is if you want to be able to change the
logging tool under need, let's say from EventLog to a file.
Ignacio Machin ( .NET/ C# MVP ) - 29 Jun 2007 16:12 GMT
Hi,

>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?

If you use it in different assemblies you will have to place it in a
separate assembly that all other projects will need to add a reference to.

Regarding your question, I have a similar situation, I decided to use a
static class and based on a flag I use either the EventLog or dump it to a
file.

Agreed, my environment is simple, if you have a dynamic environment you
should implement something more fancy like a Factory.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.