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# / September 2007

Tip: Looking for answers? Try searching our database.

C# log component

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George - 15 Sep 2007 10:12 GMT
Hello everyone,

I am using C# and Visual Studio 2005 to develop a class library. I am
wondering whether there are any built-in log component in C# so that I can
utilize directly other than write from scratch?

I am also wondering if there does exist such log component, if multiple
processes using the built-in log component to open the same log file to write
log (in my application, I want all processes which loads the class library
DLL to have a common log file), will there be any risk of racing condition
(e.g. interlacing log of one process and another process)? Do we need any
lock/synchronization approach?

thanks in advance,
George
Marc Gravell - 15 Sep 2007 10:21 GMT
You could try nlog: http://www.nlog-project.org/

Regarding the race condition - I believe most logging components will
worry about this on your behalf.

Marc
George - 15 Sep 2007 16:26 GMT
Thanks Marc,

You mean Microsoft .Net has no built-in log function?

regards,
George

> You could try nlog: http://www.nlog-project.org/
>
> Regarding the race condition - I believe most logging components will
> worry about this on your behalf.
>
> Marc
Morten Wennevik [C# MVP] - 15 Sep 2007 11:28 GMT
> Hello everyone,
>
[quoted text clipped - 11 lines]
> thanks in advance,
> George

Hi George,

In addition to Marc's suggestion there is also log4net

http://logging.apache.org/log4net/index.html

I haven't tried logging to file, but it seems robust enough that numerous logging sources should not be a problem.

Signature

Happy coding!
Morten Wennevik [C# MVP]

Andreas Mueller - 15 Sep 2007 12:08 GMT
>> Hello everyone,
>>
[quoted text clipped - 19 lines]
>
> I haven't tried logging to file, but it seems robust enough that numerous logging sources should not be a problem.

I'm using log4net in a large multi threaded application and it works
perfect.

HTH,
Andy

Signature

You can email me by removing the NOSPAM parts below:
xmen40NOSPAM@gmxNOSPAM.net

George - 15 Sep 2007 16:30 GMT
Thanks Andy,

Does it provide function to keep log size to some threshold? I do not want
the log file too big. Thanks.

regards,
George

> >> Hello everyone,
> >>
[quoted text clipped - 25 lines]
> HTH,
> Andy
Andreas Mueller - 15 Sep 2007 17:37 GMT
> Thanks Andy,
>
> Does it provide function to keep log size to some threshold? I do not want
> the log file too big. Thanks.

Yes, you can limit the size and also let the file appender roll. You can
specify multiple appenders, e.g. for sending your log output to the
console or a trace window.

It's very simple to configure and has a good performance.

HTH,
Andy

Signature

You can email me by removing the NOSPAM parts below:
xmen40NOSPAM@gmxNOSPAM.net

George - 16 Sep 2007 09:26 GMT
Thansk Andy,

I would like to try some function which is Microsoft built-in. :-)

Do you have any experiences of using TextWriterTraceListener?

http://msdn2.microsoft.com/en-us/library/system.diagnostics.textwritertraceliste
ner.aspx


Does it provide any function of limit the size of log? If it does not
provide the function, are there any way to implement the size limit function
application code?

regards,
George

> > Thanks Andy,
> >
[quoted text clipped - 9 lines]
> HTH,
> Andy
Andreas Mueller - 16 Sep 2007 12:02 GMT
> Thansk Andy,
>
[quoted text clipped - 7 lines]
> provide the function, are there any way to implement the size limit function
> application code?

No, I have never used it. From looking at the doc, I don't see a
limitation for the file size, too

Perhaps it is an option for you to implement a custom trace listener.
All you have to do (sounds simple :-) )is to derive from TraceListener
and implement the desired methods. You have to to the "ringbuffer"
writing yourself.

If you want to stick to MS stuff, how about the enterprise logging
application block?

HTH,
Andy

Signature

You can email me by removing the NOSPAM parts below:
xmen40NOSPAM@gmxNOSPAM.net

George - 16 Sep 2007 15:02 GMT
Thanks Andy,

The Enterprise Logging Application Block -- I have downloaded it, but I am
not sure whether it is too complex to use, and too much overhead. Since the
application I developed is only 60-70k, and I do not want the logging
component to add too much overhead to the application. How do you think
whether Enterprise Logging Application Block will add too much overhead to my
application?

