c# - Efficient way of logging to txt file from a console program -


what efficient way of recording log (.txt) console program on c# , .net 2.2? program loops multiple times outputting different data based on user wants, i'm searching efficient way achieve this.

i know can reopen stream , close it, everytime writing 1 line, next time around (seconds later) program reloops , needs tor write again. in opinion, doesn't seem resourse friendly.

i'm using multiple threads have output data want log (opening/closing same file or accessing same file on different threads might bad). "holds reference stream writer auto-flushes" sounds idea, don't know how that.

you hook tracing framework forms part of clr. using simple class like: http://www.chaosink.co.uk/files/tracing.zip can selectively log diagnostic information. use add class application. create inistance of tracer in class like:

private tracing trace = new tracing("my.namespace.class"); 

and call using:

myclass() {     trace.verbose("entered myclass");     int x = 12;     trace.information("x is: {0}", x);     trace.verbose("leaving myclass"); } 

there 4 levels of information in inbuilt tracing framework:

verbose - log program flow

information - log specific information of interest monitors

warning - log invalid state or recoverable exception

error - log unrecoverable exception or state

to access information application add app.config (or web.config) following:

<system.diagnostics>     <trace autoflush="false" indentsize="4">         <listeners>             <add name="mylistener" type="system.diagnostics.textwritertracelistener" initializedata="c:\mylogfile.log" />         </listeners>     </trace>     <switches>         <add name="my.namespace.class" value="4"/>           </switches> </system.diagnostics> 

you can attach listeners publishing eventlog or anywhere else interests you. more information on tracing framework can found at:

http://msdn.microsoft.com/en-us/library/ms733025.aspx


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -