Efficient way to log in txt file from console

What would be the most efficient way to write to the log (.txt) from a console program in C # and .NET 2.2? My program loop several times always outputs different data based on what the user wants, so I'm looking for the most efficient way to achieve this.

I know that I can always resume a stream and then close it, but every time I do this, it writes only one line, and then the next time (after a few seconds) the program is reinstalled and needs to be written again. In my opinion, this does not seem very friendly to the relationship.

I use several streams that have output that I want to register (opening / closing the same file or accessing the same file on different streams can be bad). “Has a link to the author of the thread that automatically resets” sounds like a good idea, but I don’t know how to do it.

+4
source share
6 answers

You can connect to the trace structure, which is part of the CLR. Using a simple class: http://www.chaosink.co.uk/files/tracing.zip , you can selectively record diagnostic information. To use it, add a class to your application. Create a patron protection in your class, for example:

private Tracing trace = new Tracing("My.Namespace.Class"); 

and call it using:

 MyClass() { trace.Verbose("Entered MyClass"); int x = 12; trace.Information("X is: {0}", x); trace.Verbose("Leaving MyClass"); } 

The built-in trace structure has 4 levels of information:

Verbose - for registering a program stream

Information - to register specific information of interest to monitors

A warning. To register an invalid state or a recoverable exception.

Error - to register a fatal exception or condition

To access information from your application, add the following to app.config (or web.config):

 <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 also connect listeners to publications in the event log or in any other place that interests you. More information about the trace structure can be found at:

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

+3
source

Consider using log4net :

a tool that helps the programmer to output logical operators to many output targets ... We have kept the structure similar in spirit to the original log4j, using new features in the .NET runtime. For more information about log4net, see the Features document ...

+16
source

I take the performance hit that comes with opening and closing every time I write a line. This is a decision on reliability. If you keep everything in mind and have a serious accident, you do not have log information to help troubleshoot. If this does not bother you, then holding it in memory will certainly provide better performance.

+3
source

I agree with the use of log4net.

But if you really need something simple, think of it, just having a singleton that contains a link to StreamWriter, which is automatically reset to each WriteLine.

Thus, you keep the file open during the session, avoiding closed / open overheads, without risking losing log data in the event of a hard failure.

+2
source

A simple approach would be to provide a TextWriterTraceListener and add it to the TraceListeners collection in the Trace class. This will automatically write all your Trace.Write requests ... to the appropriate file.

0
source

Logging structures such as log4net must handle multithreading.

There may also be a useful hint: the journal significantly pollutes the code, making it less readable. Have you considered using an aspect to introduce logging functions to code at compile time? See this article for an example .

0
source

Source: https://habr.com/ru/post/1277323/


All Articles