Another concern is, if I use Enterprise Logging Application Block to my
application, will the customer who use the application has any additional
dependencies -- will they install any additional Runtime components (to
support Enterprise Logging Application Block ) in order to use the
application I developed?

regards,
George

> > Thansk Andy,
> >
[quoted text clipped - 21 lines]
> HTH,
> Andy
Andreas Mueller - 16 Sep 2007 19:14 GMT
> Thanks Andy,
>
[quoted text clipped - 4 lines]
> whether Enterprise Logging Application Block will add too much overhead to my
> application?
I have no experience here.

> Another concern is, if I use Enterprise Logging Application Block to my
> application, will the customer who use the application has any additional
> dependencies -- will they install any additional Runtime components (to
> support Enterprise Logging Application Block ) in order to use the
> application I developed?
AFAIK, nothing expect the assemblies of the application block itself.

If you are concerned about overhead and deployment I'd suggest again
log4net. It has a small runtime overhead (less that Trace) an you only
have to deploy one additional dll.

HTH,
Andy

Signature

You can email me by removing the NOSPAM parts below:
xmen40NOSPAM@gmxNOSPAM.net

George - 16 Sep 2007 09:28 GMT
Thanks Andy,

I want to use some Microsoft built-in function. :-)

Do you have any experiences of using TextWriterTraceListener?

http://msdn2.microsoft.com/en-us/library/system.diagnostics.textwritertraceliste
ner.aspx


Does it provide function of limit the size of log? If no such function
provided, how to implement from application code?

regards,
George

> > Thanks Andy,
> >
[quoted text clipped - 9 lines]
> HTH,
> Andy
George - 15 Sep 2007 16:28 GMT
Thanks Morten,

You mean Microsoft .Net itself does not provide log function? I only need to
log to a local file.

regards,
George

