On Sep 15, 5:12 am, George <Geo...@discuss ions.microsoft. comwrote:
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
I'm a big fan of the System.Diagnost ics tracing classes. Typical
usage is to instantiate the TraceListener derived class for the
storage medium you want to you use (e.g., EventLogTraceLi stener to
write to the windows event log, TextWriterTrace Listener 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: TextWriterTrace Listener 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 EventLogTraceLi stener 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.WriteLine If(switch.Trace Info, "....")
where you set the value of the trace switch in your app config file,
instantiate it and add listeners via config file or programmaticall y
to your listener collection.