> > Hello everyone,
> >
[quoted text clipped - 19 lines]
>
> I haven't tried logging to file, but it seems robust enough that numerous logging sources should not be a problem.
Morten Wennevik [C# MVP] - 15 Sep 2007 16:57 GMT
> Thanks Morten,
>
[quoted text clipped - 31 lines]
>> Happy coding!
>> Morten Wennevik [C# MVP]

Not really.  The closes thing is the Trace and Debug classes.  But you need to write some code to make them good logging classes.

Signature

Happy coding!
Morten Wennevik [C# MVP]

George - 15 Sep 2007 17:20 GMT
Thanks Morten,

I have found one,TextWriterTraceListener.

http://msdn2.microsoft.com/en-us/library/system.diagnostics.textwritertraceliste
ner.aspx


After reading through this page, I am still a little confused about how to
use it step by step. Do you have a tutorial?

regards,
George

> > Thanks Morten,
> >
[quoted text clipped - 33 lines]
>
> Not really.  The closes thing is the Trace and Debug classes.  But you need to write some code to make them good logging classes.
Bob Powell [MVP] - 17 Sep 2007 18:28 GMT
The bank where I work as a software architect uses Log4Net extensively and
it's very reliable and simple to use.

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>> Hello everyone,
>>
[quoted text clipped - 24 lines]
> I haven't tried logging to file, but it seems robust enough that numerous
> logging sources should not be a problem.
DaveP - 15 Sep 2007 15:38 GMT
i wrote my own..logging....multi threaded service
1st thread starts the logging...all other services open and share the
log....
in my log class,
i wrote...
1. if can't log to sq db , 2 if i cant write to text file , 3 if that fails
in my app.config
1. i have sql serve connection string for log (table)
2. path and file name for log files (text files)

if you want to see the class....let me know
its work in progress..since im only about 1 year with c# net...all other
experience was win32, languages

DaveP

DaveP

write to event log
> Hello everyone,
>
[quoted text clipped - 12 lines]
> thanks in advance,
> George
DaveP - 15 Sep 2007 16:17 GMT
i typed the last comment all messed up
what i actually do is
1 try to write to my errorsdb..log table
  if fails
2. try to write to app defined text file log
 if that fails
3 try to write to machine event log
  if all 3 of the above fails... well somthing is really broken

  i never write to all 3 at once....some people do...
  i just think that is a waist of time and resources

DaveP

>i wrote my own..logging....multi threaded service
> 1st thread starts the logging...all other services open and share the
[quoted text clipped - 35 lines]
>> thanks in advance,
>> George
George - 15 Sep 2007 16:32 GMT
Thanks DaveP,

Why you use the special order (db, text and event log)? Any reasons to
design like this?

regards,
George

> i typed the last comment all messed up
> what i actually do is
[quoted text clipped - 49 lines]
> >> thanks in advance,
> >> George
George - 15 Sep 2007 16:32 GMT
Thanks for your kindness DaveP!

I have only used for C# for half a year. If possible, could I learn your
code please? From your description, it looks like what I am looking for.

regards,
George

> i wrote my own..logging....multi threaded service
> 1st thread starts the logging...all other services open and share the
[quoted text clipped - 31 lines]
> > thanks in advance,
> > George
DaveP - 15 Sep 2007 16:49 GMT
i did that order....since all my services are back end services, for web and
processing of data comming into the site (transactions and other
information)
the reason i only write 1 place ....is why write all 3 places
when only 1 will suffice.....less use on resources and performance
reasons....

i write to the sql log..our database..table (not sqls log)
this lets us have a interface to the web site under admin mode..to monitor
all the processing services...

if i can't connect with the db when i write a log entry...the log class
writes the current entry to a text file on the shared folder where logs are
located...if i can't write to our text file logs...i then write the error /
entry to the machine event handler.....
hope that makes more sense to ya
Davep
send me a email to dvs_bis ~ sbcglobal.net
replace  the ~ with @
i'll be glad to send the code to ya

> Thanks for your kindness DaveP!
>
[quoted text clipped - 44 lines]
>> > thanks in advance,
>> > George
George - 15 Sep 2007 17:22 GMT
Thanks DaveP,

Looks like your code is very comprehensive and tested with real application.
I am sending you an email. Thanks again!

have a good weekend,
George

> i did that order....since all my services are back end services, for web and
> processing of data comming into the site (transactions and other
[quoted text clipped - 65 lines]
> >> > thanks in advance,
> >> > George
Arne Vajhøj - 15 Sep 2007 19:41 GMT
> I am using C# and Visual Studio 2005 to develop a class library. I am
> wondering whether there are any built-in log component in C# so that I can
[quoted text clipped - 6 lines]
> (e.g. interlacing log of one process and another process)? Do we need any
> lock/synchronization approach?

You can get a "Logging Application Block" in "Enterprise Library"
from Microsoft.

But I would recommend log4net.

Arne
George - 16 Sep 2007 09:20 GMT
Thanks Arne,

I want to use something simple. :-)

Do you have any experiences of TextWriterTraceListener?

http://msdn2.microsoft.com/en-us/library/system.diagnostics.textwritertraceliste
ner.aspx


Is it thread and application safe?

regards,
George

> > I am using C# and Visual Studio 2005 to develop a class library. I am
> > wondering whether there are any built-in log component in C# so that I can
[quoted text clipped - 13 lines]
>
> Arne
Arne Vajhøj - 16 Sep 2007 22:14 GMT
> I want to use something simple. :-)
>
[quoted text clipped - 3 lines]
>
> Is it thread and application safe?

I have not tried it.

The link you gave say:

    Thread Safety
    Any public static (Shared in Visual Basic) members of this type
    are thread safe. Any instance members are not guaranteed to be
    thread safe.

Arne
Arnshea - 17 Sep 2007 19:50 GMT
> Hello everyone,
>
[quoted text clipped - 11 lines]
> thanks in advance,
> George

I'm a big fan of the System.Diagnostics tracing classes.  Typical
usage is to instantiate the TraceListener derived class for the
storage medium you want to you use (e.g., EventLogTraceListener to
write to the windows event log, TextWriterTraceListener to write to a
file) then conditionally trace based on a TraceSwitch.

You can automatically configure trace switches and listeners from your
app.config file.

A few caveats: TextWriterTraceListener uses exclusive access to the
file so you'll have to use one of its other constructors to specify
shared access, you'll have to handle synchronization yourself, be
careful with the EventLogTraceListener as it can easily fill up the
windows event log in a space constrained situation.

Code-wise what you'll end up with is lots of statements like:

Trace.WriteLineIf(switch.TraceInfo, "....")

where you set the value of the trace switch in your app config file,
instantiate it and add listeners via config file or programmatically
to your listener collection.

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.